0

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"]));

    }
}
Tristan Descartes
  • 145
  • 1
  • 4
  • 11

2 Answers2

1

First of all: Is the datatable filled with any data?

Second: you are trying to fill that one row (given in the event args) with all the colors from the datatable, resulting in that only one property (e.Row.Cells[0].BackColor) is filled with the color coming from the last row in the table (dtMyList.Rows[i]["Color"]).

I think you should first lookup the correct datarow which is attached to your gridrow (e.Row.DataItem), then read its color and then fill the property of your gridrow. Like this:

protected void grdMyList_RowDataBound(object sender, GridViewEventsArgs e)
{
    DataRow row = (DataRow)e.Row.DataItem;
    e.Row.Cells[0].BackColor = System.Drawing.Color.FromName(row["Color"].ToString());
}
Martin Mulder
  • 12,642
  • 3
  • 25
  • 54
  • The datatable is filled with data. That makes sense let me try that and see if it works. – Tristan Descartes Apr 10 '14 at 16:19
  • I changed the index to 0. However, the value e.Row.Cells[0].BackColor is still Name=0, ARGB=(0,0,0,0) – Tristan Descartes Apr 10 '14 at 16:27
  • What happens if you add a breakpoint at that line. What is the value inside row["Color"], what is the result of `System.Drawing.Color.FromName(row["Color"].ToString())`, and what is the result in BackColor after that line is executed? – Martin Mulder Apr 10 '14 at 16:33
  • At that line the value is being set to 0, i.e. an unknown color. This leads to me to think that I am pulling the data incorrectly from the datatable – Tristan Descartes Apr 10 '14 at 16:38
  • At this moment I cannot help you with that since I cannot see the relation between your gridview and your datable. I mean: I cannot see how they are bound. – Martin Mulder Apr 10 '14 at 16:40
  • I am going to look into this, but do you see anything wrong with the code currently apart from the correction you made earlier? – Tristan Descartes Apr 10 '14 at 16:42
  • Did you modify it? I was sure your line said: `dtMyList.Rows[i]["Color"]`. Is your iteration variable `i` used anywehre? – Martin Mulder Apr 10 '14 at 16:47
  • 1
    Well... in that case my recommendation would be: Remove the for-line. Or use my code example :) – Martin Mulder Apr 10 '14 at 16:54
1

You're looping through the entire table and setting the color for each row. I think you want something like

protected void grdMyList_RowDataBound(object sender, GridViewEventsArgs e)
{
    DataTable dtMyList = (DataTable)ViewState["MyList"];

    index i = e.Row.RowIndex;
    e.Row.Cells[0].BackColor = System.Drawing.Color.FromName(Convert.ToString(dtMyList.Rows[i]["Color"]));

}
D Stanley
  • 149,601
  • 11
  • 178
  • 240