DropDownList DataGrid

R

RN1

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?
 
M

Manish

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Dynamic BoundColumn 2
DataGrid Edit Problem 9
Default Selected Item in DropDownList 4
Insert New Record 3
Show "No Records Found" Message 1
DB Value in Label 1
DataGrid.Columns(Index) 3
DataBind 1

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top