How can i get the value of a field that is set to Visible=false? as follows:
<asp:BoundField DataField="ItemID" HeaderText="Line Item ID" Visible="false"/>
but when i try to get it with
int temID = Convert.ToInt32(row.Cells[0].Text);
It cant find it and throws an exception, but if I make it Visible="true" it works.
How can I retrieve the value if visible = false?
In the definition of your GridView, add
<asp:GridView .... DataKeyNames="ItemID" ...>
You also need to use OnRowDataBound, not OnDataBound
<asp:GridView .... DataKeyNames="ItemID" ... OnRowDataBound="GridView_RowDataBound">
Then in your code behind, something like this
protected void GridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
int ItemId = Int32.Parse(YourGridView.DataKeys[e.Row.RowIndex].Values[0].ToString());
}
}
I did not test this code before posting. But this is the general idea of what you need to do. It may or may not work as is.
Another way without all those code is something like that:
<style type="text/css">
.Hide
{
display: none;
}
</style>
in the page or in your css file. And set this class to your boundField:
<asp:BoundField DataField="Id" ItemStyle-CssClass="Hide" HeaderStyle-CssClass="Hide" />
Hope this help.
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