Access GridView column and return values

Kris Ellison 20 Reputation points

I have a Gridview displaying my data in ASP.NET and C#. How can I access a column and return values greater than 10 and display them in a label below the GridView? I've been looking but couldn't find out how to access them. Thanks

A set of technologies in the .NET Framework for building web applications and XML web services.
3,312 questions
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,691 questions
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,376 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Lan Huang-MSFT 26,361 Reputation points Microsoft Vendor

    Hi @Kris Ellison,

    You'd better provide your code so that we can help you more specifically.

    Below are two methods of getting columns that I wrote according to your needs.

     protected void Button1_Click(object sender, EventArgs e)
                List<int> numbers = new List<int>();
                List<int> number = new List<int>();
                for (int i = 0; i < gvCustomers.Rows.Count; i++)
                    //get the second column
                    int a = Convert.ToInt32(gvCustomers.Rows[i].Cells[1].Text);
                    if (a > 10)
                        Label1.Text = string.Join(",", numbers);
                    //get the third column
                    TextBox tb = (TextBox)gvCustomers.Rows[i].FindControl("TextBox1");
                    int b = Convert.ToInt32(tb.Text.ToString());
                    if (b > 10)
                        Label2.Text = string.Join(",", number);
            protected void Page_Load(object sender, EventArgs e)
                if (!this.IsPostBack)
                    DataTable dt = new DataTable();
                    dt.Columns.AddRange(new DataColumn[]
                        new DataColumn("CustomerId"),
                        new DataColumn("Name"),
                        new DataColumn("Country")
                    dt.Rows.Add(1, 1, 11);
                    dt.Rows.Add(2, 22, 2);
                    dt.Rows.Add(3, 12, 0);
                    dt.Rows.Add(4, 3, 13);
                    gvCustomers.DataSource = dt;
                <asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false" >
            <asp:BoundField DataField="CustomerId" HeaderText="Id" />
            <asp:BoundField DataField="Name" HeaderText="Name" />
            <asp:TemplateField HeaderText="Country">
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("Country") %>'></asp:TextBox>
                <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
                <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
                <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>


    Best regards,
    Lan Huang

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.