I have two forms, one with a data grid and the other with individual controls (detail form), both bound to a database.
What do I need to do to be able to click on a particular row (record) in the data grid and have it open the detail form with that record displayed I can get it to open the form, but it just opens to the first record. I need it to open the record I clicked on the data grid form.
Thanks.

Opening Database Record on a Separate Form
ddaSedN
99.99% is initialize compoent code generated by the IDE. . .
there are three forms BaseForm, BrowseForm & DetailsForm.
BrowseForm & DetailsForm inherit from BaseForm which just simply defines a common layout:
[BaseForm Image]
[DetailsForm Image]
[BrowseForm Image]
BaseForm has no procedural code in it. just a right aligned panel (protected) which contains a button (protected)
DetailsForm inherits from base form and contains a datagrid and sets the Button: Accept=True and Cancel=true.
Proceduraly, DetailsForm hides its contructor, defines a method for loading a DataRowView into its grid and Defines a static ExecuteMethod that takes a DataRowView as a argument, instances a DetailsForm and loads the form with the DataRowView:
static public void Execute(IWin32Window owner,
DataRowView aRow)
{
using (DetailForm instance = new DetailForm())
{
instance.LoadRowView(aRow);
instance.ShowDialog(owner);
}
}
void LoadRowView(DataRowView aRowView)
{
DataTable dt = new DataTable();
dt.Columns.Add("Field", typeof(string));
dt.Columns.Add("Value", typeof(object));
for (int i = 0 ; i< aRowView.Row.Table.Columns.Count; i++)
dt.Rows.Add(new object[]
{aRowView.Row.Table.Columns[ i ].ColumnName,
aRowView[ i ]});
dataGrid1.DataSource = dt;
}
BrowseForm has a DataGrid on it.
BrowseForm simply opens a query in its contructor, defines a DoubleClick handler for the DataGrid to load the details form, a Click event for the button inherited from base form, and the [STAThread] Main procedure:
public BrowseForm()
{
InitializeComponent();
/* CHANGE connstr TO MATCH YOUR DATABASE */
using (System.Data.SqlClient.SqlConnection con =
new System.Data.SqlClient.SqlConnection(connstr))
/* CHANGE Sql TO MATCH YOUR DATABASE */
using( System.Data.SqlClient.SqlDataAdapter da =
new System.Data.SqlClient.SqlDataAdapter(Sql, con))
{
DataSet ds = new DataSet();
da.Fill(ds);
dataGrid1.DataSource = ds.Tables[0].DefaultView;
}
}
private void dataGrid1_DoubleClick(object sender,
System.EventArgs e)
{
/* HERE'S THE MEAT AND POTATOES!!! */
CurrencyManager cm =
dataGrid1.BindingContext[dataGrid1.DataSource]
as CurrencyManager;
if (cm == null) return;
if(cm.Current == null) return;
if ((cm.Current as DataRowView) == null) return;
DetailForm.Execute(this, (cm.Current as DataRowView));
}
private void button1_Click(object sender,
System.EventArgs e)
{
this.Close();
}
[STAThread]
static void Main()
{
Application.Run(new BrowseForm());
}
running the app opens BrowseForm:
[BrowseForm (Run-Time) Image]
Double-Clicking the grid opens the DetailsForm:
[DetailsForm (Run-Time) Image]
Notes:
1. You could use "hittest" in the double click event to determine that you are actually on a row.
2. You can use DataRowView just as you can a DataRow. In your design, consider whether, when browsing, you want to already have all your data columns and limit display via grid column definition or you want the data to be composed of a minimal set of the columns. If the later, the DataRowView would be used to then requery the database to get the details. There are pros and cons to both scenarios. In the case here, I have all the data already and simply display the row.
hope that helps
Amjad
is your problem solved by Blair Stark's solution. if not let me know.......!
prageeth gunathilaka
prageeth1980925@hotmail.com
Darren Kennedy
no. . .no. . . no. . .no!!!!!
use DataGrid.BindingContext
run this code (just be sure to change the DB connection string and the select statements in the first couple of lines in the top form. . .
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace UsingCurrencyManager
{
public class BrowseForm : BaseForm
{
/*CHANGE THIS TO REFLECT YOUR DATABASE!!!*/
const string connstr = "workstation id=ARCTURUS;packet size=4096;integrated security=SSPI;data source=loc" +
"alhost;persist security info=False;initial catalog=Northwind";
/*CHANGE THIS TO REFLECT YOUR DATABASE AS WELL!!!!! */
const string Sql = "SELECT * FROM DBO.EMPLOYEES";
private System.Windows.Forms.DataGrid dataGrid1;
private System.ComponentModel.Container components = null;
public BrowseForm()
{
InitializeComponent();
using (System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(connstr))
using( System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(Sql, con))
{
DataSet ds = new DataSet();
da.Fill(ds);
dataGrid1.DataSource = ds.Tables[0].DefaultView;
}
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
private void InitializeComponent()
{
this.dataGrid1 = new System.Windows.Forms.DataGrid();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
this.button1.Click += new System.EventHandler(this.button1_Click);
this.dataGrid1.DataMember = "";
this.dataGrid1.Dock = System.Windows.Forms.DockStyle.Fill;
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(0, 0);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(370, 210);
this.dataGrid1.TabIndex = 0;
this.dataGrid1.DoubleClick += new System.EventHandler(this.dataGrid1_DoubleClick);
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(436, 210);
this.Controls.Add(this.dataGrid1);
this.Name = "BrowseForm";
this.Text = "Form1";
this.Controls.SetChildIndex(this.dataGrid1, 0);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);
}
[STAThread]
static void Main()
{
Application.Run(new BrowseForm());
}
private void button1_Click(object sender, System.EventArgs e)
{
this.Close();
}
private void dataGrid1_DoubleClick(object sender, System.EventArgs e)
{
CurrencyManager cm = dataGrid1.BindingContext[dataGrid1.DataSource] as CurrencyManager;
if (cm == null) return;
if(cm.Current == null) return;
if ((cm.Current as DataRowView) == null) return;
DetailForm.Execute(this, (cm.Current as DataRowView));
}
}
public class DetailForm : BaseForm
{
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.DataGridTableStyle dataGridTableStyle1;
private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn1;
private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn2;
private System.ComponentModel.Container components = null;
void LoadRowView(DataRowView aRowView)
{
DataTable dt = new DataTable();
dt.Columns.Add("Field", typeof(string));
dt.Columns.Add("Value", typeof(object));
for (int i = 0 ; i< aRowView.Row.Table.Columns.Count; i++)
{
dt.Rows.Add(new object[]{aRowView.Row.Table.Columns[ i ].ColumnName, aRowView[ i ]});
}
dataGrid1.DataSource = dt;
}
static public void Execute(IWin32Window owner, DataRowView aRow)
{
using (DetailForm instance = new DetailForm())
{
instance.LoadRowView(aRow);
instance.ShowDialog(owner);
}
}
private DetailForm()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
private void InitializeComponent()
{
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.dataGridTableStyle1 = new System.Windows.Forms.DataGridTableStyle();
this.dataGridTextBoxColumn1 = new System.Windows.Forms.DataGridTextBoxColumn();
this.dataGridTextBoxColumn2 = new System.Windows.Forms.DataGridTextBoxColumn();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
this.button1.DialogResult = System.Windows.Forms.DialogResult.OK;
this.dataGrid1.AllowNavigation = false;
this.dataGrid1.DataMember = "";
this.dataGrid1.Dock = System.Windows.Forms.DockStyle.Fill;
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(0, 0);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(304, 320);
this.dataGrid1.TabIndex = 1;
this.dataGridTableStyle1.DataGrid = null;
this.dataGridTableStyle1.GridColumnStyles.AddRange(new System.Windows.Forms.DataGridColumnStyle[] {
this.dataGridTextBoxColumn1,
this.dataGridTextBoxColumn2});
this.dataGridTableStyle1.HeaderFont = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.dataGridTableStyle1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGridTableStyle1.MappingName = "";
this.dataGridTextBoxColumn1.Alignment = System.Windows.Forms.HorizontalAlignment.Right;
this.dataGridTextBoxColumn1.Format = "";
this.dataGridTextBoxColumn1.FormatInfo = null;
this.dataGridTextBoxColumn1.HeaderText = "Field Name";
this.dataGridTextBoxColumn1.MappingName = "Field";
this.dataGridTextBoxColumn1.ReadOnly = true;
this.dataGridTextBoxColumn1.Width = 75;
this.dataGridTextBoxColumn2.Format = "";
this.dataGridTextBoxColumn2.FormatInfo = null;
this.dataGridTextBoxColumn2.HeaderText = "Field Value";
this.dataGridTextBoxColumn2.MappingName = "Value";
this.dataGridTextBoxColumn2.ReadOnly = true;
this.dataGridTextBoxColumn2.Width = 75;
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(384, 320);
this.Controls.Add(this.dataGrid1);
this.Name = "DetailForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Text = "DetailForm";
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);
}
}
public class BaseForm : System.Windows.Forms.Form
{
private System.Windows.Forms.Panel panel1;
protected System.Windows.Forms.Button button1;
private System.ComponentModel.Container components = null;
public BaseForm()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
private void InitializeComponent()
{
this.panel1 = new System.Windows.Forms.Panel();
this.button1 = new System.Windows.Forms.Button();
this.panel1.SuspendLayout();
this.SuspendLayout();
this.panel1.Controls.Add(this.button1);
this.panel1.Dock = System.Windows.Forms.DockStyle.Right;
this.panel1.Location = new System.Drawing.Point(226, 0);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(66, 266);
this.panel1.TabIndex = 0;
this.button1.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.button1.Location = new System.Drawing.Point(10, 10);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(50, 24);
this.button1.TabIndex = 0;
this.button1.Text = "&Ok";
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.panel1);
this.Name = "BaseForm";
this.Text = "BaseForm";
this.panel1.ResumeLayout(false);
this.ResumeLayout(false);
}
}
}
Brian Crawford
but if it is windows application u can do the same by
putting a hidden column to the grid with the primary key. for the click event of the rowselect event of the data grid get the value of the primary key of the clicked row. then send it to the other form with the data set, u may use a public static variable (easiest) . use the findBy<primary key> method (if it is a typed data set, else use select method) to get the row of data from the grid. then u can do what u want.
OR
u can get the fresh data from the database using the primary key.
i think this can slove the problem.
thanks
prageeth gunathilaka
prageeth1980925@hotmail.com
limno
This seems like an awful lot of code for this operation: could you possible break it down for me a little Or can you direct me to a site where I can study up on the concept behind this code In the mean time I'll walk through it myself and try to figure out what's going on.
I appreciate the help very nuch!