By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,120 Members | 1,786 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,120 IT Pros & Developers. It's quick & easy.

Dropdownlist in Datagrid

P: n/a
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>
Nov 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.FindByTe xt(currValue))
myDropDown.SelectedIndex = idx


On Tue, 30 Nov 2004 11:43:05 -0800, "Richard"
<Ri*****@discussions.microsoft.com> wrote:
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.DataGridCommandEventArg s) 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>


Nov 18 '05 #2

P: n/a
Thank you hansiman! This was what I needed!

"hansiman" wrote:
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.FindByTe xt(currValue))
myDropDown.SelectedIndex = idx


On Tue, 30 Nov 2004 11:43:05 -0800, "Richard"
<Ri*****@discussions.microsoft.com> wrote:
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.DataGridCommandEventArg s) 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>


Nov 18 '05 #3

P: n/a
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(Contai ner.DataItem,"Division"))%
'/> </EditItemTemplate>

"Richard" <Ri*****@discussions.microsoft.com> wrote in message
news:74**********************************@microsof t.com... 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>

Nov 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.