Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / March 2008

Tip: Looking for answers? Try searching our database.

DropDownList DataGrid

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
RN1 - 10 Mar 2008 00:41 GMT
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 - 10 Mar 2008 16:41 GMT
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

> A DataGrid displays 3 columns from a database table - ID, Name &
> Subject. When the DataGrid is in the editable mode, I want the 3rd
[quoted text clipped - 90 lines]
>
> What am I doing wrong?

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.