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="↑" />
<asp:ButtonField DataTextField="down" HeaderText="Omlaag" SortExpression="down" CommandName="down" Text="↓" />
<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 = "↑", ButtonType = ButtonType.Button, Visible = true };
dr["down"] = new ButtonField { CommandName = "down", Text = loader.LoadResourceString(1025), HeaderText = "↓", 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.
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"];