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.
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With