DropDownList DataGrid

Discussion in 'ASP .Net' started by RN1, Mar 9, 2008.

  1. RN1

    RN1 Guest

    A DataGrid displays 3 columns from a database table - ID, Name &
    Subject. When the DataGrid is in the editable mode, I want the 3rd
    column Subject to a DropDownList so that users can change the subject.
    This is how I tried it:

    --------------------------------------------------------------------------------
    <script runat="server">
    Public dSet As DataSet
    Public strSQL As String
    Public sqlDapter As SqlDataAdapter
    Public sqlConn As New SqlConnection(".......")

    Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
    strSQL = "SELECT * FROM tblSS"
    Call LoadData(strSQL)

    If Not (Page.IsPostBack) Then
    dgSS.DataBind()
    End If
    End Sub

    Sub LoadData(ByVal SQLQuery As String)
    sqlDapter = New SqlDataAdapter(strSQL, sqlConn)

    dSet = New DataSet
    sqlDapter.Fill(dSet, "SS")

    dgSS.DataSource = dSet.Tables("SS").DefaultView
    End Sub

    Sub Edit_Command(ByVal obj As Object, ByVal ea As
    DataGridCommandEventArgs)
    If (ea.Item.ItemType = ListItemType.Item Or ea.Item.ItemType =
    ListItemType.AlternatingItem) Then
    Dim ddl As DropDownList

    ddl = CType(ea.Item.FindControl("ddlSubject"),
    DropDownList)

    strSQL = "SELECT DISTINCT(Subject) FROM tblSS"
    sqlDapter = New SqlDataAdapter(strSQL, sqlConn)

    dSet = New DataSet
    sqlDapter.Fill(dSet, "Subject")

    ddl.DataSource = dSet.Tables("Subject")
    ddl.DataBind()
    End If

    dgSS.EditItemIndex = ea.Item.ItemIndex

    strSQL = "SELECT * FROM tblSS"
    Call LoadData(strSQL)
    dgSS.DataBind()
    End Sub
    </script>

    <form runat="server">
    <asp:DataGrid ID="dgSS" OnEditCommand="Edit_Command" runat="server">
    <Columns>
    <asp:TemplateColumn HeaderText="#">
    <ItemTemplate>
    <asp:Label ID="lblID" Text='<%# Container.DataItem("ID") %>'
    runat="server"/>.
    </ItemTemplate>
    </asp:TemplateColumn>

    <asp:BoundColumn DataField="SName" HeaderText="NAME"/>

    <asp:TemplateColumn HeaderText="SUBJECT">
    <ItemTemplate>
    <asp:Label ID="lblSubject" Text='<%# Container.DataItem("Subject") %>'
    runat="server"/>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:DropDownList ID="ddlSubject" DataTextField="Subject"
    runat="server"/>
    </EditItemTemplate>
    </asp:TemplateColumn>
    <asp:EditCommandColumn CancelText="CANCEL" EditText="EDIT"
    HeaderText="EDIT" UpdateText="UPDATE"/>
    </Columns>
    </asp:DataGrid>
    </form>
    --------------------------------------------------------------------------------

    But when I click the EDIT link in the DataGrid to change the DataGrid
    into editable mode, the following error gets generated:

    Object reference not set to an instance of an object.

    pointing to the ddl.DataSource...... line in the above code.

    What am I doing wrong?
    RN1, Mar 9, 2008
    #1
    1. Advertising

  2. RN1

    Manish Guest

    Hi,

    You can try the following code to bind the DropDownList control when you
    edit the row.

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    'Put user code to initialize the page here
    Dim ds As DataSet = loaddata()
    Me.DataGrid1.DataSource = ds
    Me.DataGrid1.DataBind()
    End Sub

    Private Sub DataGrid1_EditCommand(ByVal source As Object, ByVal e As
    System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
    DataGrid1.EditCommand
    Me.DataGrid1.EditItemIndex = e.Item.ItemIndex
    Dim ds As DataSet = loaddata()
    Me.DataGrid1.DataSource = ds
    Me.DataGrid1.DataBind()
    End Sub
    Public Function loaddata() As DataSet
    Dim con As String = Me.OleDbConnection1.ConnectionString
    Dim da As OleDbDataAdapter = New OleDbDataAdapter("select
    CategoryID, CategoryName from categories", con)
    Dim ds As New DataSet
    da.Fill(ds)
    Return ds
    End Function

    Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As
    System.Web.UI.WebControls.DataGridItemEventArgs) Handles
    DataGrid1.ItemDataBound
    If e.Item.ItemType = ListItemType.EditItem Then
    Dim ds As DataSet = loaddata()
    Dim dl As DropDownList = e.Item.FindControl("DropDownList1")
    dl.DataSource = ds
    dl.DataTextField = ds.Tables(0).Columns(1).ToString()
    dl.DataValueField = ds.Tables(0).Columns(1).ToString()
    dl.DataBind()
    End If
    End Sub

    Regards,
    Manish
    www.ComponentOne.com


    "RN1" wrote:

    > A DataGrid displays 3 columns from a database table - ID, Name &
    > Subject. When the DataGrid is in the editable mode, I want the 3rd
    > column Subject to a DropDownList so that users can change the subject.
    > This is how I tried it:
    >
    > --------------------------------------------------------------------------------
    > <script runat="server">
    > Public dSet As DataSet
    > Public strSQL As String
    > Public sqlDapter As SqlDataAdapter
    > Public sqlConn As New SqlConnection(".......")
    >
    > Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
    > strSQL = "SELECT * FROM tblSS"
    > Call LoadData(strSQL)
    >
    > If Not (Page.IsPostBack) Then
    > dgSS.DataBind()
    > End If
    > End Sub
    >
    > Sub LoadData(ByVal SQLQuery As String)
    > sqlDapter = New SqlDataAdapter(strSQL, sqlConn)
    >
    > dSet = New DataSet
    > sqlDapter.Fill(dSet, "SS")
    >
    > dgSS.DataSource = dSet.Tables("SS").DefaultView
    > End Sub
    >
    > Sub Edit_Command(ByVal obj As Object, ByVal ea As
    > DataGridCommandEventArgs)
    > If (ea.Item.ItemType = ListItemType.Item Or ea.Item.ItemType =
    > ListItemType.AlternatingItem) Then
    > Dim ddl As DropDownList
    >
    > ddl = CType(ea.Item.FindControl("ddlSubject"),
    > DropDownList)
    >
    > strSQL = "SELECT DISTINCT(Subject) FROM tblSS"
    > sqlDapter = New SqlDataAdapter(strSQL, sqlConn)
    >
    > dSet = New DataSet
    > sqlDapter.Fill(dSet, "Subject")
    >
    > ddl.DataSource = dSet.Tables("Subject")
    > ddl.DataBind()
    > End If
    >
    > dgSS.EditItemIndex = ea.Item.ItemIndex
    >
    > strSQL = "SELECT * FROM tblSS"
    > Call LoadData(strSQL)
    > dgSS.DataBind()
    > End Sub
    > </script>
    >
    > <form runat="server">
    > <asp:DataGrid ID="dgSS" OnEditCommand="Edit_Command" runat="server">
    > <Columns>
    > <asp:TemplateColumn HeaderText="#">
    > <ItemTemplate>
    > <asp:Label ID="lblID" Text='<%# Container.DataItem("ID") %>'
    > runat="server"/>.
    > </ItemTemplate>
    > </asp:TemplateColumn>
    >
    > <asp:BoundColumn DataField="SName" HeaderText="NAME"/>
    >
    > <asp:TemplateColumn HeaderText="SUBJECT">
    > <ItemTemplate>
    > <asp:Label ID="lblSubject" Text='<%# Container.DataItem("Subject") %>'
    > runat="server"/>
    > </ItemTemplate>
    > <EditItemTemplate>
    > <asp:DropDownList ID="ddlSubject" DataTextField="Subject"
    > runat="server"/>
    > </EditItemTemplate>
    > </asp:TemplateColumn>
    > <asp:EditCommandColumn CancelText="CANCEL" EditText="EDIT"
    > HeaderText="EDIT" UpdateText="UPDATE"/>
    > </Columns>
    > </asp:DataGrid>
    > </form>
    > --------------------------------------------------------------------------------
    >
    > But when I click the EDIT link in the DataGrid to change the DataGrid
    > into editable mode, the following error gets generated:
    >
    > Object reference not set to an instance of an object.
    >
    > pointing to the ddl.DataSource...... line in the above code.
    >
    > What am I doing wrong?
    >
    Manish, Mar 10, 2008
    #2
    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. mr2_93
    Replies:
    1
    Views:
    3,591
    mr2_93
    Oct 2, 2005
  2. tshad
    Replies:
    8
    Views:
    2,221
    tshad
    Oct 19, 2005
  3. =?Utf-8?B?SnVhbmpv?=

    GridView: Filtr DropDownList from another DropDownList

    =?Utf-8?B?SnVhbmpv?=, Dec 23, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    1,859
    =?Utf-8?B?SnVhbmpv?=
    Dec 23, 2005
  4. =?Utf-8?B?SnVhbmpv?=

    GridView: Filter DropDownList for another DropDownList

    =?Utf-8?B?SnVhbmpv?=, Dec 29, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    1,292
    =?Utf-8?B?SnVhbmpv?=
    Dec 29, 2005
  5. keithb
    Replies:
    1
    Views:
    11,950
    shauns1
    Nov 1, 2006
Loading...

Share This Page