DataView / DataRow

  • Thread starter Thread starter Hein Albrecht
  • Start date Start date
H

Hein Albrecht

'ello

How can I get the DataRow of the DataGrid's SelectedItem for the bound
DataView's DataTable?

Thanx
Hein
 
Hein,

DataRow dr =
((DataRowView)BindingContext[MyDataGrid.DataSource].Current).Row;
 
Hello,

First, obtain the corresponding CurrencyManager instance like this:

CurrencyManager cm =
(CurrencyManager)theForm.BindingContext[theGrid.DataSource,
theGrid.DataMember];

next, obtain the data view:

DataView theView = (DataView)cm.List;

and finally, get the row:

DataRow row = theView[cm.Current].Row;
 
Hein,
Thanx for the replies, but I seemed to forget to mention one thing. My
application uses C# webforms, so I can't use
System.Windows.Forms.BindingContext.

I can think of two possible ways.

First, a DataGrid:

<ASP:DATAGRID id="myDataGrid" runat="server"
onitemcommand="myDataGrid_ItemCommand">
<COLUMNS>
<ASP:BUTTONCOLUMN HeaderText="Select Row"
ButtontType="PushButton"
Text="Select"
CommandName="SelectRow">
</ASP:BUTTONCOLUMN>
<ASP:BOUNDCOLUMN HeaderText="MyID" datafield="MyID">
</ASP:BOUNDCOLUMN>
</COLUMNS>
</ASP:DATAGRID>

Then in your code behind file either:

1. Store the DataSet for the Grid in Session State (depending on its size)
and:

protected void myDataGrid_ItemCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
TableCell myID = e.Item.Cells[1];
string key = myID.Text;
DataRow[] dr =
((DataSet)Session["MyDataSet"]).Tables[0].Select("MyID=" + key);
}

or

(2) go back to the database with the id and get the single record:

protected void myDataGrid_ItemCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
TableCell myID = e.Item.Cells[1];

int id = int.Parse(myID.Text);

if(((Button)e.CommandSource).CommandName == "SelectRow")
{
DataRow dr = // fetch the record from the database using the id
}
}
 
Back
Top