binding to current value in a drop down list when populating from a database

  • Thread starter Thread starter TB
  • Start date Start date
T

TB

Hi All:

The following is probably a newbie question, but please bear with me:

I am populating a drop down list with items from a database, and would
the default selected item to be the current value of the corresponding
field. I am doing something like this:

ddlexample.DataSource = mydatareader
ddlexample.DataValueField = "ID"
ddlexample.DataTextField = "Name"
ddlexample.SelectedIndex =
ddlexample.Items.IndexOf(ddlexample.Items.FindByValue(currvalue))
ddlexample.DataBind()

the variable "Currvalue" contains the current value of "ID"

But the result is that the first item of the list is always displayed
as the selected item, not the one the corresponds to the actual value
of the field "ID"

What I am doing wrong here?

Thanks

TB
 
Set the SelectedIndex property after the DataBind() call.

Hi All:

The following is probably a newbie question, but please bear with me:

I am populating a drop down list with items from a database, and would
the default selected item to be the current value of the corresponding
field. I am doing something like this:

ddlexample.DataSource = mydatareader
ddlexample.DataValueField = "ID"
ddlexample.DataTextField = "Name"
ddlexample.SelectedIndex =
ddlexample.Items.IndexOf(ddlexample.Items.FindByValue(currvalue))
ddlexample.DataBind()

the variable "Currvalue" contains the current value of "ID"

But the result is that the first item of the list is always displayed
as the selected item, not the one the corresponds to the actual value
of the field "ID"

What I am doing wrong here?

Thanks

TB
 
Following your advice, I just tried:

ddlexample.DataSource = mydatareader
ddlexample.DataValueField = "ID"
ddlexample.DataTextField = "Name"
ddlexample.DataBind()
ddlexample.SelectedIndex =
ddlexample.Items.IndexOf(ddlexample.Items.FindByValue(currvalue))

But the result is still the same: the selected item is still the first
in the list, not the current value of the ID field.

TB
 
This can be the case if the currvalue was not found in the list. While
developing asp.net programming skills, I would suggest expanding the code
lines for clarity and easy debugging, such as:



Dim li As ListItem = ddlexample.Items.FindByValue(currvalue)
If li Is Nothing Then
'this will throw an exception that the value was not found
'which you will see on the web page. After you complete the
'debugging you can comment out this line
Throw New Exception("No list item with the value = " & currvalue & " was
found")
Else
ddlexample.ClearSelection()
li.Selected = True
End If
 
Does the ID value exist in the list?

Following your advice, I just tried:

ddlexample.DataSource = mydatareader
ddlexample.DataValueField = "ID"
ddlexample.DataTextField = "Name"
ddlexample.DataBind()
ddlexample.SelectedIndex =
ddlexample.Items.IndexOf(ddlexample.Items.FindByValue(currvalue))

But the result is still the same: the selected item is still the first
in the list, not the current value of the ID field.

TB
 
Hi,

This is c# code, try this .

protected void OrdersGrid_ItemDataBound(object sender, DataGridItemEventArgs e)

{

// Initialising Connection & Command Objects

SqlConnection oConn = new SqlConnection(ConnectionStr);

SqlCommand oComm =
new SqlCommand();

// Create DropDownLists for Edit mode (in selected row)

if (e.Item.ItemType == ListItemType.EditItem)

{

// Prepare Connnect & Command Objects

oComm.Connection = oConn;

oComm.CommandType = CommandType.StoredProcedure;

DropDownList ddlSupplier, ddlExpType;



// Get Row Details

DataRowView drv = (DataRowView)e.Item.DataItem;

string currValue;

// Departments DropDownList - Create, Fill and Preselect

oComm.CommandText = "sp_PO_ListSuppliers";

oConn.Open();

ddlSupplier = (DropDownList)e.Item.FindControl("ddlSupplier");

ddlSupplier.DataSource = oComm.ExecuteReader(CommandBehavior.CloseConnection);

ddlSupplier.DataBind();

currValue = Convert.ToString(drv["SupplierNo"]);

ddlSupplier.Items.FindByValue(currValue).Selected =
true;

// Cost Codes DropDownList - Create, Fill and Preselect

oComm.CommandText = "sp_PO_ListExpType";

oConn.Open();

ddlExpType = (DropDownList)e.Item.FindControl("ddlExpType");

ddlExpType.DataSource = oComm.ExecuteReader(CommandBehavior.CloseConnection);

ddlExpType.DataBind();

currValue = Convert.ToString(drv["ExpenditureType"]);

ddlExpType.Items.FindByValue(currValue).Selected =
true;

}

}

 
binding to current value in a drop down list when populating from a database Reply

I am doing the following:

the bcUOM.GetUOM is just a external method that returns a dataset populated with the fields.
It is just a standard get.
ListName is a String

in my Page_load method:
Code:
     DataSet dscUOM = bcUOM.GetUOM(ClientState);
     for (int i = 0; i < dscUOM.Tables[0].Rows.Count; i++ ) 
     {
       ListName = dscUOM.Tables[0].Rows[i]["uomcode"].ToString() + " - " +
   	dscUOM.Tables[0].Rows[i]["uomdesc"].ToString();
      ddlUOMCode.Items.Add(new ListItem(ListName, dscUOM.Tables[0].Rows[i]["uomcode"].ToString().Trim()));
       }

Now do this after the previous.
Code:
    ddlUOMCode.SelectedIndex = ddlUOMCode.Items.IndexOf(ddlUOMCode.Items.FindByValue(ModRow["uomcode"].ToString().Trim()));

If this seems confusing, i am sorry....
I am a bit of a sloppy programmer....
Hope this helps
 
Sorry for not replying before. Everything works now. Some bug testing
revealed an incorrect use of the variable currvalue. Also, it is clear
that databinding must take place before the selecteditem property.

Thanks for your help.

TB
 

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

Back
Top