Get the Cell Value from GridView In C#

Background

I am sharing this because I see in the forums a lot of questions based on how to get the value from the gridview object, and I know the reason behind that. As a beginner it's very tough and challenging to work with controls like a gridview in ASP.NET. Even I faced difficulties at the starting point of my career. So I am writing this mainly for beginners.

And mainly here I am not going to explain about gridview and other events in it, because there are a lot of good articles available for that. Here I am only going to show the different ways we can use code to take the value from gridview.

First, we have to very be clear in our mind that there are mainly two way of taking the cell value from the gridview:

  • Bound field
  • Template field

Use the bound field columns

Sample aspx code,

  1. <asp:BoundField DataField="activity_item_Code" HeaderText="Code" />  
  2. <asp:BoundField DataField="activity_item_name" HeaderText="name"  />
C# Code
  1. //any gridview event  
  2. protected void GVactivityItems_SelectedIndexChanged(object sender, EventArgs e)  
  3. {  
  4.     // step 1 get the selected row from the gridview,since SelectedIndexChanged is a event of gridview you will surely get the SelectedRow(property)  
  5.     GridViewRow gvr = GVactivityItems.SelectedRow;  
  6.     //step 2-- from the row access the cell(Column) you want  
  7.     txtcode.Text = gvr.Cells[0].Text;  
  8.     //2nd way  
  9.     //combination of step1 and step 2  
  10.     txtname.Text = GVactivityItems.SelectedRow.Cells[1].Text;  
  11. }  
When you use the Template field column,
  1. <asp:TemplateField HeaderText="description">  
  2.    <EditItemTemplate>  
  3.   
  4.    <asp:TextBox ID="txt_object_Value_description" runat="server" Text='<%# Bind("object_Value_description") %>'> </asp:TextBox>  
  5. </EditItemTemplate>  
  6.   
  7.    <ItemTemplate>  
  8.    <asp:Label ID="lbl_object_Value_description" runat="server" Text='<%# Bind("object_Value_description") %>'></asp:Label>  
  9. </ItemTemplate>  
  10.   
  11.    <FooterTemplate>  
  12. <asp:TextBox ID="txt_object_Value_description_ins" runat="server" ></asp:TextBox>  
  13. </FooterTemplate>  
  14.   
  15. </asp:TemplateField>  
  16. C# code:  
  17. protected void GV_ObjVal_RowUpdating(object sender, GridViewUpdateEventArgs e)  
  18. {  
  19.   
  20.    string obj_Value_Desc;   
  21.   
  22.    GridViewRow row = GV_ObjVal.Rows[e.RowIndex];  
  23.    obj_Value_Desc = ((TextBox)row.FindControl("txt_object_Value_description")).Text.ToString().Trim();  
  24. }  
Important Note

The main difference is, here we are using the Find Control() method, that is, first we need to find the control inside the gridview and then only can we access the data from it.

Another Way of Taking -- Just for Sharing

This code is for taking the value apart from the gridview events; that is, any common event using sender object . But ensure that the control/link is inside the gridview, only then can we cast sender as grid view. This code is a bit complex but just add it knowing that we may refer to it in the future.
  1. protected void lnk_Click(object sender, EventArgs e)  
  2. {  
  3.     try  
  4.     {  
  5.         GridView gv = sender as GridView;  
  6.         //int tenderid = Convert.ToInt16(HdnfldNewTenderID.Value);  
  7.         GridViewRow gvr = (GridViewRow)(sender as LinkButton)  
  8.             .NamingContainer;  
  9.         int tenderid = Convert.ToInt16(GVCreateNewTenderWorkspace.DataKeys[gvr.RowIndex].Value);  
  10.         string tendername = ((Label) gvr.FindControl("GvTenderBaseTenderName"))  
  11.             .Text;  
  12.     }  
  13. }  
Conclusion

Here in gridview, in the beginning the major part is to make the decision of putting either a bound field or a template field and the answer is simple: When we want to place any control inside the gridview then we can go for template field, otherwise bound field will be simpler.

I hope the above information wa a little bit helpful for you, kindly let me know your valuable feedback or thoughts.