Programmactially adding picturebox in C# - c#

Ok so I am testing with adding a picturebox to my winform app. I am finally asking here, because when ever I look up how to do this I don't see anything different than what I am doing. Here is the code:
namespace AddPanel
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
PictureBox pictureBox1 = new PictureBox();
pictureBox1.ImageLocation = #"C:\Users\xoswaldr\Desktop\OrangeLogo.jpg";
pictureBox1.Location = new System.Drawing.Point(20, 40);
pictureBox1.Name = "pictureBox1";
pictureBox1.Size = new System.Drawing.Size(100, 50);
pictureBox1.BackColor = Color.Black;
this.Controls.Add(pictureBox1);
}
}
}
That is the entire code, because I am just trying to test adding a picturebox for something else I'm working on. What I am trying to do with this is when I run the program it puts the picturebox in the form, but that doesn't happen. The form is just blank.
-----EDIT------------
Here is the Form1.Designer.cs code
namespace AddPanel
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.SuspendLayout();
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(690, 381);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
}
}
and here is the Program.cs code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace AddPanel
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
Is there something in the designer that is blocking it or something that I haven't added?

Since your code looks correct, is it possible there is another control covering the picture box?
Try bringing it to the Front:
private void Form1_Load(object sender, EventArgs e)
{
var pictureBox1 = new PictureBox
{
BackColor = Color.Black,
ImageLocation = #"C:\Users\xoswaldr\Desktop\OrangeLogo.jpg",
Location = new Point(20, 40),
Name = "pictureBox1",
Size = new Size(100, 50)
};
this.Controls.Add(pictureBox1);
pictureBox1.BringToFront();
}

The picture may not be completely displayed, just set the SizeMode to have a try:
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom

Related

How to display employee information in form2 by specifying emp id in form1?

