Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cannot set backcolor of cells in datagridview

Status
Not open for further replies.

Darkpoem

Programmer
Nov 5, 2007
3
SE
Hi I have this strange problem concerning datagridview.
I have build an aplication which has a datagridview in which I add data stored in a file. When the application starts up I call a function that adds data to the datagridview and after that I change the color of specific cells in the datagridview like this:

public Form1()
{
//default function added by the application
//all initialization of buttons datagrids and so on are set here.
InitializeComponent();

//Add data to the grid, reads data from file and adds them
AddDataToTable(myDataGridView);

//Change color of cells in datagridview
ChangeColorOfData(myDataGridView);
}

The data is set just fine but the color won't change,
I use the following to set the backcolor:
myDataGridView.Rows.Cells[k].Style.BackColor = Color.DeepSkyBlue;

But if the user later interacts with the application and presses a button which calls the ChangeColorOfData function the datagridview cells updates, why does this happen???
Why won't it change during startup.
I have the same problem with the height of the cells, but I solved it by setting the rowtemplate.Height for the datagridView. Seems that the datagridview takes default values and won't update any changes that will change a default value... but they are update if I for example press a button as this:

private void Form1_Resize(object sender, EventArgs e)
{
ChangeColorOfData(myDataGridView);
}

Can someone help me?

 
I'm guessing here, but have you tried putting the AddData and ChangeColorOfData calls in the form's Load event rather than in the constructor?

You should also post the complete function that is not working, and any relevant details about the DataGridView in question.

Hope this helps,

Alex


[small]----signature below----[/small]
Now you can go where the people are one!
Now you can go where they get things done!
 
Hmmm I tried to create my own datagridview instead of using the application generated one. I feel so unlucky.. no difference. I really need some help now, it feels that it cannot be solved.

myDataGridView is defined as:
public partial class Form1 : Form
{
DataGridView myDataGridView = new DataGridView();
...

Following code is executed when application is started:
public Form1()
{
InitializeComponent();
MyOwnInitialize();
AddDataToDataGridView();
ChangeColorOfDataGridView();
...


In MyOwnInitialize() the following is made:

private void MyOwnInitialize()
{
myDataGridView.Visible = false;
myDataGridView.Size = new Size(636, 462);
myDataGridView.Location = new System.Drawing.Point(3, 42);
myDataGridView.ColumnCount = 0;
DataGridViewCellStyle columnHeaderStyle =
new DataGridViewCellStyle();
columnHeaderStyle.BackColor = Color.Aqua;
columnHeaderStyle.Font =
new Font("Verdana", 10, FontStyle.Bold);
myDataGridView.ColumnHeadersDefaultCellStyle =
columnHeaderStyle;
myDataGridView.RowTemplate.Height = 18;

this.tabPage3.Controls.Add(myDataGridView);
}

The following code adds data to myDataGridView:
private void AddDataToDataGridView()
{
...
DataTable dt = CreateDataTable(inArray);
//Add data to table, one time for each row
for (int i = 0; i < egenskapsListan.Length; ++i)
{
AddDataToTableForSpelUtveckling(weeks, checkedListBoxEgenskap.CheckedItems.ToString(), egenskapsListan, ref dt);
}

dt.DefaultView.Sort = "Eg/Vec ASC";

myDataGridView.DataSource = null;
myDataGridView.Rows.Clear();
myDataGridView.Refresh();
myDataGridView.Columns.Clear();
myDataGridView.DataSource = dt;
}

and finally the code I am trying to use to set the backcolor of one single element:

private void ChangeColorOfDataGridView()
{
if (myDataGridView.RowCount > 2)
{
DataGridViewCellStyle cellStyle = new DataGridViewCellStyle();

cellStyle.BackColor = Color.DeepSkyBlue;
myDataGridView.Rows[2].Cells[3].Style.ApplyStyle(cellStyle);
//myDataGridView.Rows[2].Cells[3].Style.BackColor = Color.DeepSkyBlue;
myDataGridView.Refresh();
myDataGridView.Visible = true;
}
}

If a button is pressed in the application the function ChangeColorOfDataGridView() is called and then the backcolor is changed!!!! I have checked the code when starting up and the function ChangeColorOfDataGridView is called and the color is set but nothing happens...
 
In the dataGridView all formating should be done in the
Code:
private void MyGrid_CellFormatting(object sender, 
                     DataGridViewCellFormattingEventArgs e)

so an exmple might be, change the colour of a cell if year less than 2000?
Code:
private void MyGrid_CellFormatting(object sender,
      DataGridViewCellFormattingEventArgs e)

{

   if (this.MyDataGrid.Columns[e.ColumnIndex].Name == "Year")

   {
      string year = (string)e.Value;

      if (Int32.Parse(year) < 2000)
      {
          e.CellStyle.ForeColor = Color.Red;
          e.CellStyle.SelectionForeColor = Color.Red;

          // Indicate that event was handled
          e.FormattingApplied = true;
      }
   }

Users can now change things but this even will always be called and updated

NOT TESTED!!!!!!


Age is a consequence of experience
 
Thx for the help but I found out a way that solved the problem. I have an application with tabpages and in the From1_load function I added:

tabControl1.SelectTab(2);
tabControl1.SelectTab(3);
tabControl1.SelectTab(0);

When the tabpage was selected in the form1_load function then the coloring suddenly worked at startup!!!!
The datagridview was perhaps not visible if the tabpage wasn't shown, I had datagridviews in tabpage 2 and 3. over and out!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top