Get value from hidden boundfield? ASP.NET

15,406

Solution 1

Add a data key for the column you need to get:

<asp:GridView ID="GridView1" runat="server" DataKeyNames="hidden" ...>

Once you've added the datakey, you can access its value with the row index:

var rowIndex = 0;
var hiddenValue = (string)GridView1.DataKeys[rowIndex]["hidden"];

Solution 2

Another option is to keep the BoundField (or any other type of column) visible in the markup (remove Visible=False), and use the RowDataBound event to hide the columns:

protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
    e.Row.Cells[COLUMN_USERID].Visible = false;
}

Solution 3

you can use this code. i tested it.

TextBox1.Text = GridView1.DataKeys[e.NewSelectedIndex].Value.ToString();

Share:
15,406
Wesley Lalieu
Author by

Wesley Lalieu

Updated on July 13, 2022

Comments

  • Wesley Lalieu
    Wesley Lalieu almost 2 years

    I know the question I'm going to ask is already asked for by other people, but those answers are no solution for my problem.

    I have a gridview containing 2 BoundFields, 2 ButtonFields and a checkbox field (which is a TemplateField).

    I also have a datatable, filled with data from the database.

    In the aspx code I create my gridview with the fields set the last BoundField Visible = false.

    In my codebehind, I add the colums and bind the datasource to my datatable.

    But when I try to read the data from the hidden boundfield, that field is empty. The problem why I can't use the solutions mentioned with similar questions, is because the people assume the data gets filled in one by one, and not by binding a datatable to the datasource of the gridview.

    So my question is: Is their a way to get the data from the hidden boundfield and also retain the possibility to add the data by binding the datatable to the datasource of the gridview? And if so, is it posible to get the value from that field?

    p.s. I'm using asp.net/c# in visual studio 2010

    ASPX:

    <asp:GridView ID="gvSelect" runat="server" AutoGenerateColumns="False" BorderStyle="None" onrowcommand="gvTestSelect_RowCommand">
        <Columns>
            <asp:TemplateField>
                <HeaderTemplate>
                    <asp:CheckBox runat="server" ID="cbHeader" OnPreRender="cbHeader_PreRender" />
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:CheckBox runat="server" ID="cbItems" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="field" HeaderText="Veld" SortExpression="field" />
            <asp:ButtonField DataTextField="up" HeaderText="Omhoog" SortExpression="up" CommandName="up" Text="&uarr;" />
            <asp:ButtonField DataTextField="down" HeaderText="Omlaag" SortExpression="down" CommandName="down" Text="&darr;" />
            <asp:BoundField DataField="hidden" SortExpression="hidden" />
        </Columns>
    </asp:GridView>
    

    Code Behind (where I fill the gridview):

    //create array list and fill it with all columns
    Dictionary<string, string> dict = FillLists.getColumnsByTable(loader, ddlInfoTableI.SelectedItem.Value.ToString());
    
    //loop trough dictionary
    foreach (var val in dict)
    {
        //create new dtSelect datarow
        DataRow dr = dtSelect.NewRow();
    
        //set row values for column values
        dr["select"] = false;
        dr["field"] = val.Value.ToString();
        dr["up"] = new ButtonField { CommandName = "up", Text = loader.LoadResourceString(1024), HeaderText = "&uarr;", ButtonType = ButtonType.Button, Visible = true };
        dr["down"] = new ButtonField { CommandName = "down", Text = loader.LoadResourceString(1025), HeaderText = "&darr;", ButtonType = ButtonType.Button, Visible = true };
        dr["hidden"] = val.Key.ToString();
    
        //add the datarow
        dtSelect.Rows.Add(dr);
    
        //set datatable session to datatable
        Session["dtSelect"] = dtSelect;
    
        //set datasource of the gridview to datatable
        gvSelect.DataSource = dtSelect;
    
        //bind data to gridview
        gvSelect.DataBind();
    }
    

    So now I need to get the data from the gridview (escpecialy from the hidden boundfield), because they can edit the gridview except the hidden boundfield, so that is the only way to know which original row it was.

  • Wesley Lalieu
    Wesley Lalieu about 12 years
    Thank you very much, this helped me out!
  • ArtK
    ArtK almost 10 years
    This one is truly the cleanest way I found so far.
  • U.Y.
    U.Y. over 6 years
    All the answers look good, but this method works best if you have already written code and don't want to do other changes