What you need to do is put all the information that is to go into the droplist into a datable. You then need to send the data item in the datagrid cell to a function which will iterate through the datatable while incrementing a counter until it finds a match (eg name). This counter is returned to the droplist as its index number.
Here is an example of my code which will explain it better. First we'll start with the drop down list asp code. You'll see that when the binding data event happens and the datagrid cell turns into a drop list it the dropdownlis Edit_level calls two functions. The first is LevelDrop(), which can be seen underneath the droplist code. This returns a Datatable of data that is the DataSource of the dropList. The second function is called by the selectedIndex of the DropDownList. This is the function called GetAreaDropListIndex and can be seen under the LevelDrop function(). This function takes the string which was in the datagrid cell (and is passed to the function in the Asp code as "AwardLevelName"), it then calls the
LevelDrop() function. This again returns the same DataTable which the droplist recieved. Using a counter and a for statement, the function compares the string against all the variables in the DataTable, if a match is found the counter is returned to the droplist instructing it what index to show once it is bound to the datagrid.
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "AwardLevelName") %>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList runat="server" id= "Edit_Level" Datasource= '<%#LevelDrop()%>' datatextfield= "AwardLevelName" SelectedIndex= '<%#GetLevelDropListIndex((string)DataBinder.Eval( Container.DataItem, "AwardLevelName"))%>' />
</EditItemTemplate>
</asp:TemplateColumn>
DataTable LevelDrop(){
string ConnectionString = "server=\'diginet\'; user id=\'training\'; password=\'training\'; Database=\'training\'";
string CommandText = "select AwardLevelName from AwardLevel";
SqlConnection myConnection = new SqlConnection(ConnectionString);
SqlDataAdapter myCommand = new SqlDataAdapter(CommandText, myConnection);
DataSet ds = new DataSet();
myCommand.Fill(ds, "LevelList");
return ds.Tables["LevelList"];
}
int GetAreaDropListIndex(string area){
//returns the index setting of the area droplist, which it must display when called.
DataTable AreaTable = AreaDrop();
for (int i=0; i<AreaTable.DefaultView.Count; i++)
{
if(AreaTable.DefaultView[i]["area"].ToString()==area)
{
return i;
}
}
return 0;
}
I'm afraid the code is in C# but the theory will be the same in
VB.