C# 如何通过单击按钮从GridView中的多个下拉列表中获取值?

C# 如何通过单击按钮从GridView中的多个下拉列表中获取值?,c#,asp.net,gridview,drop-down-menu,webforms,C#,Asp.net,Gridview,Drop Down Menu,Webforms,我在asp.NETWebForm上有一个GridView,其中包含从后面用C代码创建的所有列和行。除第一列外,每列中的每个单元格都是一个带有数据源的DropDownList 以下是我的GridView: 例如,我的页面上显示的内容: 因此,我的问题是,我现在想将除第一列之外的每一列按计划周保存到数据库中,但不知道如何获取所有或任何DropDownList中选定项的值,然后将其传递回数据库。请记住,这不是对SelectedIndex进行更改。我将从所有下拉列表中选择项目,然后按下按钮提交值。请有人

我在asp.NETWebForm上有一个GridView,其中包含从后面用C代码创建的所有列和行。除第一列外,每列中的每个单元格都是一个带有数据源的DropDownList

以下是我的GridView:

例如,我的页面上显示的内容:

因此,我的问题是,我现在想将除第一列之外的每一列按计划周保存到数据库中,但不知道如何获取所有或任何DropDownList中选定项的值,然后将其传递回数据库。请记住,这不是对SelectedIndex进行更改。我将从所有下拉列表中选择项目,然后按下按钮提交值。请有人给我介绍一下如何做到这一点?谢谢

编辑1:Response解释了如何获取一行或单个单元格中DropDownList的每个值。但是,现在我需要更具体地了解如何在列而不是行中获取特定单元格的值。是否有可能进一步强调这一点

使用Gridview.Rows属性


谢谢你的回复。因此,根据我的理解,使用第一种方法,我可以得到每行中每个单元格的值,而第二种方法可以得到行中特定的单元格?但是,我特别需要一列而不是一行中的所有7个单元格,因为我需要能够获得一周中每一天的选定值。以天为行,以周为列。这有可能吗?如果有,怎么做?编辑的问题。我相信你可以改进这段代码来做到这一点。此循环应运行7倍的天数,然后对于每行,使用第二个示例“获取单元格以获取下拉列表”检查单元格1、2、3和4,并检查是否已选择。您的意思是每行只能选择一个单元格?用户可以选择多个吗?这有限制吗?我想我可以从这开始工作,并尝试让它工作。更多信息:我需要从每一行中选择所有内容,但需要确定单元格所在行的哪一列,因为很明显,我需要知道轮班时间是几周。将列视为周,行视为天。因此,要将事件保存到我的数据库中。我首先得到第一列中的第一个dropdownlist单元格,得到第1周-周一。这将是第二行。您可以使用一个计数器,将每天表示为1,2,3,…7。星期一星期二将是第2行。单元格[1]。FindControlddlShift
 <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>
 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();
    }
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);
            }
        }  
    }
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;
       ....
    }
}