473,320 Members | 1,695 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Dropdownlist in Datagrid

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
3 1785
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Stanley J Mroczek | last post by:
How do you load a dropdownlist when edit is clicked in a datagrid ? <Columns> <asp:BoundColumn DataField="OptionDescription" ItemStyle-Wrap="True" HeaderText="Option...
2
by: rmorvay | last post by:
I am trying to dynamically build a dropdownlist and bind it to a cell in a grid. I tried to utilize the following code but I am stuck at the point where I bind the dropdownlist to the grid cell. ...
2
by: Dominic | last post by:
Hi guys, I'm not sure if this question belongs to FAQ, but I couldn't find a concrete answer. I created a Datagrid control using ItemTemplate, but it's NOT a in-place editing datagrid. One of...
0
by: Shane O. Pinnell | last post by:
I am sure this has come up before, but I haven't been able to find an answer as of yet. That said, any help is definitely appreciated! I have a datagrid populated from a dataset. I have a...
2
by: Shiju Poyilil | last post by:
Hello, I have a datagrid with only one row and its having 2 dropdownlists, I need to populate the secodn dropdownlist on the basis of the selection in the first dropdown. but I am not able to...
1
by: m3ckon | last post by:
Hi there, please help if you can, I'm having an issue with droponnlists in a datagrid I have a datagrid which is populated from a query .. all works fine I've added two extra columns, one...
3
by: Tim::.. | last post by:
Can someone please tell me how I go about preselecting an item in a drop drown list when I click the Edit Command in a datagrid? I have tried the following but it doesn't work for me! I would...
0
by: Daniel Doyle | last post by:
Hello and apologies in advance for the amount of code in this post. I've also sent this message to the Sharepoint group, but thought that ASP.NET developers may also be able to help, even though...
4
by: Mark Waser | last post by:
I've discovered a very odd bug when attempting to put a dropdown list in a datagrid. In the page PreRender step, the selected index of the datagrid is successfully set during databinding. Yet,...
15
by: glenn | last post by:
Hi folks, I have a DropDownList in a DataGrid that is populated from records in a database. I want to add a value that might be a string such as "Select a Company" for the first item since an...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.