Communication Between Two Forms: Part-II


Last time I wrote about the possibility of sending a string from a textbox in one form to another. Some people wrote to me asking if it could work in other situations, but one of them was very interesting, so I thought Id share this code with you as well! The scenario is to have a datagrid in a form and when you click the cell in the datagrid twice, you can see the result in a picturebox situated in another form.


In this example I used Access as database, connecting to it with ADO.NET. So we need an OleDbConnection, OleDbCommand, OleDbAdapter, and a DataSet.

Look at this example:

private System.Data.OleDb.OleDbDataAdapter oleDbDataAdapter1;
private System.Data.OleDb.OleDbCommand oleDbSelectCommand1;
private System.Data.OleDb.OleDbConnection oleDbConnection1;
private SendingPicture.DataSet1 dataSet11;
this.oleDbDataAdapter1 = new System.Data.OleDb.OleDbDataAdapter();
this.oleDbSelectCommand1 = new System.Data.OleDb.OleDbCommand();
this.oleDbConnection1 = new System.Data.OleDb.OleDbConnection();
this.dataSet11 = new SendingPicture.DataSet1();
// oleDbDataAdapter1
this.oleDbDataAdapter1.SelectCommand = this.oleDbSelectCommand1;
new System.Data.Common.DataTableMapping[]
new System.Data.Common.DataTableMapping("Table", "tbl_Test", new System.Data.Common.DataColumnMapping[]
new System.Data.Common.DataColumnMapping("IDImmagine","IDImmagine"),
new System.Data.Common.DataColumnMapping("Descrizione","Descrizione")
// oleDbSelectCommand1
this.oleDbSelectCommand1.CommandText = "SELECT IDImmagine,Descrizione FROM tbl_Test";
this.oleDbSelectCommand1.Connection = this.oleDbConnection1;

Look at here carefully

// oleDbConnection1
this.oleDbConnection1.ConnectionString =
@"Provider=Microsoft.Jet.OLEDB.4.0;Password="""";User ID=Admin;Data
Source=" + Environment.CurrentDirectory + @"\Test.mdb;Mode=Share
Deny None;Extended Properties="""";Jet OLEDB:System database="""";Jet
OLEDB:Registry Path="""";Jet OLEDB:Database Password="""";Jet
OLEDB:Engine Type=5;Jet OLEDB:Database Locking Mode=1;Jet
OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;Jet
OLEDB:New Database Password="""";Jet OLEDB:Create System
Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy
Locale on Compact=False;Jet OLEDB:Compact Without Replica
Repair=False;Jet OLEDB:SFP=False";
// dataSet11
this.dataSet11.DataSetName = "DataSet1";
this.dataSet11.Locale = new System.Globalization.CultureInfo("it-IT");
this.dataSet11.Namespace =;

and then in the Form-Load:

private void Form1_Load(object sender, System.EventArgs e)

finally in the double-click event:

private void dataGrid1_DoubleClick_1(object sender, System.EventArgs e)
// here you can now the row number cell when you double-click
int i = dataGrid1.CurrentCell.RowNumber;
Form2 frm =
new Form2();
// and now you can load the image...dataGrid1[i,1]
frm.pictureBox1.Image = System.Drawing.Image.FromFile
Environment.CurrentDirectory + @"\Images\" + dataGrid1[i,1].ToString());

thats it !!!

Similar Articles