G
Guest
Change DataGrid EditControl On Data Value
Hi,
I have a datagrid, and on editing, I want to change the control in the third
colunm based on the value of the first column.
The value in the first column can only be either "Text" or "Image", which is
selcted in a dropdown list.
If the value in the first colum is "Text" I want third column to edited with
a TextBox, but if the value in first column is "Image" I want it to be edited
with a DropDownList.
What I have been trying to do is Dynamically load the edit control into the
third column in the load event for the dropdown in the first column (code
below), but it throws a "Object reference not set to an instance of an
object" on the "phDtls.Controls.Add" line.
May well be going about this the wrong way.
----------------------------------------------------------------------------
Sub ddlCType_OnLoad(ByVal sender As Object, ByVal e As System.EventArgs)
If _editing Then
Dim ddl As DropDownList = CType(sender, DropDownList)
Dim ds As DataSet = getCatDS(_catid)
Dim dt As DataTable = ds.Tables(0)
Dim dr As DataRow
dr = dt.Rows(_editIndex)
If dr("CType") = "T" Then
ddl.Items(0).Selected = True
ElseIf dr("CType") = "I" Then
ddl.Items(1).Selected = True
Dim ddlTmp As New DropDownList
ddlTmp.ID = "ddlImg"
ddlTmp.Items.Add("image1")
ddlTmp.Items.Add("image2")
phDtls.Controls.Add(ddlTmp)
End If
End If
End Sub
------------------------------------------------------------
Hi,
I have a datagrid, and on editing, I want to change the control in the third
colunm based on the value of the first column.
The value in the first column can only be either "Text" or "Image", which is
selcted in a dropdown list.
If the value in the first colum is "Text" I want third column to edited with
a TextBox, but if the value in first column is "Image" I want it to be edited
with a DropDownList.
What I have been trying to do is Dynamically load the edit control into the
third column in the load event for the dropdown in the first column (code
below), but it throws a "Object reference not set to an instance of an
object" on the "phDtls.Controls.Add" line.
May well be going about this the wrong way.
----------------------------------------------------------------------------
Sub ddlCType_OnLoad(ByVal sender As Object, ByVal e As System.EventArgs)
If _editing Then
Dim ddl As DropDownList = CType(sender, DropDownList)
Dim ds As DataSet = getCatDS(_catid)
Dim dt As DataTable = ds.Tables(0)
Dim dr As DataRow
dr = dt.Rows(_editIndex)
If dr("CType") = "T" Then
ddl.Items(0).Selected = True
ElseIf dr("CType") = "I" Then
ddl.Items(1).Selected = True
Dim ddlTmp As New DropDownList
ddlTmp.ID = "ddlImg"
ddlTmp.Items.Add("image1")
ddlTmp.Items.Add("image2")
phDtls.Controls.Add(ddlTmp)
End If
End If
End Sub
------------------------------------------------------------