This is my form1 which I designed. When I type emp id in form1's textbox and click on the search button it shows form2.
In this second form I need to carry all the details corresponding to emp id and should display details into corresponding textboxes.
I have created emp table in SQL Server...I would like to pull the employee details from the database based on emp id. This is my code:
form1:
private void btnsearch_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2(tbempid.Text);
f2.Show();
SqlConnection con = new SqlConnection("Data Source=RAJIM-PC;Initial Catalog=Practicing;User ID=sa;Password=RajiSha");
try
{
con.Open();
SqlCommand com = new SqlCommand("SELECT eid,emp_name,mobile_no FROM emp WHERE ID='" + tbempid.Text.Trim() + "'", con);
com.CommandType = CommandType.Text;
DataTable dtb = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(com);
da.Fill(dtb);
if (dtb.Rows.Count > 0)
{
Form2.txtempid.Text= dtb.Rows[0]["eid"].ToString();
Form2.txtempname.Text = dtb.Rows[0]["emp_name"].ToString();
Form2.txtmbno.Text= dtb.Rows[0]["mobile_no"].ToString();
}
FormCollection fc = System.Windows.Forms.Application.OpenForms;
foreach (Form f in fc)
{
if (f.Name == "Form2")
{
f.Update();
}
}
}
catch (SqlException sql)
{
System.Windows.Forms.MessageBox.Show(sql.Message);
}
finally
{
if (con.State == ConnectionState.Open)
{
con.Close();
con.Dispose();
}
}
}
form2:
public partial class Form2 : Form
{
public static string txtempid;
public static string txtempname;
public static string txtmbno;
public Form2(string strtxtbox)
{
InitializeComponent();
tbempid.Text = strtxtbox;
}
}
Looks like your doing all the work for filling out Form2 in Form1, I would suggest moving the code into Form2 and calling it from the Form2's constructor. Not only will this make more sense, but means you will only hit your database once to get the results.
Alternatively, you could pass Form2 your data table from Form1 and use the values in Form2's constructor. Again, saving a second query going to the database.
You can take constructor of form 2 as shown below :-
public Form2(string txtempid, string txtempname, string txtmbno)
{
InitializeComponent();
txtempid.Text = txtempid;
txtempname.Text = txtempname;
txtmbno.Text = txtmbno;
}
and in form 1 :-
var form2 = new Form2(dtb.Rows[0]["eid"].ToString(), dtb.Rows[0]["emp_name"].ToString(), dtb.Rows[0]["mobile_no"].ToString());
You should change Form2 to f2 here:
Form2.txtempid.Text= dtb.Rows[0]["eid"].ToString();
Form2.txtempname.Text = dtb.Rows[0]["emp_name"].ToString();
Form2.txtmbno.Text= dtb.Rows[0]["mobile_no"].ToString();
So it becomes:
f2.txtempid.Text= dtb.Rows[0]["eid"].ToString();
f2.txtempname.Text = dtb.Rows[0]["emp_name"].ToString();
f2.txtmbno.Text= dtb.Rows[0]["mobile_no"].ToString();
Update 1
You are trying to assign those fields to the string variables. However, what you should do is to assign them to relevant textbox controls like this:
Assume you have 3 textboxes named tbempid, tbempname, tbmbno on Form2
f2.tbempid.Text= dtb.Rows[0]["eid"].ToString();
f2.tbempname.Text = dtb.Rows[0]["emp_name"].ToString();
f2.tbmbno.Text= dtb.Rows[0]["mobile_no"].ToString();
Update 2
Due to the protection level, you need to add a function in Form2:
public void SetTextBoxes(string strempid, string strempname, string strmbno)
{
tbempid.Text = strempid;
tbempname.Text = strempname;
tbmbno.Text = strmbno;
}
And change the 3 lines to:
f2.SetTextBoxes(dtb.Rows[0]["eid"].ToString(), dtb.Rows[0]["emp_name"].ToString(), dtb.Rows[0]["mobile_no"].ToString());
The following is a very quick example that I've thrown together of what I suggested above. It does not follow best practice for purposes of simplicity. The areas to concentrate on below is the EmployeeModel class, Form1 and Form2 source.
The idea here is utilize the EmployeeModel class as a container that both forms have access to.
Form 1 acquires the employee information. (Keep in mind, the model class can contain any information you like. It does not have to be reserved for just properties. If you prefer, you can keep a reference to a dataset here.)
Form 2 has a reference to this EmployeeModel class. When the button click event is fired on form1, a new EmployeeModel class object is created and initialized with the relevant information that would be displayed in form2. It is then passed as an object reference using Form2's overloaded constructor.
On the Onload event of Form2, the label.Text properties are initialized with what is contained in the EmployeeModel that was passed as a reference.
This is a very basic implementation and in most real world applications where expandability and maintainability are also factors to consider, a MVP or MVVM WinForms architectural framework is typically used.
Form 1 designer:
namespace FormToFormExample
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(12, 12);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(260, 20);
this.textBox1.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(197, 64);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 2;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 100);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
}
}
Form 2 designer:
namespace FormToFormExample
{
partial class Form2
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 9);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(35, 13);
this.label1.TabIndex = 0;
this.label1.Text = "label1";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(12, 34);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(35, 13);
this.label2.TabIndex = 1;
this.label2.Text = "label2";
//
// Form2
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 262);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "Form2";
this.Text = "Form2";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
}
}
Model class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FormToFormExample
{
public class EmployeeModel
{
#region Properties
private Guid _employeeID;
public Guid EmployeeID
{
get { return this._employeeID; }
set { this._employeeID = value; }
}
private string _name;
public string Name
{
get { return this._name; }
set { this._name = value; }
}
#endregion
#region Constructors
public EmployeeModel()
{
this._employeeID = Guid.NewGuid();
}
public EmployeeModel(string name)
: this()
{
this._name = name;
}
#endregion
}
}
Form 1 source:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace FormToFormExample
{
public partial class Form1 : Form
{
#region Constructors
public Form1()
{
InitializeComponent();
Initialize();
BindComponents();
}
#endregion
#region Methods
private void BindComponents()
{
this.button1.Click += button1_Click;
}
private void Initialize()
{
this.textBox1.Text = string.Empty;
}
#endregion
#region Events
void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2(new EmployeeModel(textBox1.Text));
form2.ShowDialog();
Initialize();
}
#endregion
}
}
Form 2 source:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace FormToFormExample
{
public partial class Form2 : Form
{
EmployeeModel _model;
#region Constructors
public Form2()
{
InitializeComponent();
BindComponents();
}
public Form2(EmployeeModel model)
: this()
{
this._model = model;
}
#endregion
#region Methods
private void BindComponents()
{
this.Load += Form2_Load;
}
private void Initialize()
{
this.label1.Text = this._model.EmployeeID.ToString();
this.label2.Text = this._model.Name;
}
#endregion
#region Events
void Form2_Load(object sender, EventArgs e)
{
Initialize();
}
#endregion
}
}
Program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace FormToFormExample
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}

