简体   繁体   中英

How to get values from multiple DropDownLists in a GridView on button_click?

I have a GridView on my asp.net webform with all the columns and rows created in C# code from behind. Except the first column, every cell in each column is a DropDownList with a DataSource .

Here is my GridView :

 <asp:GridView ID="gv_Rota" runat="server" AutoGenerateColumns="false" OnRowDataBound="gv_Rota_RowDataBound">
                     <HeaderStyle BackColor="#6a3d98" ForeColor="White" Height="20" />
                     <RowStyle HorizontalAlign="Center" Height="20px" Width="100px" />
                     <AlternatingRowStyle Height="20px" />
                 </asp:GridView>

And the c# creation of the columns and cells:

 private void BindGrid(int Amount)
    {
        gv_Rota.DataSource = null;
        gv_Rota.Columns.Clear();

        BoundField bfield = new BoundField();
        bfield.HeaderText = "Days";
        bfield.DataField = "Days";
        gv_Rota.Columns.Add(bfield);

        for (int i = 0; i < Amount; i++)
        {
            int week = i + 1;
            string sWeek = "Week " + week.ToString();

            TemplateField tfield = new TemplateField();
            tfield.HeaderText = sWeek;
            gv_Rota.Columns.Add(tfield);
        }

        DataTable dt = new DataTable();
        dt.Columns.Add(new DataColumn("Days", typeof(string)));
        dt.Rows.Add("M");
        dt.Rows.Add("T");
        dt.Rows.Add("W");
        dt.Rows.Add("T");
        dt.Rows.Add("F");
        dt.Rows.Add("S");
        dt.Rows.Add("S");
        gv_Rota.DataSource = dt;
        gv_Rota.DataBind();
    }

And then on the RowDataBound trigger of my GridView , I add the DropDownList control to every cell:

protected void gv_Rota_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        for (int i = 1; i <= ColumnCount; i++)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ddlShift = new DropDownList();
                ddlShift.ID = "ddlShift";
                ddlShift.DataSource = DCListOfShifts;
                ddlShift.DataValueField = "SHIFT_ID";
                ddlShift.DataTextField = "SHIFT_NAME";
                ddlShift.DataBind();
                ddlShift.Items.Insert(0, new ListItem("Shift..."));
                ddlShift.CssClass = "ddl_rotamanager";
                e.Row.Cells[i].Controls.Add(ddlShift);
            }
        }  
    }

What is displayed for example on my page:

在此处输入图片说明

So my question and problem is that I now want to save each column (except the first) to a database, as a "scheduled week", and have no idea how to get the values of selected items in all or any of the DropDownLists to then pass that back to the database. Baring in mind, this is NOT done on selectedindexchanged . I will select items from all the DropDownLists and then press a button to submit the values. Please could someone give me some highlight on how this could be done? Thanks.

EDIT 1: Response explains how I could get every value of a DropDownList in a row or by individual cell. However, now I need to know more specifically how I can get values of specific cells in the columns rather than the rows. Is it possible to get further highlight on this?

Use Gridview.Rows property

protected void MyButton_Click(object sender, EventArgs e)
{
    foreach(GridViewRow row in gv_Rota.Rows)
    {
       //find this control in this row
       DropDownList dd = row.FindControl("MyDropdown") as DropDownList;

       //OR
       //find this control in a specific cell
       DropDownList day = row.Cells[0].FindControl("MyDropdown") as DropDownList;
       DropDownList Week1 = row.Cells[1].FindControl("ddShift") as DropDownList;

       //this is just a pseudo-code to determine which dropdown was selected per row. 
       //You can improve on this
       if(dd1.SelectedValue != string.Empty)
         thisDay.SelectedWeek = dd1.SelectedValue;
       else if(dd2.SelectedValue != string.Empty)
         thisDay.SelectedWeek = dd2.SelectedValue;
       ....
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM