G
Guest
I've seen articles on GotDotNet and elsewhere on how to put a ddl in a
datagrid, and have been able to implement this technique. For a new item,
among the datagrid columns there is the one ddl for the user to choose an
account description, and when the user saves, then the value is saved and
displayed in a bound column in the datagrid. So far so good.
The problem is when the user edits the line. The ddl is refreshed with all
of the choices as a result of the databind method in the grid's editcommand
event, but I need to synchronize the user's current choice that is in the
bound column and display that choice in the ddl. I can retrieve the value in
the bound column with no problem, but when I try to find the the ddl using
FindControl, it returns nothing.
Any help would be greatly appreciated!
Here is some code:
'This event occurs immediately when the user clicks on the Edit button in
the grid.
Private Sub grdLineItems_EditCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
grdLineItems.EditCommand
Dim ds As DataSet
Dim dv As DataView
Try
ds = CType(Session("DSNewLineItems"), DataSet)
dv = ds.Tables(0).DefaultView
With grdLineItems
.EditItemIndex = e.Item.ItemIndex
.DataSource = dv
.DataBind() 'this triggers GetBillTos procedure below
End With
Dim UsersChoice As String
'Get the selected BillToID in the bound column to synchronize
with the ddl
UsersChoice = e.Item.Cells(6).Text 'this works fine
'Since we're editing, show the BillTo dropdown list
grdLineItems.Columns(7).Visible = True
'Show the user's current selection in the ddl
Dim ddl As DropDownList
Dim TempValue As String
'This line does not find the control - returns Nothing
ddl = E.Item.FindControl("ddlBillTos")
'...so this line throws an error - object not set
ddl.SelectedItem.Value = UsersChoice
Session("POAddMode") = False
lnkAdd.Visible = False
Catch ex As Exception
lblError.Text = ex.Message
End Try
End Sub
Protected Function GetBillTos(ByVal DeptID As Integer) As DataTable
'This function returns all of the account numbers that the user can
select from, based on his/her department.
Dim ds As DataSet
Dim strSQL As String = "up_select_parm_billtos"
Dim strConn As String = Session("ConnectStringSQL").ToString
'Dim NewConnection As SqlConnection = New SqlConnection(strConn)
'Dim SqlDa As SqlDataAdapter
Try
Dim paramDeptID As New SqlParameter("@DeptID", SqlDbType.Int, 4)
paramDeptID.Value = DeptID
ds = DataHandlerSqlClient.ExecuteDataset(strConn,
CommandType.StoredProcedure, strSQL, paramDeptID)
Return ds.Tables(0)
Catch ex As Exception
Throw ex
End Try
End Function
Here is my html code for the bound column:
<asp:BoundColumn DataField="BillToID" ReadOnly="True" HeaderText="Account #">
<HeaderStyle Font-Bold="True" Wrap="False" HorizontalAlign="Left"
ForeColor="White"></HeaderStyle>
<ItemStyle Wrap="False" HorizontalAlign="Left"></ItemStyle>
</asp:BoundColumn>
'Here is my html for the ddl
<asp:TemplateColumn Visible="False" HeaderText="Bill to Account">
<HeaderStyle Font-Bold="True" Wrap="False" HorizontalAlign="Left"
ForeColor="White"></HeaderStyle>
<ItemStyle Wrap="False" HorizontalAlign="Left"></ItemStyle>
<EditItemTemplate>
'This is where the ddl is populated:
<aspropDownList id=ddlBillTos runat="server" DataValueField="BillToID"
DataTextField="BillToDesc" DataSource='<%#
GetBillTos(cint(Session("DeptID"))) %>'>
</aspropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
datagrid, and have been able to implement this technique. For a new item,
among the datagrid columns there is the one ddl for the user to choose an
account description, and when the user saves, then the value is saved and
displayed in a bound column in the datagrid. So far so good.
The problem is when the user edits the line. The ddl is refreshed with all
of the choices as a result of the databind method in the grid's editcommand
event, but I need to synchronize the user's current choice that is in the
bound column and display that choice in the ddl. I can retrieve the value in
the bound column with no problem, but when I try to find the the ddl using
FindControl, it returns nothing.
Any help would be greatly appreciated!
Here is some code:
'This event occurs immediately when the user clicks on the Edit button in
the grid.
Private Sub grdLineItems_EditCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
grdLineItems.EditCommand
Dim ds As DataSet
Dim dv As DataView
Try
ds = CType(Session("DSNewLineItems"), DataSet)
dv = ds.Tables(0).DefaultView
With grdLineItems
.EditItemIndex = e.Item.ItemIndex
.DataSource = dv
.DataBind() 'this triggers GetBillTos procedure below
End With
Dim UsersChoice As String
'Get the selected BillToID in the bound column to synchronize
with the ddl
UsersChoice = e.Item.Cells(6).Text 'this works fine
'Since we're editing, show the BillTo dropdown list
grdLineItems.Columns(7).Visible = True
'Show the user's current selection in the ddl
Dim ddl As DropDownList
Dim TempValue As String
'This line does not find the control - returns Nothing
ddl = E.Item.FindControl("ddlBillTos")
'...so this line throws an error - object not set
ddl.SelectedItem.Value = UsersChoice
Session("POAddMode") = False
lnkAdd.Visible = False
Catch ex As Exception
lblError.Text = ex.Message
End Try
End Sub
Protected Function GetBillTos(ByVal DeptID As Integer) As DataTable
'This function returns all of the account numbers that the user can
select from, based on his/her department.
Dim ds As DataSet
Dim strSQL As String = "up_select_parm_billtos"
Dim strConn As String = Session("ConnectStringSQL").ToString
'Dim NewConnection As SqlConnection = New SqlConnection(strConn)
'Dim SqlDa As SqlDataAdapter
Try
Dim paramDeptID As New SqlParameter("@DeptID", SqlDbType.Int, 4)
paramDeptID.Value = DeptID
ds = DataHandlerSqlClient.ExecuteDataset(strConn,
CommandType.StoredProcedure, strSQL, paramDeptID)
Return ds.Tables(0)
Catch ex As Exception
Throw ex
End Try
End Function
Here is my html code for the bound column:
<asp:BoundColumn DataField="BillToID" ReadOnly="True" HeaderText="Account #">
<HeaderStyle Font-Bold="True" Wrap="False" HorizontalAlign="Left"
ForeColor="White"></HeaderStyle>
<ItemStyle Wrap="False" HorizontalAlign="Left"></ItemStyle>
</asp:BoundColumn>
'Here is my html for the ddl
<asp:TemplateColumn Visible="False" HeaderText="Bill to Account">
<HeaderStyle Font-Bold="True" Wrap="False" HorizontalAlign="Left"
ForeColor="White"></HeaderStyle>
<ItemStyle Wrap="False" HorizontalAlign="Left"></ItemStyle>
<EditItemTemplate>
'This is where the ddl is populated:
<aspropDownList id=ddlBillTos runat="server" DataValueField="BillToID"
DataTextField="BillToDesc" DataSource='<%#
GetBillTos(cint(Session("DeptID"))) %>'>
</aspropDownList>
</EditItemTemplate>
</asp:TemplateColumn>