Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Populating and choosing default in a DropDown 1

Status
Not open for further replies.

bigfoot

Programmer
May 4, 1999
1,779
0
0
US
Here's a teaser: I know how to create and populate a DropDown, and also how to populate it from a database. But what about the default value? Read on...

I have a table (Table1) with 10 items that I populate a DropDown with. I show an add screen and the user chooses #5.

Now I save DropDown.SelectedItem.Value to my database (Table2). It's the letter "C".

The user brings up my Edit screen.
I repopulate the DropDown with the values from my table1, and then how do I match the value saved in Table2 with one of the values in the Dropdown.
Is there a search function. or do I have to go thru all the items until I find the one I want and then display it in the box.

Did I say it right?
 
How about something like:

DropDown.SelectedIndex = DropDown.Items.IndexOf(DropDown.Items.FindByValue(ID));

--Lenard
 
Lenard, you are the greatest! Thanks.

I need more documentation on the .NET world. I'm trying to get by with WebMatrix until my VS.NET 2003 arrives.
 
LenardD, What if I just want a default for a bound dropdownlist? Or maybe a default of no entry?

Thank you,
 
If you want a default of no-entry, use -1. Another idea is to add a blank item in the first position on the list and then set the SelectedIndex = 0. Keep in mind that the latter then can't be bound.

public static DataView AddBlankItem( DataTable dt, string blankName )
{
DataTable dtTemp = dt.Clone();

// Add the "blank" item first
DataRow drTemp = dtTemp.NewRow();
drTemp["ID"] = 0;
drTemp["Name"] = blankName;
dtTemp.Rows.Add( drTemp );

// Now add the rest of the items
foreach( DataRow dr in dt.Rows )
dtTemp.ImportRow( dr );

return( dtTemp.DefaultView );
}

--Lenard
 
Can you translate that into VB.NET?

Thanks,
 
I could but that would take the fun out of it for you......just kidding.....here are you:

Public Shared Function AddBlankItem(ByVal dt As DataTable, ByVal blankName As String) As DataView

Dim dtTemp As DataTable = dt.Clone()

' Add the "blank" item first
Dim drTemp As DataRow = dtTemp.NewRow()
drTemp("ID") = 0
drTemp("Name") = blankName
dtTemp.Rows.Add(drTemp)

' Now add the rest of the items
Dim dr As DataRow

For Each dr In dt.Rows
dtTemp.ImportRow(dr)
Next

Return (dtTemp.DefaultView)

End Function

Keep in mind the "ID" and "Name" may be different for you. In my case, I use the function with lookup tables so I always name my fields with those names.

To use it, all you do then is:

DropDownList.DataSource = AddBlankItem( dataSet.Tables(&quot;MyTable&quot;).DefaultView, &quot;<None>&quot; )

Incidently, if you just wanted the &quot;blank&quot; item to show blank, then just do a &quot;&quot; for the second parameter.

--Lenard
 
Here is another way you can set a default:

DropDown.Items.FindByValue(MyValue).Selected = true;

--Lenard
 
If you are going to use DropDown.Items.FindByValue(MyValue).Selected = true method, you have to do a DropDown.ClearSelection before or you will get an error the next time you set it again.

--Lenard

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top