Click here to Skip to main content
Click here to Skip to main content
Go to top

DataGrids, Forms and Delegates.

, 17 Feb 2004
Rate this:
Please Sign up or sign in to vote.
This article describes using a Delegate to pass data from a DataGrid in one form, to set of controls in another form.


This article will explain how to select a row on a Data Grid in one Windows Form, and populate controls in another Windows Form using a Delegate.The example in this article comes from trying to find a way to pass data between two separate forms  that were located in a custom plug-in I created.  Both forms did not have a parent form so to speak, that I could use to act as a bridge between the two. '


There have been lots of articles describing what delegates are and how they are set up, I'll leave that to the reader and will just jump right into the code. '

Using the code

The code in this example consists of a Data Grid located in FormBottom and a set of controls located in FormTop.  By selecting a row from the Data Grid in FormBottom you can automatically populate the controls in FormTop using a Delegate.  Make sense? Continue reading.

In FormBottom.cs we define our delegate.

public delegate void CustomRowHandler(
  object sender, CustomRowEventArgs e);

and the event handler, which is defined as static.

public static event CustomRowHandler CustomRow;

The CustomRowEventArgs class.  The parameters passed are the DataSet, DataGrid and the Row selected.

 public class CustomRowEventArgs : EventArgs
    DataSet  dataSet;
    DataGrid grid;
    int row;
    public CustomRowEventArgs(
      DataSet DSet,DataGrid Grid,int Row)
      grid = Grid;
      row = Row;
      dataSet = DSet;
    public DataSet DSet
      get { return dataSet; }
    public DataGrid Grid
      get { return grid; }
    public int Row
      get { return row; }

We initialize the Data Grid when the form loads. See the project example code for more information on BindFamousPeople(). DataGridTableStyle is used to get the CurrentRowIndex of the DataGrid. We simply pass a DataGridTableStyle to the DataGrid, which will then be used in the MouseUp routine to get the current row.

    private void FormBottom_Load(
      object sender, System.EventArgs e)
      dgts = new DataGridTableStyle(); 
      dgts.MappingName = "People"; 

We bind a MouseUp event to the Grid, and get the current row selected.  When a MouseUp event is fired, CustomRow will pass the DataSet, DataGrid and currentRowIndex to FormTop.cs.

  public void dataGrid_MouseUp(object sender, 
     System.Windows.Forms.MouseEventArgs e)
    int rowIndex = dgts.DataGrid.CurrentRowIndex;
    if (CustomRow != null)
    CustomRow(this, new CustomRowEventArgs(

In FormTop.cs we include the following code.  By use of a delegate, the customHander_CustomRow function will be called when the mouseUp event is triggered in FormBottom.cs.  See the mouseUp event above.

  FormBottom.CustomRow += new DelegateExample.CustomRowHandler(

The customHandler that does all the work in FormTop.cs.

    private void customHandler_CustomRow(object sender,
      DelegateExample.CustomRowEventArgs e)
      DataSet  dSet = e.DSet;
      DataGrid grid = e.Grid;
      int row = e.Row;
      textBox2.Text = grid[e.Row,0].ToString();
      textBox3.Text = grid[e.Row,1].ToString();
      textBox4.Text = grid[e.Row,2].ToString();


That's it. I have kept the source code very simple so you should easily be able to see what is going on. The project files and source code for the article are provided above.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Brent Thompson
Web Developer
Canada Canada
No Biography provided

Comments and Discussions

GeneralMy vote of 5 Pinmembermanoj kumar choubey2-Mar-12 0:53 
GeneralSome problems PineditorMarc Clifton19-Feb-04 10:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140921.1 | Last Updated 18 Feb 2004
Article Copyright 2004 by Brent Thompson
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid