Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gridview getting value of a BoundField set to Visible=False

Tags:

c#

asp.net

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?

like image 757
user710502 Avatar asked Aug 04 '11 17:08

user710502


2 Answers

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.

like image 91
TheGeekYouNeed Avatar answered Oct 17 '22 11:10

TheGeekYouNeed


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.

like image 22
Elek Guidolin Avatar answered Oct 17 '22 10:10

Elek Guidolin