从数据集中填充 gridview 下拉列表时出现问题

发布于 2024-11-09 04:27:14 字数 1447 浏览 0 评论 0原文

我正在使用下面的代码:

protected void grdViewCInfo_RowDataBound(object sender, GridViewRowEventArgs e)
{
    MySqlConnection objMycon1 = new MySqlConnection(strProvider);
    objMycon1.Open();
    MySqlCommand cmd1 = new MySqlCommand("select * from tblcountrynames",objMycon1); 
    MySqlDataAdapter da = new MySqlDataAdapter(cmd1);
    DataSet ds = new DataSet(); 
    da.Fill(ds);
    // DropDownList Control Object Created to bind the data dynamically with each 
    // nested DropDownlist control placed inside the template column of the GridView 
    // Control.
    DropDownList drdList;

    // foreach loop is used to loop through each row of GridView Control.

    foreach (GridViewRow grdRow in grdViewCInfo.Rows)
    {    
        // Nested DropDownList Control reference is passed to the DrdList object.
        // This will allow you access the properties of dropdownlist placed 
        // inside the GridView Template column. 
        drdList = (DropDownList)(grdViewCInfo.Rows[grdRow.RowIndex].FindControl("ddlCountry" ));  
        // DataBinding of nested DropDownList Control for each row of GridView Control.  
        drdList.DataSource = ds;   
        drdList.DataValueField = "ID"; 
        drdList.DataTextField = "Name";
        drdList.DataBind();
    }
}

它给出的错误如下:

未将对象引用设置为对象的实例。

drdList.DataSource = ds;

我该如何解决这个问题???

I'm using the code below:

protected void grdViewCInfo_RowDataBound(object sender, GridViewRowEventArgs e)
{
    MySqlConnection objMycon1 = new MySqlConnection(strProvider);
    objMycon1.Open();
    MySqlCommand cmd1 = new MySqlCommand("select * from tblcountrynames",objMycon1); 
    MySqlDataAdapter da = new MySqlDataAdapter(cmd1);
    DataSet ds = new DataSet(); 
    da.Fill(ds);
    // DropDownList Control Object Created to bind the data dynamically with each 
    // nested DropDownlist control placed inside the template column of the GridView 
    // Control.
    DropDownList drdList;

    // foreach loop is used to loop through each row of GridView Control.

    foreach (GridViewRow grdRow in grdViewCInfo.Rows)
    {    
        // Nested DropDownList Control reference is passed to the DrdList object.
        // This will allow you access the properties of dropdownlist placed 
        // inside the GridView Template column. 
        drdList = (DropDownList)(grdViewCInfo.Rows[grdRow.RowIndex].FindControl("ddlCountry" ));  
        // DataBinding of nested DropDownList Control for each row of GridView Control.  
        drdList.DataSource = ds;   
        drdList.DataValueField = "ID"; 
        drdList.DataTextField = "Name";
        drdList.DataBind();
    }
}

It gives an error as:

Object reference not set to an instance of an object.

At the line drdList.DataSource = ds;

How do I fix this???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

德意的啸 2024-11-16 04:27:14

尝试在以下代码行中指定 COLUMN:

drdList = (DropDownList)( grdViewCInfo.Rows[ grdRow.RowIndex ][ColumnIndex].FindControl( "ddlCountry" ));

另一种选择是循环遍历另一个 foreach 中的列

根据您的评论提供更多信息:

看起来您是 ASP.NET 新手,这就是我推荐的原因以下内容:

使用 asp:TemplateColumn 并将 asp:DropDownList 放入 EditTemplate 中。将 DropDown 连接到 SqlDataSource(或您想要使用的任何其他数据源)。

绑定将为您处理。

如果没有看到您的 ASP.NET 代码并了解有关您的需求的更多信息,我无法进一步详细说明。

Try specifying the COLUMN in the following line of code:

drdList = (DropDownList)( grdViewCInfo.Rows[ grdRow.RowIndex ][ColumnIndex].FindControl( "ddlCountry" ));

Another option is to loop through columns in another foreach

More info based on your comment:

Looks like you're new to ASP.NET, that's why I recommend the following:

Use asp:TemplateColumn and put the asp:DropDownList in the EditTemplate. Hook the DropDown up to SqlDataSource (or whatever else datasource you want to use).

The binding will be handled for you.

I can't elaborate any further w/o seeing your ASP.NET code and knowing more about your requirements.

吃兔兔 2024-11-16 04:27:14

试试这个

DropDownList ddl;
        int i = 0;
        foreach (GridViewRow grdrow in grdmenu.Rows)
        {
     ddl = (DropDownList)grdmenu.Rows[grdrow.RowIndex].FindControl("ddloffer");
     ddl.SelectedIndex = Convert.ToInt16(ds.Tables[0].Rows[i]["Offer"]);
     ddl = (DropDownList)grdmenu.Rows[grdrow.RowIndex].FindControl("ddloffers");
     ddl.SelectedIndex = Convert.ToInt16(ds.Tables[0].Rows[i]["OfferType"]);

            i++;  
            ddl.DataBind();
        }

Try This

DropDownList ddl;
        int i = 0;
        foreach (GridViewRow grdrow in grdmenu.Rows)
        {
     ddl = (DropDownList)grdmenu.Rows[grdrow.RowIndex].FindControl("ddloffer");
     ddl.SelectedIndex = Convert.ToInt16(ds.Tables[0].Rows[i]["Offer"]);
     ddl = (DropDownList)grdmenu.Rows[grdrow.RowIndex].FindControl("ddloffers");
     ddl.SelectedIndex = Convert.ToInt16(ds.Tables[0].Rows[i]["OfferType"]);

            i++;  
            ddl.DataBind();
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文