Grab data from Data Grid

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

This is in a WinForm.
How do I grab all the data in all the cells of the row the user selected OR
all the data in all the cells if the user selects one of the cells in the
row? What event should I put the code in?
 
Hi,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to get all the items in the
row that is selected in a WinForm DataGrid. If there is any
misunderstanding, please feel free to let me know.

We can use the CurrencyManager.Current to get the row selected. The return
value is a DataRowView object. We can enumerate the Item property of it to
get all the values. You can check the following links for more information.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemwindowsformscurrencymanagerclasstopic.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemdatadatarowviewclassitemtopic.asp

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Your response is to complex. I just want basic code to retrieve data from
the row the user has selected, and in which event the code should be.
 
Hi,

Here I wrote a code snippet. Make sure that when getting the
CurrencyManager, we have to use the same data source object and data
memeber as we set to the DataGrid.

private CurrencyManager cm;

private void Form1_Load(object sender, System.EventArgs e)
{
DataSet ds = new DataSet();
this.sqlDataAdapter1.Fill(ds);
this.dataGrid1.DataSource = ds.Tables[0];
this.cm = (CurrencyManager)this.BindingContext[ds.Tables[0]];
this.cm.PositionChanged +=new EventHandler(cm_PositionChanged);
}

private void cm_PositionChanged(object sender, EventArgs e)
{
DataRowView dr = (DataRowView)this.cm.Current;
//Do whatever with the DataRowView object here.
}

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Back
Top