473,416 Members | 1,778 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,416 software developers and data experts.

Preselect item in DropdownList Edit mode???

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 be really grateful for any assistance!

Thanks
...CODE..
Private Function GetSelectedIndex(ByVal PageID As String) As Integer

Dim Myconn As New
SqlConnection(ConfigurationSettings.AppSettings("s trConn"))
Dim cmd As New SqlCommand("SelectOffice", Myconn)
cmd.CommandType = CommandType.StoredProcedure

Myconn.Open()

Dim myReader As SqlDataReader = cmd.ExecuteReader()

If myReader.HasRows Then
Do While myReader.Read()
Console.WriteLine(vbTab & "{0}" & vbTab & "{1}",
myReader.GetInt32(0), myReader.GetString(1))
Loop
Else
Console.WriteLine("No rows returned.")
End If

myReader.Close()

Myconn.Close()
End Function


Sub populateDDL_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs)
'Construct the Database Connection
Dim Myconn As New
SqlConnection(ConfigurationSettings.AppSettings("s trConn"))
Dim cmd As New SqlCommand("SelectOffice", Myconn)
If e.Item.ItemType = ListItemType.EditItem Then

cmd.CommandType = CommandType.StoredProcedure

Myconn.Open()

Dim add_Office As DropDownList
Dim pageID = CInt(CType(e.Item.FindControl("pageID"),
TextBox).Text)

'Populates Office DropDownList with office names
add_Office = CType(e.Item.FindControl("DDLeditOffice"),
DropDownList)
add_Office.DataSource =
cmd.ExecuteReader(CommandBehavior.CloseConnection)
add_Office.DataTextField = "offName"
add_Office.DataValueField = "officeID"
add_Office.DataBind()
add_Office.SelectedIndex = GetSelectedIndex()
Myconn.Close()

'ddl = CType(e.Item.FindControl("DDLeditOffice"), DropDownList)
' ddl.SelectedIndex =
ddl.Items.IndexOf(ddl.Items.FindByText(currentgenr e))

Else

If e.Item.ItemType = ListItemType.Footer Then

cmd.CommandType = CommandType.StoredProcedure

Myconn.Open()

Dim add_Office As DropDownList

'Populates Office DropDownList with office names
add_Office = CType(e.Item.FindControl("DDLaddOffice"),
DropDownList)
add_Office.DataSource =
cmd.ExecuteReader(CommandBehavior.CloseConnection)
add_Office.DataTextField = "offName"
add_Office.DataValueField = "officeID"
add_Office.DataBind()
add_Office.Items.Insert(0, "Select One")
add_Office.Items.FindByText("Select One").Value = 0 'insert
don't create a value, but we need a value during defaults
add_Office.SelectedIndex = 0
Myconn.Close()
End If
End If
End Sub
Nov 19 '05 #1
3 4358
"Tim::.." wrote:
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 be really grateful for any assistance!

Thanks
..CODE..
Private Function GetSelectedIndex(ByVal PageID As String) As Integer

Dim Myconn As New
SqlConnection(ConfigurationSettings.AppSettings("s trConn"))
Dim cmd As New SqlCommand("SelectOffice", Myconn)
cmd.CommandType = CommandType.StoredProcedure

Myconn.Open()

Dim myReader As SqlDataReader = cmd.ExecuteReader()

If myReader.HasRows Then
Do While myReader.Read()
Console.WriteLine(vbTab & "{0}" & vbTab & "{1}",
myReader.GetInt32(0), myReader.GetString(1))
Loop
Else
Console.WriteLine("No rows returned.")
End If

myReader.Close()

Myconn.Close()
End Function


Sub populateDDL_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs)
'Construct the Database Connection
Dim Myconn As New
SqlConnection(ConfigurationSettings.AppSettings("s trConn"))
Dim cmd As New SqlCommand("SelectOffice", Myconn)
If e.Item.ItemType = ListItemType.EditItem Then

cmd.CommandType = CommandType.StoredProcedure

Myconn.Open()

Dim add_Office As DropDownList
Dim pageID = CInt(CType(e.Item.FindControl("pageID"),
TextBox).Text)

'Populates Office DropDownList with office names
add_Office = CType(e.Item.FindControl("DDLeditOffice"),
DropDownList)
add_Office.DataSource =
cmd.ExecuteReader(CommandBehavior.CloseConnection)
add_Office.DataTextField = "offName"
add_Office.DataValueField = "officeID"
add_Office.DataBind()
add_Office.SelectedIndex = GetSelectedIndex()
Myconn.Close()

'ddl = CType(e.Item.FindControl("DDLeditOffice"), DropDownList)
' ddl.SelectedIndex =
ddl.Items.IndexOf(ddl.Items.FindByText(currentgenr e))

Else

If e.Item.ItemType = ListItemType.Footer Then

cmd.CommandType = CommandType.StoredProcedure

Myconn.Open()

Dim add_Office As DropDownList

