tags:

views:

3219

answers:

1

Hi,

Firing a RowDataBound event, my question is how do I pull out the DataKey for GridviewRowEventArgs e?

I have a table of Publications and surprisingly enough the key is PublicationID, however I do not have a boundfield to publication id but have it specified as a datakeyname. I can get the information using:

int pID =Convert.ToInt32(DataBinder.Eval(e.Row.DataItem,"PublicationID"));

But was wondering if there is a more elegant way of getting the information?!

Also, if I investigate e, i can see the underlying table (publication) which also has a child table owner, does anybody know how I could, from the point in the app i.e. inside the RowDataBound event pull information out of the child table?

This is all .Net 3.0, C#.

Thanks, R.

+3  A: 

((GridView)sender).DataKeys[e.Row.RowIndex].Value.ToString();

JBrooks
Thanks, that's pretty interesting. I have a second question, is there a way of retrieving the underlying datasource in a similar way?My GridView has an underlying dataset with two relationships in it, I was wondering if I can pick up one of those relationship and read through it to get at some data, rather than query the database or a global dataset again? It seems logically possible.I tried: DataSet x = (DataSet)((GridView)sender).DataSource; but got Null.
flavour404
I tried your tutorial on check boxes but kept getting an error on: <%# Bind("Include") %>, added column to my table, set the default value to 0, but this Bind component threw an error, something about converting from a number, bit weird.
flavour404
To the first comment: You get the key info you need from e.Row.DataItem and use that to retrieve data from the DataSet - but the DataSet needs to be in scope. So you might have to retrieve the DataSet, set a private variable of the page to that DataSet and then bind your GridView. And then within the RowDataBound event the DataSet would be visible.
JBrooks
For the check boxes did you have:<ItemTemplate> <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Bind("Include") %>' /></ItemTemplate>and if you defined the Include column in sql you couldn't have:select 0 as Include....you have to haveselect cast(0 as bit) as Include
JBrooks