Moving Frameless Windows Form via Mouse Drag

I'm new to C# and I've attempted many of the solutions here on StackExchange and I've had no luck in trying to set up a control or the entire form for that matter to left click and drag the entire window. I'm working with a frameless window in visual studio 12. The closest I've come to moving the window is moving a single control with the pastebin component(last response) from this-
How do I make mousedrag inside Panel move form window?
I could only get the panel itself to move with that component.
I've tried most of the approaches but I seem get lost where I am to customize it to my own needs. I've tried WndProc override but it didn't do anything when I attempted to move the form window.
I have two panels I want to be able to drag the window with DragPanel and DragPanel2.
Here is my most recent failed approach trying to use the whole form.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
InsideMover _dragger = new InsideMover();
_dragger.ControlToMove = this.DragPanel;
}
private void close_Click(object sender, EventArgs e)
{
Close();
}
}
public class InsideMover : Component
{
/// <summary>
/// Required designer variable.
/// </summary>
private Container components = null;
public InsideMover(IContainer container)
{
///
/// Required for Windows.Forms Class Composition Designer support
///
container.Add(this);
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
public InsideMover()
{
///
/// Required for Windows.Forms Class Composition Designer support
///
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
public Control ControlToMove
{
set
{
if (_parent != null)
{
// odkvaci prijasnje evente
_parent.MouseDown -= new MouseEventHandler(_parent_MouseDown);
_parent.MouseMove -= new MouseEventHandler(_parent_MouseMove);
_parent.MouseUp -= new MouseEventHandler(_parent_MouseUp);
_parent.DoubleClick -= new EventHandler(_parent_DoubleClick);
}
_parent = value;
if (value != null)
{
// zakači se na evente od containera koji ti trebaju
_parent.MouseDown += new MouseEventHandler(_parent_MouseDown);
_parent.MouseMove += new MouseEventHandler(_parent_MouseMove);
_parent.MouseUp += new MouseEventHandler(_parent_MouseUp);
_parent.DoubleClick += new EventHandler(_parent_DoubleClick);
}
}
get
{
return _parent;
}
}
Control _parent;
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
#endregion
int _lastMouseX;
int _lastMouseY;
bool _moving;
public void StartMouseDown(MouseEventArgs e)
{
_parent_MouseDown(null, e);
}
private void _parent_MouseDown(object sender, MouseEventArgs e)
{
_lastMouseX = e.X;
_lastMouseY = e.Y;
_moving = true;
}
private void _parent_MouseMove(object sender, MouseEventArgs e)
{
if (_moving)
{
Point newLocation = _parent.Location;
newLocation.X += e.X - _lastMouseX;
newLocation.Y += e.Y - _lastMouseY;
_parent.Location = newLocation;
}
}
private void _parent_MouseUp(object sender, MouseEventArgs e)
{
_moving = false;
}
private void _parent_DoubleClick(object sender, EventArgs e)
{
if (_parent is Form)
{
Form f = (Form)_parent;
if (f.WindowState == FormWindowState.Normal)
{
f.WindowState = FormWindowState.Maximized;
}
else
{
f.WindowState = FormWindowState.Normal;
}
}
}
}
}
How can I set the panels to left click drag the window?
I've tried all of the methods at the post above and the WndProc method here:
Drag borderless windows form by mouse
If you follow the answer in the link you've posted, it is using the mousemove event, whereas judging by the code you've posted, you're using the mousedown event. The mouse down event is only called once when you press a mouse button, it is not called again if you move the mouse while you keep pressing the button. Whereas the mousemove event is called whenever your pointer moves. So your best bet would be to change your mousedown event with the mousemove event i.e.
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
}
if that doesn't work, you can do something like this in the mousemove event, first create a Point 'prevpoint' and an offset point in the form.
Point prevpoint=new Point(0,0);
Point offset=new Point(0,0);
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
offset.X=e.X-prevpoint.X;
offset.Y=e.Y-prevpoint.Y;
prevpoint.X=e.X;
prevpoint.Y=e.Y;
this.Location = new Point(this.Location.X + offset.X, this.Location.Y + offset.Y);
}
}
I have not tested the above code but that will hopefully give you the basic idea.

