how i count dropdown value in gridview to textboxt RRS feed

  • Question

  • User-1693850256 posted

    i have a gridview with data source, then i add one item template wich have a dropdown list item 1 to 5. to score the factor on my table

    i want to count all the dropdown.selectedValue. Plis Help

    im using C# web

    Thursday, February 9, 2017 3:26 PM

All replies

  • User-1509636757 posted

    You just need to loop thru rows of GridView, find the Dropdown with FinControl method and get the selected value. Similar code:

    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <form id="form1" runat="server">
            <asp:GridView runat="server" ID="GridView1">
                            <asp:DropDownList runat="server" ID="ddlFactor">
                                <asp:ListItem Text="1" Value="1" />
                                <asp:ListItem Text="2" Value="2" />
                                <asp:ListItem Text="3" Value="3" />
                                <asp:ListItem Text="4" Value="4" />
                                <asp:ListItem Text="5" Value="5" />
            <asp:TextBox runat="server" ID="TextBox1" />


    protected void Page_Load(object sender, EventArgs e)
        Int16 sumFactor = 0;
        foreach (GridViewRow row in GridView1.Rows)
            DropDownList ddlFactor = (row.FindControl("ddlFactor") as DropDownList);
            if (null != ddlFactor.SelectedItem)
                sumFactor += Convert.ToInt16(ddlFactor.SelectedValue);
        TextBox1.Text = sumFactor.ToString();

    hope that helps./.

    Thursday, February 9, 2017 5:00 PM
  • User-271186128 posted

    Hi samuel46,

    Welcome to asp.net forum

    As KaushaL said, you need to loop through the dropdownlist and use FindControl method to find the dropdownlist, then calculate the score.

    You could refer to the following code: In the page load event, you need to bind the GridView when page first time load.

           protected void Page_Load(object sender, EventArgs e)
                if (!IsPostBack)
                    DataTable dt = new DataTable();
                    dt.Columns.AddRange(new DataColumn[5] {
                     new DataColumn("UserID", typeof(int)),
                     new DataColumn("UserName(c)", typeof(string)),
                     new DataColumn("UserDetails", typeof(string)),
                     new DataColumn("Email(c)", typeof(string)),
                     new DataColumn("Email", typeof(string)),
                    dt.Rows.Add(1, "SS", "Emp", "abc@gmail.com", "ABCd@gmail.com");
                    dt.Rows.Add(2, "AA", "Emp", "bcd@gmail.com", "BCD@gmail.com");
                    dt.Rows.Add(3, "BB", "Emp", "bcd@gmail.com", "BCD@gmail.com");
                    GridView1.DataSource = dt;
                    GridView1.DataBind(); //Bind Gridview

    protected void Button1_Click(object sender, EventArgs e) { int count = 0; foreach (GridViewRow row in GridView1.Rows) { DropDownList dropdown = (row.FindControl("dropdown") as DropDownList);
    if(dropdown !=null)
    { if (dropdown.SelectedItem != null) count += Convert.ToInt32(dropdown.SelectedValue);
    } } Response.Write(count); }

    Best regards,

    Friday, February 10, 2017 5:45 AM