net2003中datagrid镶嵌DropDownList数据绑定的问题
在datagrid1的第一列内嵌一个DropDownList,但点击编辑时数据绑定出错,提示“未将对象引用设置到对象的实例。
源错误:
行 85: _drpl.DataSource = _Etable.DefaultView”
Sub ebt(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
lbllevel.Text = " "
lbllevel.Text = DataGrid1.Items(e.Item.ItemIndex).Cells(0).Text
Dim _drpl As DropDownList = DataGrid1.Items(e.Item.ItemIndex).Cells(0).FindControl( "drplevel ")
sql = "select student_level from student_level group by student_level "
_Etable.Clear()
_Etable = _Econn._table( "sw1 ", sql)
If _Etable.Rows.Count > 0 Then
_drpl.DataSource = _Etable.DefaultView
_drpl.DataTextField = "student_level "
_drpl.DataBind()
End If
_drpl.Items.FindByText(lbllevel.Text.Trim).Selected = True
DataGrid1.EditItemIndex = e.Item.ItemIndex
_Bind_grid()
End Sub
请帮忙
[解决办法]
你这个嵌套DropDownList并给数据的事情在点编辑的时候做不对,应该在DataGrid的ItemDataBound事件中完成绑定DropDownList的操作
还有,我猜测你的DropDownList是在EditItemTemplate里面
[解决办法]
_drpl.DataSource = _Etable.DefaultView
_drpl.DataTextField = "student_level "
调试看看_Etable.DefaultView是否为NULL,
还有DropDownList绑定的时候DataValueField没付值?这样可以啊?
[解决办法]
DataGridCommand 事件是什么时候触发的?
你的 DropDownList 放在那个模板列中? ItemTemplate AlternatingTemplate 还是EditItem?
[解决办法]
Private Sub gridlst_ItemDataBound(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _
Handles gridlst.ItemDataBound
If (e.Item.ItemType = ListItemType.Footer)
Dim DDL As DropDownList =ctype(e.Item.findcontrol( "lst2 ") ,dropdownlist)
DDL.items.clear()
dim selectsql as string
selectsql= "select cpmc from test "
dim con as new sqlconnection(connectionstring)
dim cmd as new sqlcommand(selectsql,con)
dim reader as sqldatareader
try
con.open()
reader=cmd.executereader()
do while reader.read()
dim newitem as new listitem()
newitem.text=reader( "cpmc ")
DDL.items.add(newitem)
loop
reader.close()
finally
if (not con is nothing) then
con.close()
end if
end try
End If
end sub
[解决办法]
小弟请给一点分
http://www.microsoft.com/china/msdn/archives/library/dnaspp/html/creatingcustomcolumns.asp