DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Process Mining Key Elements
  • What Are SOC and SIEM? How Are They Connected?
  • 8 Must-Have Project Reports You Can Use Today
  • This Is How You Give Good Feedback at Work

Trending

  • Revolutionizing Financial Monitoring: Building a Team Dashboard With OpenObserve
  • Transforming AI-Driven Data Analytics with DeepSeek: A New Era of Intelligent Insights
  • Unmasking Entity-Based Data Masking: Best Practices 2025
  • AI-Based Threat Detection in Cloud Security
  1. DZone
  2. Culture and Methodologies
  3. Career Development
  4. Get the Cell Value From GridView in C#

Get the Cell Value From GridView in C#

A beginner tutorial for C# and ASP.NET users on working with controls like GridView.

By 
Karthik Elumalai user avatar
Karthik Elumalai
·
Jul. 26, 16 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
69.8K Views

Join the DZone community and get the full member experience.

Join For Free

Background

I am sharing this because I see a lot of questions on the forums on how to get the value from the GridView object, and I know how. 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.

Here, I am not going to explain about GridView and other events in it, because there are a lot of good articles about that. 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 ways of taking the cell value from the GridView:

  • Bound field
  • Template field

Using the Bound Field Columns

Sample aspx Code

<asp:BoundField DataField="activity_item_Code" HeaderText="Code" />  
<asp:BoundField DataField="activity_item_name" HeaderText="name"  />

C# Code

//any gridview event  
protected void GVactivityItems_SelectedIndexChanged(object sender, EventArgs e)  
{  
    // step 1 get the selected row from the gridview,since SelectedIndexChanged is a event of gridview you will surely get the SelectedRow(property)  
    GridViewRow gvr = GVactivityItems.SelectedRow;  
    //step 2-- from the row access the cell(Column) you want  
    txtcode.Text = gvr.Cells[0].Text;  
    //2nd way  
    //combination of step1 and step 2  
    txtname.Text = GVactivityItems.SelectedRow.Cells[1].Text;  
}

When you use the Template field column, you'll get this:

<asp:TemplateField HeaderText="description">  
   <EditItemTemplate>  

   <asp:TextBox ID="txt_object_Value_description" runat="server" Text='<%# Bind("object_Value_description") %>'> </asp:TextBox>  
</EditItemTemplate>  

   <ItemTemplate>  
   <asp:Label ID="lbl_object_Value_description" runat="server" Text='<%# Bind("object_Value_description") %>'></asp:Label>  
</ItemTemplate>  

   <FooterTemplate>  
<asp:TextBox ID="txt_object_Value_description_ins" runat="server" ></asp:TextBox>  
</FooterTemplate>  

</asp:TemplateField>  
C# code:  
protected void GV_ObjVal_RowUpdating(object sender, GridViewUpdateEventArgs e)  
{  

   string obj_Value_Desc;   

   GridViewRow row = GV_ObjVal.Rows[e.RowIndex];  
   obj_Value_Desc = ((TextBox)row.FindControl("txt_object_Value_description")).Text.ToString().Trim();  
}

Important Note

The main difference is that here we are using the Find Control() method, that is, first we need to find the control inside the GridView and only then 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 a sender object. But ensure that the control/link is inside the GridView, only then can we cast sender as GridView. This code is a bit complex but just add it knowing that we may refer to it in the future.

protected void lnk_Click(object sender, EventArgs e)  
{  
    try  
    {  
        GridView gv = sender as GridView;  
        //int tenderid = Convert.ToInt16(HdnfldNewTenderID.Value);  
        GridViewRow gvr = (GridViewRow)(sender as LinkButton)  
            .NamingContainer;  
        int tenderid = Convert.ToInt16(GVCreateNewTenderWorkspace.DataKeys[gvr.RowIndex].Value);  
        string tendername = ((Label) gvr.FindControl("GvTenderBaseTenderName"))  
            .Text;  
    }  
}  

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 was a little bit helpful for you, kindly let me know your valuable feedback or thoughts.

Template Event Column (database) Object (computer science) career Data (computing) Clear (Unix) IT

Opinions expressed by DZone contributors are their own.

Related

  • Process Mining Key Elements
  • What Are SOC and SIEM? How Are They Connected?
  • 8 Must-Have Project Reports You Can Use Today
  • This Is How You Give Good Feedback at Work

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!