Click here to Skip to main content
15,915,599 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
This is my grid view

ASP.NET
<asp:GridView ID="grdDepartment" runat="server" BackColor="White"
               BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3"
               ForeColor="Black" GridLines="Vertical" Width="293px"
               AutoGenerateColumns="False" AutoGenerateSelectButton="True"
               onselectedindexchanged="grdDepartment_SelectedIndexChanged">
               <AlternatingRowStyle BackColor="#CCCCCC" />
               <FooterStyle BackColor="#CCCCCC" />
               <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
               <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
               <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
               <SortedAscendingCellStyle BackColor="#F1F1F1" />
               <SortedAscendingHeaderStyle BackColor="#808080" />
               <SortedDescendingCellStyle BackColor="#CAC9C9" />
               <SortedDescendingHeaderStyle BackColor="#383838" />
               <Columns>
                   <asp:BoundField HeaderText ="ID" DataField ="ID" Visible="false" />
                   <asp:BoundField  HeaderText="Department" DataField="Department Name"/>
               </Columns>
           </asp:GridView>


and i am unable to access value of ID column by below code
C#
protected void grdDepartment_SelectedIndexChanged(object sender, EventArgs e)
    {
        lblID.Text = grdDepartment.SelectedRow.Cells[1].Text;
        txtDepartment.Text = grdDepartment.SelectedRow.Cells[2].Text;
    }
Posted

try this,

ASP.NET
<asp:BoundField HeaderText ="ID" DataField ="ID"  />

remove visible="false" and set visible=false from code behind.

C#
lblID.Text = grdDepartment.SelectedRow.Cells[0].Text;
txtDepartment.Text = grdDepartment.SelectedRow.Cells[1].Text;
 grdDepartment.Columns.Item(0).Visible =false;



thanks
 
Share this answer
 
v3
You need to use DataItem . Follow:
C#
DataRowView rowView = (DataRowView)e.Row.DataItem;
int id = Convert.ToInt32(rowView["ID"]);
//use id as your need..

There are few more ways to get it.Read the CP article:
How to get Hidden Column Value in GridView[^]
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900