Basically I have a gridview that is using a list as it’s datasource. How do I tell the gridview which column in the list is which?
// Regions
Dictionary<int, string> regions = (Master as SiteMaster).getCustomerRegions((Master as SiteMaster).userBasic_XsiteCustomerID);
List<List<string>> regionsList = new List<List<string>>();
foreach (int curRegionID in regions.Keys)
{
List<string> curRegion = new List<string>();
Dictionary<string, string> curRegionDetail = (Master as SiteMaster).getRegionDetail(curRegionID);
curRegion.Add(curRegionID.ToString());
curRegion.Add(curRegionDetail["regionName"]);
regionsList.Add(curRegion);
}
grv_regionManagement.DataSource = regionsList;
grv_regionManagement.DataBind();
So it’s throwing me an error that say regionID is not define, which make sense since there is no where that tell the gridview that the first column is actually the regionID.
Here is the code in ASP.NET for defining the regionID field:
<asp:TemplateField HeaderText="regionID" InsertVisible="False"
SortExpression="regionID" Visible="False">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("regionID") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("regionID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Rather than a list of lists of strings, have a list of objects that have your columns as properties. You can use these properties as the datasource for each column.