Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Change DataGrid EditControl based On Column Value

Reply
Thread Tools

Change DataGrid EditControl based On Column Value

 
 
=?Utf-8?B?QmlsbHk=?=
Guest
Posts: n/a
 
      02-28-2006
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
------------------------------------------------------------
 
Reply With Quote
 
 
 
 
=?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=
Guest
Posts: n/a
 
      02-28-2006
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
> ------------------------------------------------------------

 
Reply With Quote
 
 
 
 
=?Utf-8?B?QmlsbHk=?=
Guest
Posts: n/a
 
      03-01-2006
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
> > ------------------------------------------------------------

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bind a dropdown in a column in a datagrid based on the dropdown value selected in another column of the datagrid. vishnu ASP .Net 1 03-25-2006 01:24 PM
populate a dg column based upon the value populated to another row in the datagrid Mike P ASP .Net 1 01-13-2006 11:43 AM
how do i access a dropdownlists selected value in a datagrid edititemtemplate column from the selectedindexchanged event of another dropdownlist in a datagrid edititemtemplate column Dave M ASP .Net Datagrid Control 0 12-14-2004 11:53 AM
Conditionally Change a DataGrid Column Value... Amber ASP .Net 0 10-07-2003 03:14 AM
how to Add different controls(textBox,DropDownList or some ) in the same column,based upon the value in the previous column (Say second Colum which contain dropdown with some values) ? Balaji ASP .Net Datagrid Control 0 07-07-2003 08:32 AM



Advertisments