'Populates Office DropDownList with office names
add_Office = CType(e.Item.FindControl("DDLaddOffice"),
DropDownList)
add_Office.DataSource =
cmd.ExecuteReader(CommandBehavior.CloseConnection)
add_Office.DataTextField = "offName"
add_Office.DataValueField = "officeID"
add_Office.DataBind()
add_Office.Items.Insert(0, "Select One")
add_Office.Items.FindByText("Select One").Value = 0 'insert
don't create a value, but we need a value during defaults
add_Office.SelectedIndex = 0
Myconn.Close()
End If
End If
End Sub


I think your syntax is fine for preselecting the drop down list.
Following syntax should work:

add_Office.SelectedIndex = 0

So, check your control flow, whether you are reaching this syntax or not and
ensure you are not overriding your selection with some other code.

--
Cheers,
Rahul Anand

Nov 19 '05 #2
Hi Rahul,

I think you might have miss understood my question! I'm sorry if it was not
clear but I want the item shown in the standard datagrid view to be
preselected in the dropdown box when you enter edit mode!

EG:
If I have three offices "Admin, Budget and HR" Budget is shown in the
datagrid in the standard view and when I hit the edit button I want it to be
preselected in the dropdown list!

Thanks

Any ideas???
"Rahul Anand" wrote:
"Tim::.." wrote:
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 be really grateful for any assistance!

Thanks
..CODE..
Private Function GetSelectedIndex(ByVal PageID As String) As Integer

Dim Myconn As New
SqlConnection(ConfigurationSettings.AppSettings("s trConn"))
Dim cmd As New SqlCommand("SelectOffice", Myconn)
cmd.CommandType = CommandType.StoredProcedure

Myconn.Open()

Dim myReader As SqlDataReader = cmd.ExecuteReader()

If myReader.HasRows Then
Do While myReader.Read()
Console.WriteLine(vbTab & "{0}" & vbTab & "{1}",
myReader.GetInt32(0), myReader.GetString(1))
Loop
Else
Console.WriteLine("No rows returned.")
End If

myReader.Close()

Myconn.Close()
End Function


Sub populateDDL_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs)
'Construct the Database Connection
Dim Myconn As New
SqlConnection(ConfigurationSettings.AppSettings("s trConn"))
Dim cmd As New SqlCommand("SelectOffice", Myconn)
If e.Item.ItemType = ListItemType.EditItem Then

cmd.CommandType = CommandType.StoredProcedure

Myconn.Open()

Dim add_Office As DropDownList
Dim pageID = CInt(CType(e.Item.FindControl("pageID"),
TextBox).Text)

'Populates Office DropDownList with office names
add_Office = CType(e.Item.FindControl("DDLeditOffice"),
DropDownList)
add_Office.DataSource =
cmd.ExecuteReader(CommandBehavior.CloseConnection)
add_Office.DataTextField = "offName"
add_Office.DataValueField = "officeID"
add_Office.DataBind()
add_Office.SelectedIndex = GetSelectedIndex()
Myconn.Close()

'ddl = CType(e.Item.FindControl("DDLeditOffice"), DropDownList)
' ddl.SelectedIndex =
ddl.Items.IndexOf(ddl.Items.FindByText(currentgenr e))

Else

If e.Item.ItemType = ListItemType.Footer Then

cmd.CommandType = CommandType.StoredProcedure

Myconn.Open()

Dim add_Office As DropDownList

'Populates Office DropDownList with office names
add_Office = CType(e.Item.FindControl("DDLaddOffice"),
DropDownList)
add_Office.DataSource =
cmd.ExecuteReader(CommandBehavior.CloseConnection)
add_Office.DataTextField = "offName"
add_Office.DataValueField = "officeID"
add_Office.DataBind()
add_Office.Items.Insert(0, "Select One")
add_Office.Items.FindByText("Select One").Value = 0 'insert
don't create a value, but we need a value during defaults
add_Office.SelectedIndex = 0
Myconn.Close()
End If
End If
End Sub


I think your syntax is fine for preselecting the drop down list.
Following syntax should work:

add_Office.SelectedIndex = 0

So, check your control flow, whether you are reaching this syntax or not and
ensure you are not overriding your selection with some other code.

--
Cheers,
Rahul Anand

Nov 19 '05 #3
Hi Tim,

As per my understanding you want to to retrieve the setting from standard
DataGrid view to Edit mode view.

Once you have the data you can preselect your DropDownList in your Edit Item
command handler. You can use FindControl to find your DropDownList and use
SelectedValue property to set the default selection.

In your EditCommandHandler you will be getting DataGridCommandEventArgs (say
e) paramenter. From this parameter you can retrieve the DataGrid Item
(e.Item).

Now if you have only Bound Columns you can retrieve the value by accessing
the e.Item.Cells[index]. And if you have Controls then you will require to
call FindControl method.

In case you are defining ItemTemplate and EditItemTemplate then I will
suggest to add a redundant, readonly, invisible bound column which will have
a copy of ItemTemplate value. And thus you can retrieve it by simply
accessing Cells of DataGrid Item.

I hope now it will help you in solving your problem.

Cheers,
Rahul Anand
"Tim::.." wrote:
Hi Rahul,

I think you might have miss understood my question! I'm sorry if it was not
clear but I want the item shown in the standard datagrid view to be
preselected in the dropdown box when you enter edit mode!

