Objective: The objective is to set the System.Drawing.Color dynamically based on a text value within a datatable. The text value is the name of a color within System.Drawing.Color. The purpose is to change the backcolor of a grid based on the given value.
Issue: The method I am using now does not obtain a value from the datatable and sets the Color to 0,0,0,0. The datatable is created using a ViewState. The research I've conducted on this issue indicates this should work. However, the value obtained from the DataTable is "" . What is incorrect in this code? Thank you in advance for you comments, suggestions and consideration. The code is as follows:
DataTable code
private void CreateList()
{
DataTable dtMyList = new DataTable();
DataColumn dcID = new DataColumn("ID");
dtMyList.Columns.Add(dcID);
DataColumn dcColor = new DataColumn("Color");
dtMyList.Columns.Add(dcColor);
ViewState["MyList"] = dtMyList;
}
On RowDataBound code intended to change the backcolor
protected void grdMyList_RowDataBound(object sender, GridViewEventsArgs e)
{
DataTable dtMyList = (DataTable)ViewState["MyList"];
for (int i = 0; i < dtMyList.Rows.Count; i++)
{
e.Row.Cells[0].BackColor = System.Drawing.Color.FromName(Convert.ToString(dtMyList.Rows[0]["Color"]));
}
}