Dropdownlist in Datagrid

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:
<asp:DropDownList id=ddlBillTos runat="server" DataValueField="BillToID"
DataTextField="BillToDesc" DataSource='<%#
GetBillTos(cint(Session("DeptID"))) %>'>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
 
H

hansiman

Take a look at this:

Private Sub dg_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles
dg.ItemDataBound

Dim u As New utils

' in EDIT MODE, make sure cbo have correct selected item (index)
If e.Item.ItemType = ListItemType.EditItem Then
Dim currValue As String = ""
Dim myDropDown As DropDownList
Dim idx As Integer

' cboGroup
currValue = CType(e.Item.DataItem("EmployeeGroup"), String)
myDropDown = CType(e.Item.FindControl("cboEmployeeGroup"),
DropDownList)
idx =
myDropDown.Items.IndexOf(myDropDown.Items.FindByText(currValue))
myDropDown.SelectedIndex = idx
 
G

Guest

Thank you hansiman! This was what I needed!

hansiman said:
Take a look at this:

Private Sub dg_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles
dg.ItemDataBound

Dim u As New utils

' in EDIT MODE, make sure cbo have correct selected item (index)
If e.Item.ItemType = ListItemType.EditItem Then
Dim currValue As String = ""
Dim myDropDown As DropDownList
Dim idx As Integer

' cboGroup
currValue = CType(e.Item.DataItem("EmployeeGroup"), String)
myDropDown = CType(e.Item.FindControl("cboEmployeeGroup"),
DropDownList)
idx =
myDropDown.Items.IndexOf(myDropDown.Items.FindByText(currValue))
myDropDown.SelectedIndex = idx
 
E

Eric

Another solution is to use the Edit Item Template and the SelectedIndex
property and bind that to a function that gives it the proper index to set
like so:
<EditItemTemplate>
<asp:DropDownList runat="server" DataSource='<%# GetDivisions() %>'
DataTextField="Division" DataValueField="DivisionID" ID="ddlEditDivisions"
SelectedIndex='<%#GetDivIdx(DataBinder.Eval(Container.DataItem,"Division"))%
</EditItemTemplate>
 

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

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,782
Latest member
ThomasGex

Latest Threads

Top