Preselect item in DropdownList Edit mode???

G

Guest

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("strConn"))
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("strConn"))
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(currentgenre))

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
 
G

Guest

Tim::.. said:
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("strConn"))
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("strConn"))
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(currentgenre))

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.
 
G

Guest

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???
 
G

Guest

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
 

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

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top