Retrieving Invisible Columns data from Gridview RowCommand And Databound events


From this article, you will know retrieving invisible columns data from Gridview RowCommand And Databound events. You can find the code for Retrieving Invisible Columns data from Gridview RowCommand And Databound events.

Retrieving Invisible Column data of Gridview from RowCommand And Databound events


This code shows how to retrieve invisible column data from Gridview

Design:


<form id="form1" runat="server">
<div>
<asp:GridView ID="GrdRetrieveControlsData" runat="server" AutoGenerateColumns="false"
DataKeyNames="EmployeeCode"
onrowdatabound="GrdRetrieveControlsData_RowDataBound"
onrowcommand="GrdRetrieveControlsData_RowCommand">
<Columns>
<asp:BoundField DataField="EmployeeCode" HeaderText="Emp Code" Visible="false"/>
<asp:BoundField DataField="FirstName" HeaderText="First Name" /
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:ImageButton ID="btnEdit" ImageUrl="~/images/Edit.gif" CommandName="Edit"
CommandArgument='<%#Container.DataItemIndex %>' runat="server"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>


Code:


string sDsn = ConfigurationSettings.AppSettings["Con"].ToString();

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
// binding gridview
BindGridView();
}
}

private void BindGridView()
{
DataSet ds = SqlHelper.ExecuteDataset(sDsn, "PS_GetDetails");
GrdRetrieveControlsData.DataSource = ds.Tables[0];
GrdRetrieveControlsData.DataBind();
}

protected void GrdRetrieveControlsData_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex != -1 && e.Row.DataItem != null)
{
int iIndex = e.Row.RowIndex;
string sEmpId = GrdRetrieveControlsData.DataKeys[iIndex].Values["EmployeeCode"].ToString().Trim();
}
}

protected void GrdRetrieveControlsData_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("Edit"))
{
int iIndex = Convert.ToInt32(e.CommandArgument);
GridViewRow gRow = GrdRetrieveControlsData.Rows[iIndex];

string sEmpId1 = GrdRetrieveControlsData.DataKeys[iIndex].Values["EmployeeCode"].ToString().Trim();
}
}


Comments

Guest Author: Carlos17 Nov 2011

Not Work



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: