【问题标题】:Bind DropDown in GridView from code behind从后面的代码在 GridView 中绑定 DropDown
【发布时间】:2015-12-02 15:25:56
【问题描述】:

我正在尝试从后面的代码绑定网格视图中的下拉列表。最初,我为我的 gridview 生成一个空行,我想在生成空行时绑定下拉列表。请帮忙.. aspx 代码

<form id="form1" runat="server">
     <asp:DropDownList ID="ddlSelectWeek" runat="server" OnSelectedIndexChanged="ddlSelectWeek_SelectedIndexChanged" Width="200px" AutoPostBack="true">
                <asp:ListItem Text="2015-11-15" Value="2015-11-15"></asp:ListItem>
                <asp:ListItem Text="2015-11-22" Value="2015-11-22"></asp:ListItem>
                <asp:ListItem Text="2015-11-29" Value="2015-11-29"></asp:ListItem>
            </asp:DropDownList>

    <div>
          <asp:GridView ID="grvStudentDetails" runat="server"
            ShowFooter="True" AutoGenerateColumns="False"
            CellPadding="4" ForeColor="#333333"
            GridLines="None" OnRowDeleting="grvStudentDetails_RowDeleting">
            <Columns>

              <asp:BoundField DataField="RowNumber" HeaderText="Row ID" />


                    <asp:TemplateField HeaderText="Task Name">
                <ItemTemplate>

                  <asp:DropDownList ID="drpQualification" runat="server">
                    <asp:ListItem Value="G">test1</asp:ListItem>
                    <asp:ListItem Value="P">test2</asp:ListItem>
                  </asp:DropDownList>

                </ItemTemplate>
                <FooterStyle HorizontalAlign="Right" />
                <FooterTemplate>
                  <asp:Button ID="ButtonAdd" runat="server"
                    Text="Add New Row" OnClick="ButtonAdd_Click" />
                </FooterTemplate>
              </asp:TemplateField>


              <asp:TemplateField HeaderText="Day1">
                <ItemTemplate>
                  <asp:TextBox ID="txtDay1" runat="server"></asp:TextBox>
                </ItemTemplate>
              </asp:TemplateField>

              <asp:TemplateField HeaderText="Day2">
                <ItemTemplate>
                  <asp:TextBox ID="txtDay2" runat="server"></asp:TextBox>
                </ItemTemplate>
              </asp:TemplateField>


              <asp:CommandField ShowDeleteButton="True" />
            </Columns>
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <RowStyle BackColor="#EFF3FB" />
            <EditRowStyle BackColor="#2461BF" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="White" />
          </asp:GridView>
        </div>
      </form>

后面的代码

 protected void ddlSelectWeek_SelectedIndexChanged(object sender, EventArgs e)
    {
      DateTime theDate;
      DateTime.TryParseExact(this.ddlSelectWeek.SelectedValue, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out theDate);
      FirstGridViewRow(theDate);
    }


    private void FirstGridViewRow(DateTime theDate)
    {
      DataTable dt = new DataTable();
      DataRow dr = null;
      dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
      dt.Columns.Add(new DataColumn("Col1", typeof(string)));
      dt.Columns.Add(new DataColumn("Col2", typeof(string)));
      dt.Columns.Add(new DataColumn("Col3", typeof(string)));     
      dr = dt.NewRow();
      for (var i = 1; i < 6; i++)
      {
        dr["RowNumber"] = i;
        dr["Col1"] = string.Empty;
        dr["Col2"] = string.Empty;
        dr["Col3"] = string.Empty;      
        dt.Rows.Add(dr);
        dr = dt.NewRow();
      }
      ViewState["CurrentTable"] = dt;
      grvStudentDetails.DataSource = dt;
      grvStudentDetails.DataBind();

      for (int i = 2; i < 9; i++)
      {
        grvStudentDetails.HeaderRow.Cells[i].Text = theDate.AddDays(i - 2).ToString("ddd") + " " + theDate.AddDays(i - 2).ToShortDateString();
      }
      TextBox txn = (TextBox)grvStudentDetails.Rows[0].Cells[1].FindControl("txtDay1");
      txn.Focus();
      Button btnAdd = (Button)grvStudentDetails.FooterRow.Cells[2].FindControl("ButtonAdd");
      Page.Form.DefaultFocus = btnAdd.ClientID;
    }

【问题讨论】:

  • 是否要将所有下拉列表绑定到同一个数据源,独立于网格视图行?
  • 是的,我只需要为同一个数据源绑定每一行中的所有下拉列表。我在数据库中有任务名称列表,我需要将其拉出并为每一行绑定它。谢谢

标签: c# asp.net gridview


【解决方案1】:

基本思想非常简单。订阅 GridView 的 RowDataBound 事件:

<asp:GridView ID="grvStudentDetails" runat="server"
              ...
              OnRowDataBound="grvStudentDetails_RowDataBound"

并在其中找到在该行中创建的下拉控件并绑定它:

protected void grvStudentDetails_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        DropDownList ddl = (DropDownList)e.Row.FindControl("drpQualification");
        ddl.DataSource = your_data_source_here;
        ddl.DataBind();
    }
}

【讨论】:

    猜你喜欢
    • 2017-06-14
    • 2011-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-29
    相关资源
    最近更新 更多