Reputation: 10997
I have a GridView
defined as follows:
<asp:GridView ID="myGridView" AutoGenerateColumns="false" runat="server"
OnLoad="myGridView_Load" OnRowCommand="myGridView_Command" OnRowEditing="myGridView_RowEditing" OnRowDeleting="myGridView_RowDeleting" DataKeyNames="ID" >
<Columns>
<asp:BoundField DataField="ID" Visible="false" />
<asp:BoundField DataField="BirthDate" Visible="false" />
<asp:BoundField DataField="FirstName" HeaderText="First Name" />
<asp:BoundField DataField="LastName" HeaderText="Last Name" />
<asp:TemplateField HeaderText="Other">
<ItemTemplate>
<asp:LinkButton ID="editLB" runat="server" Text="edit" CommandName="Edit" />
<asp:LinkButton ID="deleteLB" runat="server" Text="delete" CommandName="Delete" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
When a user clicks the edit button, I need to get the value of the BirthDate
column. To attempt this, I have tried the following:
protected void myGridView_RowEditing(object sender, GridViewEditEventArgs e)
{
GridViewRow row = gvUsers.Rows[e.NewEditIndex];
DateTime birthDate = (DateTime)(row.Cells[1].Text);
// Does not work
}
I know it has something to do with the fact that the column is not visible. The column must be hidden. But I need to get that value— how can I do this?
Upvotes: 5
Views: 31631
Reputation: 8421
The problem is that when the Visibility property of the BoundField
is set to false the column isn't rendered to the client. A work around would be to use a HiddenField
within a TemplateField
instead.
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="HiddenField1" runat="server"
Value='<%# Eval("BirthDate") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:HiddenField ID="HiddenField1" runat="server"
Value='<%# Eval("BirthDate") %>' />
</EditItemTemplate>
</asp:TemplateField>
protected void GridView_RowEditing(object sender, GridViewEditEventArgs e)
{
GridViewRow row = GridView.Rows[e.NewEditIndex];
HiddenField hidden = (HiddenField)row.Cells[0].FindControl("HiddenField1");
DateTime birthDate = Convert.ToDateTime(hidden.Value);
}
EDIT
The above method still renders the column in the table, so you end up with an empty column. It works but not the best solution, here's a way the hide the BirthDate field but still get its value in the RowEditing
event handler. Just keep in mind that the BirthDate is still rendered to the client, just not displayed.
<style type="text/css">
.hide
{
display:none;
}
</style>
<asp:BoundField DataField="BirthDate">
<ItemStyle CssClass="hide"/>
</asp:BoundField>
protected void GridView_RowEditing(object sender, GridViewEditEventArgs e)
{
GridViewRow row = GridView1.Rows[e.NewEditIndex];
DateTime birthDate = Convert.ToDateTime(row.Cells[1].Text);
}
Upvotes: 9
Reputation: 51
To hide the content as well as the Header in the Grid use the below code:
and access the column as usual. GridView.SelectedRow.Cells[index].Text;
<style type="text/css">
.hide
{
display:none;
}
<asp:BoundField DataField="MailRoomID" HeaderText="Mailroom ID" ItemStyle-HorizontalAlign="Center" ShowHeader="false" ><ItemStyle CssClass="hide" /><HeaderStyle CssClass="hide"/></asp:BoundField>
Upvotes: 5
Reputation: 20066
Check out the following article that shows how to access GridView invisible columns:
http://www.highoncoding.com/Articles/178_Access_GridView_Invisible_Columns.aspx
The idea is to basically use a TemplateField column instead of a BoundColumn.
Upvotes: 1
Reputation: 8574
I would use the hiddenfield suggestion that Phaedrus suggested, and then in your code behind; use something like this:
GridViewRow row = gvUsers.Rows[e.NewEditIndex];
var hfBirthDate = (HiddenField)row.FindControl("hfBirthDate");
DateTime birthDate = DateTime.Parse(hfBirthDate.Value);
Upvotes: 0
Reputation: 539
I have done something like this (adapting to your example):
string[] keyList = new string[1];
keyList[0] = "BirthDate";
myGridView.DataKeyNames = keyList;
I then bound the GridView with a DataTable that has a column named "BirthDate". The data under this column would be stored under the DataKeyName specified above.
To get the desired value, I would do something like this:
protected void myGridView_RowEditing(object sender, GridViewEditEventArgs e)
{
DataKeyArray keyList = myGridView.DataKeys as DataKeyArray;
if (keyList != null)
DateTime birthDate = keyList[e.NewEditIndex];
}
I realize that this doesn't involve hidden fields, however.
Upvotes: 2
Reputation: 8474
this is one way to get data from underlying data
protected void myGridView_RowEditing(object sender, GridViewEditEventArgs e)
{
GridViewRow row = gvUsers.Rows[e.NewEditIndex];
DateTime birthDate = Convert.ToDateTime(DataBinder.Eval(row.DataItem, "BirthDate"));
}
Upvotes: 0