Click here to Skip to main content
15,885,876 members
Articles / Programming Languages / C#
Article

DataGrids, Forms and Delegates.

Rate me:
Please Sign up or sign in to vote.
4.41/5 (17 votes)
17 Feb 20042 min read 63.2K   3K   42   2
This article describes using a Delegate to pass data from a DataGrid in one form, to set of controls in another form.

Introduction

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. '

Background

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.

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

and the event handler, which is defined as static.

C#
public static event CustomRowHandler CustomRow;

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

C#
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.

C#
private void FormBottom_Load(
  object sender, System.EventArgs e)
{
  BindFamousPeople();
  dgts = new DataGridTableStyle();
  dgts.MappingName = "People";
  dataGrid1.TableStyles.Add(dgts);
}

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.

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

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.

C#
FormBottom.CustomRow += new DelegateExample.CustomRowHandler(
  customHandler_CustomRow);

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

C#
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();

}

Conclusion

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.

License

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


Written By
Web Developer
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey2-Mar-12 0:53
professionalManoj Kumar Choubey2-Mar-12 0:53 
GeneralSome problems Pin
Marc Clifton19-Feb-04 10:07
mvaMarc Clifton19-Feb-04 10:07 

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

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