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