Accessing Forms data from another form

"Give the code below, how would I access the listBox1 in Form2? I'm sure I'm missing stupid! Thanks in advance."
Error 1 'WindowsFormsApplication1.Form2.listBox1' is inaccessible due
to its protection
level C:\Users\dugaj0\Desktop\Developing\GlobalUser\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 24 19 WindowsFormsApplication1
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
String value1 = File.ReadAllText(textBox1.Text);
foreach (string line in value1.Split('\n'));
Form2.listBox1.Items.Add();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
namespace WindowsFormsApplication1
{
partial class Form2
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.listBox1 = new System.Windows.Forms.ListBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// listBox1
//
this.listBox1.FormattingEnabled = true;
this.listBox1.Location = new System.Drawing.Point(13, 13);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(259, 212);
this.listBox1.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(105, 231);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 1;
this.button1.Text = "Exit";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form2
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 262);
this.Controls.Add(this.button1);
this.Controls.Add(this.listBox1);
this.Name = "Form2";
this.Text = "Form2";
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Button button1;
}
}
First of all you have to create an instance of Form2.
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private Form2 form2;
public Form1()
{
InitializeComponent();
form2 = new Form2();
}
private void button1_Click(object sender, EventArgs e)
{
String value1 = File.ReadAllText(textBox1.Text);
foreach (string line in value1.Split('\n'))
{
form2.listBox1.Items.Add(line);
}
}
}
}
Your specific error is because of listBox1 is being private. Change it to public and you can access it.
public System.Windows.Forms.ListBox listBox1;
Also since you already have:
using System.Windows.Forms;
You can write
public ListBox listBox1;

open form with crystalreportsviewer using C#

I'm working on one application. in datagridview I have created event dataGridView1_CellContentDoubleClick which would supposed to open another form that has crystalreportviewer component, then pass the path of the crystalreportdocument to the crystalreportviewer and load that report so it can present data...
so far I have done this:
private void dataGridView1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
{
//string that carries path of the report document
string path = dataGridView1.SelectedRows[0].Cells["ReportPath"].Value.ToString();
}
Form3 looks like:
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ICAMReports
{
public partial class Form3 : Form
{
public Form3(string path)
{
InitializeComponent();
}
}
}
Form3.Designer looks like:
namespace ICAMReports
{
partial class Form3
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.crystalReportViewer1 = new CrystalDecisions.Windows.Forms.CrystalReportViewer();
this.SuspendLayout();
//
// crystalReportViewer1
//
this.crystalReportViewer1.ActiveViewIndex = -1;
this.crystalReportViewer1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.crystalReportViewer1.Cursor = System.Windows.Forms.Cursors.Default;
this.crystalReportViewer1.Dock = System.Windows.Forms.DockStyle.Fill;
this.crystalReportViewer1.Location = new System.Drawing.Point(0, 0);
this.crystalReportViewer1.Name = "crystalReportViewer1";
this.crystalReportViewer1.Size = new System.Drawing.Size(609, 413);
this.crystalReportViewer1.TabIndex = 0;
//
// Form3
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(609, 413);
this.Controls.Add(this.crystalReportViewer1);
this.Name = "Form3";
this.Text = "Form3";
this.ResumeLayout(false);
}
#endregion
private CrystalDecisions.Windows.Forms.CrystalReportViewer crystalReportViewer1;
}
}
You create a new form in your project. Then in that form's code you change the constructor to take a string argument that is the report path.
The code you then use to show that form is:
//CrystalReportForm is your new form.
CrystalReportForm form = new CrystalReportForm(path);
form.Show(); //or form.ShowDialog();
Event:
private void dataGridView1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
{
string path = dataGridView1.SelectedRows[0].Cells["ReportPath"].Value.ToString();
Form3 form = new Form3(path);
//ReportDocument crystal = new ReportDocument();
//crystal.Load(dataGridView1.SelectedRows[0].Cells["ReportPath"].Value.ToString());
//pass = crystal;
form.Show();
}
Form3.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ICAMReports
{
public partial class Form3 : Form
{
public string source;
public Form3(string path)
{
source = path;
InitializeComponent();
}
private void Form3_Load(object sender, EventArgs e)
{
this.crystalReportViewer1.ReportSource = source;
}
}
}
Form3.Designer.cs
namespace ICAMReports
{
partial class Form3
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.crystalReportViewer1 = new CrystalDecisions.Windows.Forms.CrystalReportViewer();
this.SuspendLayout();
//
// crystalReportViewer1
//
this.crystalReportViewer1.ActiveViewIndex = -1;
this.crystalReportViewer1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.crystalReportViewer1.Cursor = System.Windows.Forms.Cursors.Default;
this.crystalReportViewer1.Dock = System.Windows.Forms.DockStyle.Fill;
this.crystalReportViewer1.Location = new System.Drawing.Point(0, 0);
this.crystalReportViewer1.Name = "crystalReportViewer1";
this.crystalReportViewer1.Size = new System.Drawing.Size(609, 413);
this.crystalReportViewer1.TabIndex = 0;
//
// Form3
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(609, 413);
this.Controls.Add(this.crystalReportViewer1);
this.Name = "Form3";
this.Text = "Form3";
this.Load += new System.EventHandler(this.Form3_Load);
this.ResumeLayout(false);
}
#endregion
private CrystalDecisions.Windows.Forms.CrystalReportViewer crystalReportViewer1;
}
}

