public class MyItem
{
public string Name { get; set; }
public int UserId { get; set; }
}
public void MethodThatLoadsMyData
{
var originalListData = MethodThatFetchesMyData(45);
myDropDownList.DataSource = MethodThatBumpsTwoItemDatasources(myOuterList, originalListData);
myDropDownList.DataBind();
}
public void MethodThatBumpsTwoItemDatasources(List<MyItem> outerList, List<MyItem> dropdownList)
{
/*This modifies the original result set from the database that you were going
to use to populate your dropdown list. It compares the outer list (whatever that is)
with the result set, and adds items called "Not Available" when there is an item in the
outer list that doesn't exist in the dropdownlist */
var result = new List<Item>();
foreach (var item in listA)
{
var matched = false;
foreach (var itemB in listB.Where(itemB => itemB.Id == item.Id))
{
matched = true;
result.Add(item);
}
if (!matched)
{
result.Add(new Item
{
Name = "Not Available",
Id = 0
});
}
matched = false;
}
return result;
}
public List<MyItem> MethodThatFetchesMyData(int myParameter)
{
//gets my data from the database and builds dto objects
var sql = "my_stored_procedure_or_sql";
var list = new List<MyItems>();
using(var conn = new SqlConnection(myConnectionString))
{
using(var comm = new SqlCommand(sql, conn))
{
//do your normal client setup here (sql type, parameters, etc//
var parameters = SqlParameter[1];
parameters[0] = new SqlParameter("@ParameterName", DbType.Int);
parameters[0].Value = myParameter;
comm.Parameters = parameters;
comm.CommandType = CommandType.StoredProcedure;
conn.Open();
using(var rdr = comm.ExecuteReader())
{
while(rdr.Read())
{
list.Add(
new MyItem{
Name = rdr["NameColumn"].ToString(),
UserId = rdr["ID"]
});
}
return list;
}
}
}
}
In your databound controls, you can get at the new values in the itemtemplate with your typical
<%#Eval("Name") %> <%#Eval("UserId") %>
What I'm actually doing here is binding the controls to a list of actual objects instead of the datatable that is constructed with the datasource control. By doing that, I can do whatever I need to do with those lists before binding it to the control. In this case, I bump the two lists together and add items for those that do not exist in one list but exist in the other. Not sure if this is exactly what you needed, but this should be enough to give you some ideas.