【问题标题】:Data Binding a DropDownList in a GridView在 GridView 中绑定 DropDownList 的数据
【发布时间】:2018-05-30 20:02:45
【问题描述】:

我知道这个主题已经讨论了很多,但我找不到解决我的问题的方法。

我已尝试使用 SqlDataSource 使用以下指南创建字段绑定,但我遵循每一步,然后最后字段绑定选项显示为灰色。 https://msdn.microsoft.com/en-us/library/ms178294.aspx

所以我开始尝试将 DropDownList 编码到 GridView 中,但我目前没有在 DDL 中生成任何数据。不知道接下来要添加什么。

下面是我的 GridView 代码,我试图让 ddlCategory 显示我的类别表中的所有类别。

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
    DataKeyNames="FileID" DataSourceID="filenameTableDataSource"
    EnableModelValidation="True" Style="text-align: center">
    <Columns>
        <asp:CommandField ShowEditButton="True" />
        <asp:BoundField DataField="FileID" HeaderText="FileID" InsertVisible="False" ReadOnly="True" SortExpression="FileID" />
        <asp:BoundField DataField="Filename" HeaderText="Filename" SortExpression="Filename" />
        <asp:TemplateField HeaderText="Category" SortExpression="Category">
            <ItemTemplate>
                <asp:Label ID="Category" runat="server" Visible="false" />
                <asp:DropDownList ID="ddlCategory" runat="server">
                </asp:DropDownList>
            </ItemTemplate>
        </asp:TemplateField>
        <%-- <asp:BoundField DataField="Category" HeaderText="Category" SortExpression="Category" />--%>
        <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
        <asp:CheckBoxField DataField="IsPublished" HeaderText="IsPublished" SortExpression="IsPublished" />
        <asp:CheckBoxField DataField="IsArchived" HeaderText="IsArchived" SortExpression="IsArchived" />
    </Columns>
</asp:GridView>

这是我用来获取 GridView 数据的 SQL 语句:

SELECT Files.FileID, Files.CategoryID, Files.Filename, Files.Description,
       Files.IsPublished, Files.IsArchived, Categories.Description AS Category 
FROM Files INNER JOIN Categories ON Files.CategoryID = Categories.CategoryID

提前致谢!

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    你可以使用 OnRowDataBound 事件:

    protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {  
        DropDownList ddlCategory= (e.Row.FindControl("ddlCategory") as DropDownList);
        ddlCategory.DataSource = GetData("SELECT DISTINCT CategoryName,CategoryId FROM Categories");
        ddlCategory.DataTextField = "CategoryName";
        ddlCategory.DataValueField = "CategoryId";
        ddlCategory.DataBind();
    }
    }
    

    【讨论】:

      【解决方案2】:

      我也遇到过类似的问题,您需要按照这些思路使用一些东西 private void BindDropDownList()

              DataView dv = mgr.GetItemSeriesMaster().DefaultView; //how to filter data
              dv.RowFilter = ProductQueryFilter;
              Dropdownlist1.DataSource = dv;
      
              Dropdownlist1.DataTextField = "Description"; // the items to be displayed in the list items
              Dropdownlist1.DataValueField = "Id"; // the id of the items displayed
              Dropdownlist1.DataBind();
      

      【讨论】:

        【解决方案3】:

        我的建议如下: 创建一个从数据库返回您的数据集的方法,如下所示:

        private DataSet GetData(string query)
        {
        string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        SqlCommand cmd = new SqlCommand(query);
        using (SqlConnection con = new SqlConnection(conString))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                using (DataSet ds = new DataSet())
                {
                    sda.Fill(ds);
                    return ds;
                }
            }
        }
        }
        

        将您的 GridView 绑定到您的数据源,例如:

        protected void Page_Load(object sender, EventArgs e)
        {
        if (!IsPostBack)
        {
            GridView1.DataSource = GetData("SELECT ContactName, Country FROM Customers");
            GridView1.DataBind();
        }
        }
        

        gridview的GridView1_RowDataBound事件如下:

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //Find the DropDownList in the Row.
            DropDownList ddlCategory= (e.Row.FindControl("ddlCategory") as DropDownList);
            ddlCategory.DataSource = GetData("SELECT DISTINCT Country FROM Category");
            ddlCategory.DataTextField = "CategoryName";
            ddlCategory.DataValueField = "CategoryId";
            ddlCategory.DataBind();
        
        
        
        
        }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-09-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多