EG:
If I have three offices "Admin, Budget and HR" Budget is shown in the
datagrid in the standard view and when I hit the edit button I want it to be
preselected in the dropdown list!

Thanks

Any ideas???
"Rahul Anand" wrote:
"Tim::.." wrote:
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 be really grateful for any assistance!

Thanks
..CODE..
Private Function GetSelectedIndex(ByVal PageID As String) As Integer

Dim Myconn As New
SqlConnection(ConfigurationSettings.AppSettings("s trConn"))
Dim cmd As New SqlCommand("SelectOffice", Myconn)
cmd.CommandType = CommandType.StoredProcedure

Myconn.Open()

Dim myReader As SqlDataReader = cmd.ExecuteReader()

If myReader.HasRows Then
Do While myReader.Read()
Console.WriteLine(vbTab & "{0}" & vbTab & "{1}",
myReader.GetInt32(0), myReader.GetString(1))
Loop
Else
Console.WriteLine("No rows returned.")
End If

myReader.Close()

Myconn.Close()
End Function


Sub populateDDL_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs)
'Construct the Database Connection
Dim Myconn As New
SqlConnection(ConfigurationSettings.AppSettings("s trConn"))
Dim cmd As New SqlCommand("SelectOffice", Myconn)
If e.Item.ItemType = ListItemType.EditItem Then

cmd.CommandType = CommandType.StoredProcedure

Myconn.Open()

Dim add_Office As DropDownList
Dim pageID = CInt(CType(e.Item.FindControl("pageID"),
TextBox).Text)

'Populates Office DropDownList with office names
add_Office = CType(e.Item.FindControl("DDLeditOffice"),
DropDownList)
add_Office.DataSource =
cmd.ExecuteReader(CommandBehavior.CloseConnection)
add_Office.DataTextField = "offName"
add_Office.DataValueField = "officeID"
add_Office.DataBind()
add_Office.SelectedIndex = GetSelectedIndex()
Myconn.Close()

'ddl = CType(e.Item.FindControl("DDLeditOffice"), DropDownList)
' ddl.SelectedIndex =
ddl.Items.IndexOf(ddl.Items.FindByText(currentgenr e))

Else

If e.Item.ItemType = ListItemType.Footer Then

cmd.CommandType = CommandType.StoredProcedure

Myconn.Open()

Dim add_Office As DropDownList

'Populates Office DropDownList with office names
add_Office = CType(e.Item.FindControl("DDLaddOffice"),
DropDownList)
add_Office.DataSource =
cmd.ExecuteReader(CommandBehavior.CloseConnection)
add_Office.DataTextField = "offName"
add_Office.DataValueField = "officeID"
add_Office.DataBind()
add_Office.Items.Insert(0, "Select One")
add_Office.Items.FindByText("Select One").Value = 0 'insert
don't create a value, but we need a value during defaults
add_Office.SelectedIndex = 0
Myconn.Close()
End If
End If
End Sub


I think your syntax is fine for preselecting the drop down list.
Following syntax should work:

add_Office.SelectedIndex = 0

So, check your control flow, whether you are reaching this syntax or not and
ensure you are not overriding your selection with some other code.

--
Cheers,
Rahul Anand

Nov 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: localhost | last post by:
A DataGrid with shows a label in one of the columns when in view mode. When in edit mode, I want to show a dropdown, and have the default selection set to what the textbox used to be. Right now...
2
by: NewToDotNet | last post by:
Hi, I am very new to ASP.NET and web programming in general. I have one issue. I have a Datagrid object with Edit template. In one Datagrid row, I have 1 DropdownList, 1 textbox and 1 readonly...
3
by: Sam | last post by:
Hi All, I'm having troubles with populating data to the dropdownlist in a datagrid and I just don't know what causes it. I have been struggling for 2 days now Would some one give me a hand? ...
0
by: jobs at webdos | last post by:
I have a formview that will either edit or insert records into a database. when I call the page with a query string ?x= its goes to edit mode and loads data from a datasource in the form and load...
2
by: =?Utf-8?B?Z2FuZQ==?= | last post by:
Hi, In a gridview, How can i display different columns between item and edit modes. For eg. i have a sql that returns productname, categoryname, etc. In viewmode, i need to display only...
3
by: Lohboy | last post by:
Using ASP.NET and IE7. (Sorry if I am posting in the wrong forum but my problem seemed to be more related to the JavaScript side than the ASP.NET side.) I have two DropDownList controls the...
0
by: Fabrizio | last post by:
Hi all. I have a field in a gridview as templatefield which is a label in view mode and a dropdownlist in edit mode with his datasource. This gridview is inserted as templatefield in another...
3
by: RPhlb | last post by:
This is my first post, so excuse me if I don't get this right the first time. I have an issue where when I use DropDownList I only get the first, or "SelectedValue" back when I update a GridView...
1
by: rogerford | last post by:
I have a grid which i bind with values from Database. I have events to edit and update the grid. I am not using SqlDatasource to connect to DB. Rather i am doing the updating of the grid...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.