Access GridView column and return values

Kris Ellison 25 Reputation points
2023-04-18T11:12:16.7533333+00:00

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

Developer technologies Visual Studio Other
Developer technologies ASP.NET Other
Developer technologies C#
{count} votes

1 answer

Sort by: Most helpful
  1. Lan Huang-MSFT 30,186 Reputation points Microsoft External Staff
    2023-04-19T08:52:08.6366667+00:00

    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)
                    {
                        numbers.Add(a);
                     
                        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)
                    {
                        number.Add(b);
    
                        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;
                    gvCustomers.DataBind();
                }
            }
    
     <div>
                <asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false" >
        <Columns>
            <asp:BoundField DataField="CustomerId" HeaderText="Id" />
            <asp:BoundField DataField="Name" HeaderText="Name" />
            <asp:TemplateField HeaderText="Country">
                <ItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("Country") %>'></asp:TextBox>
                  
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
                <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>
            </div>
    

    2

    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.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.