By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,256 Members | 1,463 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,256 IT Pros & Developers. It's quick & easy.

Change DataGrid EditControl based On Column Value

P: n/a
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
------------------------------------------------------------
Feb 28 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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
"Billy" wrote:
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
------------------------------------------------------------

Feb 28 '06 #2

P: n/a
Exactly what i was looking for, thanks Phillip.

"Phillip Williams" wrote:
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
"Billy" wrote:
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
------------------------------------------------------------

Mar 1 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.