Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to get the text of an invisible GridViewRow in the codebehind?

I have a GridView BoundField defined like this:

<asp:BoundField DataField="Id" />

In the codebehind, I can get the instance of a row and read the text like this:

Row.Cells(0).Text

However, when I make the BoundField invisible like this...

<asp:BoundField DataField="Id" Visible="false" />

...Row.Cells(0).Text returns an empty string.

The only solution I've found is to create an item and edit item template, put a hidden field in both, and then get the hidden field using .FindControl(). I don't really like that idea, though.

like image 391
oscilatingcretin Avatar asked Mar 23 '23 11:03

oscilatingcretin


1 Answers

Use DataKeyNames property of gridview.

<asp:GridView runat="server" ID="MyGridView" DataKeyNames="Id">
</asp:GridView>

And access this ID value as:

var data = MyGridView.DataKeys[RowIndex].Values[KeyIndex]

so, in your case it can be as below for say 2nd row

var data = MyGridView.DataKeys[1].Values[0]
like image 93
R.C Avatar answered Apr 06 '23 00:04

R.C