Change DataGrid EditControl based On Column Value

Discussion in 'ASP .Net' started by =?Utf-8?B?QmlsbHk=?=, Feb 28, 2006.

  1. 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
    ------------------------------------------------------------
    =?Utf-8?B?QmlsbHk=?=, Feb 28, 2006
    #1
    1. Advertising

  2. 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
    > ------------------------------------------------------------
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=, Feb 28, 2006
    #2
    1. Advertising

  3. 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
    > > ------------------------------------------------------------
    =?Utf-8?B?QmlsbHk=?=, Mar 1, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Amber
    Replies:
    0
    Views:
    804
    Amber
    Oct 7, 2003
  2. Mike P
    Replies:
    1
    Views:
    366
    DavidG
    Jan 13, 2006
  3. vishnu
    Replies:
    1
    Views:
    1,191
    Patrick.O.Ige
    Mar 25, 2006
  4. Amber

    Conditionally Change a DataGrid Column Value...

    Amber, Oct 7, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    177
    Amber
    Oct 7, 2003
  5. TeknoShock
    Replies:
    1
    Views:
    148
Loading...

Share This Page