How to get event for ComboBox inside of Gridview

How to get event for ComboBox inside of Gridview using C# Windows Application... Anyone Tell me the solution of this problem.....
Thanks in Advance...
check this http://www.eggheadcafe.com/community/aspnet/2/10098379/gridview.aspx
I think you are looking at the wrong place. For value change event, you should use your class's property changed event that is bound to that specific column in the GridView. Because, when you change value in the ComboBoxColumn, it will in turn update the value in the object that is bound to that row.
Example:
Designer generated code
`partial class Form1
{
///
/// Required designer variable.
///
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.dataGridView1 = new System.Windows.Forms.DataGridView();
this.Column1 = new System.Windows.Forms.DataGridViewComboBoxColumn();
this.Column2 = new System.Windows.Forms.DataGridViewTextBoxColumn();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout();
//
// dataGridView1
//
this.dataGridView1.AllowUserToAddRows = false;
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.Column1,
this.Column2});
this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
this.dataGridView1.Location = new System.Drawing.Point(0, 0);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.RowTemplate.Height = 24;
this.dataGridView1.Size = new System.Drawing.Size(282, 255);
this.dataGridView1.TabIndex = 0;
//
// Column1
//
this.Column1.DataPropertyName = "Value1";
this.Column1.HeaderText = "Column1";
this.Column1.Name = "Column1";
//
// Column2
//
this.Column2.DataPropertyName = "Value2";
this.Column2.HeaderText = "Column2";
this.Column2.Name = "Column2";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(282, 255);
this.Controls.Add(this.dataGridView1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.DataGridView dataGridView1;
private System.Windows.Forms.DataGridViewComboBoxColumn Column1;
private System.Windows.Forms.DataGridViewTextBoxColumn Column2;
}`
Code Behind
`public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// Column1 is
Column1.DataPropertyName = "Value1";
Column1.Items.AddRange(Enumerable.Range(1, 10).Select(i => i.ToString()).ToArray());
Column2.DataPropertyName = "Value2";
dataGridView1.DataError += new DataGridViewDataErrorEventHandler(dataGridView1_DataError);
dataGridView1.DataSource = (from obj in Enumerable.Range(1, 20)
select new Model() { Value1 = obj, Value2 = ("Value2 #" + obj.ToString()) }).ToList();
}
void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
System.Diagnostics.Debug.WriteLine(e.Exception.ToString());
}
}
class Model
{
protected Int32 _value1;
public Int32 Value1
{
get
{
return _value1;
}
set
{
// Here is your property change event
MessageBox.Show(value.ToString());
_value1 = value;
}
}
public String Value2 { get; set; }
}`

Categories

Resources