Hello All:

Im using a dataGridView in unbound way, and would like to set the foreColor of the cell i just added based on info i fetch from my db table, here is my current code :-

if (cheque == true)
                            {
                                dgv.Rows.Add(date.ToString("dd ddd"), venue, fee,"chk");
                            }
                            else
                            {
                                dgv.Rows.Add(date.ToString("dd ddd"), venue, fee, "cash");
                            }

This routine sits right in the middle of an sqlReader looping through a Diary table.

There are 4 columns and id like to get rid of the 4th (chk/cash)and just set the 3rd 'fee' cell's foreColor to either red or green depending on weather its cash or cheque.

Can I set the foreColor of a cell I just added?

Any help appreciated

Tino

Recommended Answers

All 10 Replies

Modify your new rode with new style:

DataGridViewRow row = new DataGridViewRow();
            DataGridViewCellStyle style = new DataGridViewCellStyle();
            style.ForeColor = Color.Green; // the color change
            row.DefaultCellStyle = style;
            dataGridView1.Rows.Add(row);

Here you go:

//in designer:
 this.dataGridView1.RowsAdded += new System.Windows.Forms.DataGridViewRowsAddedEventHandler(this.dataGridView1_RowsAdded);

//in code:
   private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            if (cheque == true)
            {
                dataGridView1.Rows[e.RowIndex].Cells[2].Style.ForeColor = Color.Red;
            }
            else
            {
                dataGridView1.Rows[e.RowIndex].Cells[2].Style.ForeColor = Color.Green;
            }
        }

every time you call dataGridView1.Rows.Add it will fire the event and set the colour :)

Modify your new rode with new style:

DataGridViewRow row = new DataGridViewRow();
            DataGridViewCellStyle style = new DataGridViewCellStyle();
            style.ForeColor = Color.Green; // the color change
            row.DefaultCellStyle = style;
            dataGridView1.Rows.Add(row);

There is no need to create a new style as I demonstrated in that example. You can just set the default style:

DataGridViewRow row = new DataGridViewRow();
            row.DefaultCellStyle.ForeColor = Color.Green;
            dataGridView1.Rows.Add(row);

Also, the event handler that Ryshad gave is a more versatile approach and you could set the entire row's ForeColor too instead of individual cells if that is your intention.

Tried your version Ryshad but I get an index out of range error when dataGridView1 RowsAdded event fires.

Set a breakpoint and the RowCount = 1 & RowIndex = 0

Any ideas

Tino

Tried your version Ryshad but I get an index out of range error when dataGridView1 RowsAdded event fires.

Set a breakpoint and the RowCount = 1 & RowIndex = 0

Any ideas

Tino

Sorry about reading right over the "Cell" color designator with this thread. Anyway, check your Cell's indexer because the RowIndex looks good...

Like DdoubleD said, check the cell index, i saw that you were adding 3 pieces of data so i assumed that it was column 2 that needed changing. If you have headings on the columns you can call Row[e.RowIndex].Cells["ColumnName"] to be sure your changing the right column.

Like DdoubleD said, check the cell index, i saw that you were adding 3 pieces of data so i assumed that it was column 2 that needed changing. If you have headings on the columns you can call Row[e.RowIndex].Cells["ColumnName"] to be sure your changing the right column.

Ive tried another angle Ryshad and it worked for me :-

                        if (cheque == true)
                        {

                            dgv.Rows.Add(date.ToString("dd ddd"), venue, fee, "chk");
                            dgv.Rows[day - 1].Cells[2].Style.ForeColor = Color.Purple;
                        }

I've left your version commented out so i'll give it a little more time tonight.

Many thanks for your help & also DdoubleD

Tino

I'm not sure where you are getting 'day' from : / And i'd recomend taking the time to get my approach working..its a much more versatile method, and you know that the row you're editing is the row that you added. There may be conditions where your approach alters the wrong row.

Also, for future implementations you can do a lot in the event handlers: validation, styling, etc.

Good luck, whichever way you choose to go, and remember to mark the thread solved if you feel the question is answered :)

Yeah sorry, 'day' was in the Diary loop (1st post) but I kept the code concise for readability.

I will certainly get your version working on my box and I agree, it's a better all rounder :)

Thnx Tino

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.