Change DataGrid EditControl based On Column Value

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
------------------------------------------------------------
 
G

Guest

You can handle either the ItemCreated or ItemDataBound event, e.g.

Private Sub datagrid1_ItemDataBound(ByVal sender As Object, ByVal e As _
System.Web.UI.WebControls.DataGridItemEventArgs) Handles
datagrid1.ItemDataBound

If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = _
ListItemType.AlternatingItem Then
Dim drv As DataRowView = CType(e.Item.DataItem, DataRowView)
'clear the controls in the 3rd column
e.Item.Cells(2).Controls.Clear()
If drv("Field1").Equals("Text") Then
Dim txt As New TextBox
'add a textbox in the third column
e.Item.Cells(2).Controls.Add(txt)
Else
Dim ddl As New DropDownList
'you can add code to populate the dropdownlist here
'add a textbox in the third column
e.Item.Cells(2).Controls.Add(ddl)
End If
End If

End Sub
 
G

Guest

Exactly what i was looking for, thanks Phillip.

Phillip Williams said:
You can handle either the ItemCreated or ItemDataBound event, e.g.

Private Sub datagrid1_ItemDataBound(ByVal sender As Object, ByVal e As _
System.Web.UI.WebControls.DataGridItemEventArgs) Handles
datagrid1.ItemDataBound

If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = _
ListItemType.AlternatingItem Then
Dim drv As DataRowView = CType(e.Item.DataItem, DataRowView)
'clear the controls in the 3rd column
e.Item.Cells(2).Controls.Clear()
If drv("Field1").Equals("Text") Then
Dim txt As New TextBox
'add a textbox in the third column
e.Item.Cells(2).Controls.Add(txt)
Else
Dim ddl As New DropDownList
'you can add code to populate the dropdownlist here
'add a textbox in the third column
e.Item.Cells(2).Controls.Add(ddl)
End If
End If

End Sub


--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top