2009-01-16 89 views
1

我正在使用Gridview来显示一些数据。在GridView的EditItemTemplate中,我使用DropDownList作为GridView的一列。 gridview的DataSource是一个表“UserEntries”。 Dropdown的Datasource是另一个表格“TypeEntries”。 TypeEntries的列是 - Guid和TypeName。 Guid是下拉的DataValueField,TypeName是DataTextField。我在UserEntries表中存储DataValueFiels的下拉列表。C#中CType的等价物将转换为Gridview的EditItemTemplate中的下拉列表

现在,当用户点击GridView的Edit按钮时,如何使用“TypeEntries”表填充下拉列表?我正在使用

Dropdownlist tempddl = new Dropdownlist(); 
    tempddl = (Dropdownlist)gvUserData.FindControl("ddlTypeListInGrid"); 
    tempddl.DataSource = _section.GetTypeEntries(); 
    tempddl.DataBind(); 

但它不起作用。任何人都可以告诉我任何其他方式来完成这项任务吗? 在此先感谢。

+0

什么是“不工作”?它抛出一些异常或什么? – TcKs 2009-01-16 11:37:48

+0

您首先创建一个Dropdownlist,然后将其设置为另一个控件,您应该执行其中一个控件。 – Recep 2009-01-16 11:43:27

+0

它引发异常“对象引用未设置为对象的实例”。 – 2009-01-16 12:03:42

回答

0

C#区分大小写,您应该使用DropDownList来代替。

0

您是否在寻找Convert.ChangeType,我将不得不看到更多的代码来解决您的问题。

0

我正在上传整个函数的代码。

protected void gvUserData_OnRowEditing(object sender, GridViewEditEventArgs e) 
{ 
    gvUserData.EditIndex = e.NewEditIndex; 

    gvUserData.DataSource = _section.GetUserEntries(); 
    gvUserData.DataBind(); 

    DropDownList tempddl = new DropDownList();  //I am not sure whether this is correct or not..   
    tempddl = (DropDownList)gvUserData.FindControl("ddlTypeListInGrid"); 
    tempddl.DataSource = _section.GetTypeEntries(); 
    tempddl.DataBind();   

} 
1

如果您将“对象引用未设置为对象的实例”。在例外情况下,这意味着未找到“ddlTypeListInGrid”控件。所以你不能将NULL(NOTHING)转换为目标类型。

您可以在wrogn页面的生命周期中合理地执行此代码。 如果容器真的包含ID为“ddlTypeListInGrid”的控件,请尝试在稍后的事件(Load,LoadComplete等)之一中进行检查或检查。

0

作为一个侧面说明(不是你的问题相关的,只为您的信息)如下因素线:

DropDownList tempddl = new DropDownList();  

可能是:

DropDownList tempddl; 

你不需要创建一个新的实例DropDownList,因为在下一行,你的tryng找到一个实例nammed“ddlTypeListInGrid”。然后,如Tcks所说,如果ddlTypeListInGrid控件不存在,那么您可能会得到一个NullReferenceException。

相关问题