如何在asp.net中使用syncfusion填充grip分组控件中的下拉列表

时间:2014-04-07 06:38:58

标签: c# asp.net syncfusion

我在syncfusion asp.net中有一个网格分组控件,第一列单元格类型为组合框。我需要填写pageload中的下拉列表。我写了下面的内容代码:

Sqlcommand cmd = new SqlCommand("Select item_Id, item_Name from productsnrwmtrls where item_Ctgry in('R','B')", con);
         sqldataadapter   da = new SqlDataAdapter(cmd);
           Datatable dtLocl = new DataTable();
            da.Fill(dtLocl);

        DropDownList ddlrwmtrl1 = (DropDownList)GridGroupingControl1.FindControl("ddlrwmtrl");

        ddlrwmtrl1.DataTextField = "item_Name";
        ddlrwmtrl1.DataValueField = "item_Id";
        ddlrwmtrl1.DataSource = dtLocl;
        ddlrwmtrl1.DataBind();

但是在此行ddlrwmtrl1.DataTextField = "item_Name";显示错误:对象引用未设置为对象的实例

1 个答案:

答案 0 :(得分:1)

您可以使用Rowdatabound填充Syncfusion GridGroupingControl中的下拉列表。 将下拉列表添加为ASPX文件中的项模板:

[**aspx**]

    <syncfusion:GridColumnDescriptor MappingName="City" HeaderText="City">
          <ItemTemplate>
               <asp:DropDownList ID="ddlCity" runat="server"></asp:DropDownList>
          </ItemTemplate>
    </syncfusion:GridColumnDescriptor>

RowDataBound事件

**[cs]**


  protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            GetData();
        this.GridGroupingControl1.RowDataBound += GridGroupingControl1_RowDataBound;
    }

    protected void GridGroupingControl1_RowDataBound(object sender,RowDataBoundEventArgs e)
    {
        if (e.Element.Kind == DisplayElementKind.Record)
        {
            for (int i = 0; i < e.Row.Cells.Count; i++)
            {
                if (((GridCell)e.Row.Cells[i]).ColumnDescriptor.Name == "City")
                {
                    myConnection = new SqlConnection(ConnectionString);
                    myConnection.Open();
                    DropDownList ddl = (DropDownList)e.Row.Cells[i].FindControl("ddlCity");
                    SqlCommand cmd = new SqlCommand("SELECT Distinct City FROM Employees", 
                                             myConnection);
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    DataSet ds = new DataSet();
                    da.Fill(ds);
                    myConnection.Close();
                    ddl.DataSource = ds;
                    ddl.DataTextField = "City";
                    ddl.DataValueField = "City";
                    ddl.DataBind();
                    ddl.Items.Insert(0, new ListItem("--Select--", "0"));
                }
            }
        }
    }

您将获得如下所示的网格

enter image description here