Gridview中的Dropdroplist绑定 [英] Dropdroplist binding in Gridview

查看:60
本文介绍了Gridview中的Dropdroplist绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我在网格edititemtemplate中的dropdownlist上添加了内容,当我将.cs文件中的数据与事件rowdataboud绑定在一起时,出现错误

Hi,
i added on dropdownlist in grid edititemtemplate, when i am binding the data in .cs file with the event rowdataboud then i got the error

System.NullReferenceException: Object reference not set to an instance of an object.



这是我的示例CS代码



here is my sample cs code

protected void gvTestType_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DropDownList ddl = (DropDownList)e.Row.FindControl("ddlStatus");
               DataSet ds = new DataSet();
               StatusBll sBl = new StatusBll();
                ds = sBl.SelectStatusDetails();
                ddl.DataTextField = "Description";//the source of your dropdown
                ddl.DataValueField = "StatusId";
                ddl.DataSource = ds.Tables["Status"];
                ddl.DataBind();
            }
        }

推荐答案

可能是您的
sBl.SelectStatusDetails()

试图从空值中获取变量.

tries to get a variable from something that is null.

Or

ds.Tables["Status"]

不存在...


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

当您尝试使用属性或调用对象的方法为null时,会发生此错误.更多详细信息:此处 [
Object reference not set to an instance of an object

This error happens when you try to use a property or call a method of an object that is null. More details: here[^]

A simple use of Visual studio DEBUGGER can tell you the object because of which it is happening. Just look at the stack trace and put a debugger on that line. Check the objects of that line and see if any one is null and you are trying to use that objects property. Handle the same.

Based on the code snippet shared, potential places:
DropDownList ddl = (DropDownList)e.Row.FindControl("ddlStatus"); //ddlStatus was not found and explicit cast threw an error


ds = sBl.SelectStatusDetails(); // ds returned is null or empty, assuming no error in method called!


ddl.DataSource = ds.Tables["Status"]; // there is no table named 'Status' in the dataset ds


检查,查找并修复它.


Check, find and fix it.


这篇关于Gridview中的Dropdroplist绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