Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > DropDownList DataGrid

Reply
Thread Tools

DropDownList DataGrid

 
 
RN1
Guest
Posts: n/a
 
      03-09-2008
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">
<aspataGrid 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>
<aspropDownList ID="ddlSubject" DataTextField="Subject"
runat="server"/>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn CancelText="CANCEL" EditText="EDIT"
HeaderText="EDIT" UpdateText="UPDATE"/>
</Columns>
</aspataGrid>
</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?
 
Reply With Quote
 
 
 
 
Manish
Guest
Posts: n/a
 
      03-10-2008
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">
> <aspataGrid 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>
> <aspropDownList ID="ddlSubject" DataTextField="Subject"
> runat="server"/>
> </EditItemTemplate>
> </asp:TemplateColumn>
> <asp:EditCommandColumn CancelText="CANCEL" EditText="EDIT"
> HeaderText="EDIT" UpdateText="UPDATE"/>
> </Columns>
> </aspataGrid>
> </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?
>

 
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
GridView: Filter DropDownList for another DropDownList =?Utf-8?B?SnVhbmpv?= ASP .Net 0 12-29-2005 07:44 AM
GridView: Filtr DropDownList from another DropDownList =?Utf-8?B?SnVhbmpv?= ASP .Net 0 12-23-2005 01:31 PM
databinding a Dropdownlist to another dropdownlist tshad ASP .Net 8 10-19-2005 10:00 PM
Using a data-bind dropdownlist to populate another data-bind dropdownlist mr2_93 ASP .Net 1 10-02-2005 05:07 PM
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



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57