从后面的代码在GridView中绑定DropDown

我正在尝试从后面的代码绑定到网格视图中的下拉列表。最初,我正在为我的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;
    }
安德烈

基本思想非常简单。订阅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();
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

从后面的代码中过滤TextBox的KeyPress事件上的GridView

在后面的代码中设置Xamarin.Forms绑定CommandParameter

在itemTemplate中查找标签并从后面的代码中绑定数据

WPF如何在后面的代码中设置Gridview绑定中的复选框

WPF从后面的代码创建样式绑定

在gridview中所做的更改不会显示在后面的代码中

WPF如何在后面的代码中检索绑定的属性

将XAML“标签”绑定到Xamarin.Forms中“ public const”后面的代码?

从后面的代码更新GridView。无法保留数据

从文件后面的代码中添加/删除GridView ASP.NET的标题列中的类

使用后面的代码在gridview中设置单选按钮列表值

如何在后面的代码中绑定工具提示?

wpf xaml绑定到在后面的代码中创建的对象

从后面的代码为GridView内部的标签分配值

Asp.net-将Textbox字段更改为gridview中位于标签后面的代码

如何从后面的代码中获取在gridview中使用jquery更改的标签文本

绑定控件在后面的代码中被检查

在GridView中的DropDown绑定

从后面的代码向Gridview添加属性

如何从后面的代码在GridView中显示gridLines

绑定中的WPF StringFormat在后面的代码中不起作用

如果两个涉及的对象都是在后面的代码中创建的,则元素绑定

绑定到后面的代码

C# WPF 在后面的代码中绑定到变量和属性

C# WPF 如何在后面的代码中获取绑定值

Xamarin:从后面的代码中的事件处理程序获取 Entry.Text 的绑定属性

如何在 WPF 后面的代码中绑定属性

如何在后面的代码中的标签中使用绑定

如何在 Xamarin 后面的代码中绑定 StackLayout 数据