How to use hidden Boundfield in GridView in ASP.NET?

I am binding a GridView using a DataSource in asp.net, and I would like to have some hidden BoundFields so that I can access these values ​​in my RowDataBound function. However, when I set Visible = "False" to these BoundFields, the values ​​are not set and always remain empty in the RowDataBound function.

Is there any way to do this? I saw some suggestions for setting the style on BoundField for hiding, but that didn't work for me either. Ideally, I don’t even need a column created in gridview, I just want these values ​​to be hidden, so I can access them. Thank!

+3
source share
3 answers

DataItem GridViewRowEventArgs, RowDataBound?

protected void MyGridView_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
        DataRowView rowView = (DataRowView)e.Row.DataItem;
        // do something with rowView["myHiddenField"]
    }
}

MSDN .

+1

, ? , , :

<asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Eval("ColumnName")%>'/>
+2

For this, I suggest you use a DataKey.

Pls link to this link http://www.codeproject.com/KB/grid/Data_presentation.aspx

eg:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1"

DataKeyNames = "emp_id, code">

int EmployeeID = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Values[0]);
  int DepartementID = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Values[1]);

in DataRow binding:

((GridView)sender).DataKeys[e.Row.RowIndex].Value.ToString();

Geetha

+2
source

Source: https://habr.com/ru/post/1791590/