Link to home
Start Free TrialLog in
Avatar of ANINDYA
ANINDYAFlag for India

asked on

DatagridviewColumnButton Color setting

Experts
Please see the attached image and tell me how to set a specific color to specific datagridviewcoloumButton.
I am using VS 2008
Thanking you
Anindya
error.JPG
Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

check this examlpe changing color:DataGridViewCellStyle currentIDCellStyle = new DataGridViewCellStyle(currentRow.Cells["ID"].Style);currentIDCellStyle.BackColor = FormUtilities.editedRowIDCellColor;currentIDCellStyle.SelectionBackColor = FormUtilities.editedRowIDCellSelectionColor;currentRow.Cells["ID"].Style = currentIDCellStyle;(http://www.mycsharp.de/wbb2/thread.php?postid=149991)
to set the coloumheader button in datagridview, chec this example:   // Set the column default header style.        DataGridViewCellStyle columnHeaderStyle =            new DataGridViewCellStyle();        columnHeaderStyle.BackColor = Color.Aqua;        columnHeaderStyle.Font =            new Font("Verdana", 10, FontStyle.Bold);        dataGridView.ColumnHeadersDefaultCellStyle =            columnHeaderStyle;//set specfic coloum header styleDataGridViewColumn column =            dataGridView.Columns[0];        DataGridViewCell cell = new DataGridViewTextBoxCell();        cell.Style.BackColor = Color.Wheat;cell.Style.ForeColor = Color.Green;        column.CellTemplate = cell;
hi
Are you creating a Bar Graph Column?
if you are try to see this link;
http://www.codeproject.com/KB/webforms/graphdatagrid.aspx

And if your not,  can you expound or elaborate your problem.
I think "sedgwick"  got the solution,  but if that's not, please expound or elaborate again.
Avatar of ANINDYA

ASKER

Expert sedgwick
Thank for the response.
Sir I  am not getting the required result as I am perhaps using it in the wrong way..
please see the code below.....
Can you please suggest me what I have to write inside the
case block so that only the 1st datagridview button should become colored.


private void Form1_Load(object sender, EventArgs e)
        {
            show_reminders();
           
            using (SqlConnection con = new SqlConnection(Connectionstring.Connection))
            {
               
                string query = "select Opportunity_Title as [Name of Opportunity],Rate as [Rate]  from Table_Opportunity";
                using (SqlDataAdapter ad = new SqlDataAdapter(query, con))
                {
                    DataSet ds = new DataSet();
                    ad.Fill(ds, "Table_Opportunity");
                    dataGridView1.DataSource = ds.Tables[0];
                    //setButtonColor(ds.Tables[0]);
                    setcolor(ds.Tables[0], e);
                }
            }

           
           
        }

        private void setcolor(DataTable dataTable, EventArgs e)
        {
            foreach (DataRow row in dataTable.Rows)
            {
                foreach (DataColumn col in dataTable.Columns)
                {
                    //dataGridView1.Columns.IndexOf[
                    for (Int32 i = 0; i <= 3; i++)
                    {


                        switch (row[col].ToString())
                        {
                            case "0": dataGridView1.CurrentRow.Cells[0].Style.ForeColor = Color.Green;
                                break;
                            case "1":
                                dataGridView1.CurrentRow.Cells[0].Style.ForeColor = Color.Green;
                                break;
                            case "2": dataGridView1.CurrentRow.Cells[0].Style.ForeColor = Color.Green;
                                break;
                            case "3":
                                DataGridViewCellStyle currentIDCellStyle = new DataGridViewCellStyle(row.Cells[0].Style);

                                //currentIDCellStyle.BackColor = dataGridView1.editedRowIDCellColor;
                                //currentIDCellStyle.SelectionBackColor = dataGridView1.editedRowIDCellSelectionColor;

                                currentRow.Cells["ID"].Style = currentIDCellStyle;
                                break;
                            case "4": dataGridView1.CurrentRow.Cells[0].Style.ForeColor = Color.Green;
                                break;
                            case "5": dataGridView1.CurrentRow.Cells[0].Style.ForeColor = Color.Green;
                                break;
                            case "6": dataGridView1.CurrentRow.Cells[0].Style.ForeColor = Color.Green;
                                break;
                            case "7": dataGridView1.CurrentRow.Cells[0].Style.ForeColor = Color.Green;
                                break;
                            case "8": dataGridView1.CurrentRow.Cells[0].Style.ForeColor = Color.Green;
                                break;
                            case "9": dataGridView1.CurrentRow.Cells[0].Style.ForeColor = Color.Green;
                                break;
                            case "10": dataGridView1.CurrentRow.Cells[0].Style.ForeColor = Color.Green;
                                break;
                            default:
                                break;
                        }
                    }
                }
            }
        }
       

Thanking you
Thanks for the response.
Anindya
private void Form1_Load(object sender, EventArgs e)
        {
            show_reminders();
            
            using (SqlConnection con = new SqlConnection(Connectionstring.Connection))
            {
                
                string query = "select Opportunity_Title as [Name of Opportunity],Rate as [Rate]  from Table_Opportunity";
                using (SqlDataAdapter ad = new SqlDataAdapter(query, con))
                {
                    DataSet ds = new DataSet();
                    ad.Fill(ds, "Table_Opportunity");
                    dataGridView1.DataSource = ds.Tables[0];
                    //setButtonColor(ds.Tables[0]);
                    setcolor(ds.Tables[0], e);
                }
            }

            
            
        }

        private void setcolor(DataTable dataTable, EventArgs e)
        {
            foreach (DataRow row in dataTable.Rows)
            {
                foreach (DataColumn col in dataTable.Columns)
                {
                    //dataGridView1.Columns.IndexOf[
                    for (Int32 i = 0; i <= 3; i++)
                    {


                        switch (row[col].ToString())
                        {
                            case "0": dataGridView1.CurrentRow.Cells[0].Style.ForeColor = Color.Green;
                                break;
                            case "1":
                                dataGridView1.CurrentRow.Cells[0].Style.ForeColor = Color.Green;
                                break;
                            case "2": dataGridView1.CurrentRow.Cells[0].Style.ForeColor = Color.Green;
                                break;
                            case "3":
                                DataGridViewCellStyle currentIDCellStyle = new DataGridViewCellStyle(row.Cells[0].Style);

                                //currentIDCellStyle.BackColor = dataGridView1.editedRowIDCellColor;
                                //currentIDCellStyle.SelectionBackColor = dataGridView1.editedRowIDCellSelectionColor;

                                currentRow.Cells["ID"].Style = currentIDCellStyle; 
                                break;
                            case "4": dataGridView1.CurrentRow.Cells[0].Style.ForeColor = Color.Green;
                                break;
                            case "5": dataGridView1.CurrentRow.Cells[0].Style.ForeColor = Color.Green;
                                break;
                            case "6": dataGridView1.CurrentRow.Cells[0].Style.ForeColor = Color.Green;
                                break;
                            case "7": dataGridView1.CurrentRow.Cells[0].Style.ForeColor = Color.Green;
                                break;
                            case "8": dataGridView1.CurrentRow.Cells[0].Style.ForeColor = Color.Green;
                                break;
                            case "9": dataGridView1.CurrentRow.Cells[0].Style.ForeColor = Color.Green;
                                break;
                            case "10": dataGridView1.CurrentRow.Cells[0].Style.ForeColor = Color.Green;
                                break;
                            default:
                                break;
                        }
                    }
                }
            }
        }
        

Open in new window

ANINDYA;
Is it fixed?
"Sedgwick" is not listening for this time
>>1st datagridview button should become colored.

do you mean the first column header button, right?
Avatar of ANINDYA

ASKER

Expert sedgwick and Expert Systan
I am extremely sorry for not responding to you at the specified time .
Actually it was weekends in India so I had not come to office.

Experts here is what I am trying but not getting

using (SqlConnection con = new SqlConnection(Connectionstring.Connection))
            {

                string query = "select Opportunity_Title as [Name of Opportunity],Rate as [Rate],Opportunity_ID  from Table_Opportunity";
                using (SqlDataAdapter ad = new SqlDataAdapter(query, con))
                {
                    DataSet ds = new DataSet();
                    ad.Fill(ds, "Table_Opportunity");
                    dataGridView1.DataSource = ds.Tables[0];                                      
                    foreach (DataRow dr in ds.Tables["Table_Opportunity"].Rows)
                    {
                        int rate = 0;
                        rate = Convert.ToInt32(dr["Rate"].ToString());
                        switch (rate)
                        {
                            case 0:
                                DataGridViewButtonColumn dgvbtncol = new DataGridViewButtonColumn();
                                //Experts here I have created dgvbtncol now I want to set the dgvbtncol. color  to be as I am going to specify. but I am not getting what code to write so that the button should get the color
                                break;
                            case 1:
                                DataGridViewButtonColumn DataGridViewButtonColumn dgvbtncol = new DataGridViewButtonColumn();
                                break;
                            case 2:
                                DataGridViewButtonColumn dgvbtncol = new DataGridViewButtonColumn();
                                break;
                            case 3:
                                DataGridViewButtonColumn dgvbtncol = new DataGridViewButtonColumn();
                                break;
                            case 4:
                                DataGridViewButtonColumn dgvbtncol = new DataGridViewButtonColumn();
                                break;
                            case 5:
                                DataGridViewButtonColumn dgvbtncol = new DataGridViewButtonColumn();
                                break;
                            case 6:
                                DataGridViewButtonColumn dgvbtncol = new DataGridViewButtonColumn();
                                break;
                            case 7:
                                DataGridViewButtonColumn dgvbtncol = new DataGridViewButtonColumn();
                                break;
                            case 8:
                                DataGridViewButtonColumn dgvbtncol = new DataGridViewButtonColumn();
                                break;
                            case 9:
                                DataGridViewButtonColumn dgvbtncol = new DataGridViewButtonColumn();
                                break;
                            case 10:
                                DataGridViewButtonColumn dgvbtncol = new DataGridViewButtonColumn();
                                break;
                            default:
                                MessageBox.Show("Please check there is an Opportunity which has no Rate stated.");
                                break;
                               


 
                        }
                    }
                }
            }

Experts here I have created dgvbtncol now I want to set the dgvbtncol. color  to be as I am going to specify. but I am not getting what code to write so that the button should get the color
Avatar of ANINDYA

ASKER

Experts
Please see the attached image here also.
Thanking you
Anindya
error.JPG
error.JPG
Avatar of ANINDYA

ASKER

Expert sedgwick

please glance upon this code and see what is going wrong

 private void populate_opportunityDTGDV()
        {
            using (SqlConnection con = new SqlConnection(Connectionstring.Connection))
            {

                string query = "select Opportunity_Title as [Name of Opportunity],Rate as [Rate],Opportunity_ID  from Table_Opportunity";
                using (SqlDataAdapter ad = new SqlDataAdapter(query, con))
                {
                    DataSet ds = new DataSet();
                    ad.Fill(ds, "Table_Opportunity");
                    dataGridView1.DataSource = ds.Tables[0];

                    if (dataGridView1.Columns["RateStatus"] == null)
                    {
                        DataGridViewButtonColumn col = new DataGridViewButtonColumn();
                        col.HeaderText = "RateStatus";
                        col.Name = "RateStatus";
                        dataGridView1.Columns.Add(col);
                    }
                   
                   
                    foreach (DataGridViewRow dr in dataGridView1.Rows.Cast<DataGridViewRow>())
                    {
                        DataGridViewButtonCell btnCell = (DataGridViewButtonCell)dr.Cells["RateStatus"];
                        if (dr.Cells["Rate"].Value != null)
                        {
                            btnCell.Value = int.Parse(dr.Cells["Rate"].Value.ToString()) <= 5 ? "Low" : "High";
                                                                                   
                        }
                    }
                    if (dataGridView1.Columns["BusinessProspect"] == null)
                    {
                        DataGridViewButtonColumn col2 = new DataGridViewButtonColumn();
                        col2.HeaderText = "BusinessProspect";
                        col2.Name = "BusinessProspect";
                        dataGridView1.Columns.Add(col2);
                    }
                    foreach (DataGridViewRow dr in dataGridView1.Rows.Cast<DataGridViewRow>())
                    {
                        DataGridViewButtonCell btnCell = (DataGridViewButtonCell)dr.Cells["BusinessProspect"];
                        if (dr.Cells["Rate"].Value != null)
                        {
                            int BusinessProspectRate = 0;
                            BusinessProspectRate = int.Parse(dr.Cells["Rate"].Value.ToString());


                            switch (BusinessProspectRate)
                            {
                                case 1:
                                    //DataGridViewCell cell = new DataGridViewTextBoxCell();

                                    btnCell.Style.BackColor = Color.Pink;
                                    //cell.Style.ForeColor = Color.Green;
                                    //column.CellTemplate = cell;


                                    //DataGridViewCellStyle columnHeaderStyle = new DataGridViewCellStyle();
                                    //columnHeaderStyle.BackColor = Color.Aqua;

                                    //columnHeaderStyle.Font =
                                    //    new Font("Verdana", 10, FontStyle.Bold);
                                    //dataGridView1.ColumnHeadersDefaultCellStyle = columnHeaderStyle;
                                    break;
                                //case 2:
                                //    DataGridViewCellStyle columnHeaderStyle = new DataGridViewCellStyle();
                                //    columnHeaderStyle.BackColor = Color.Aqua;
                                //    columnHeaderStyle.Font =
                                //        new Font("Verdana", 10, FontStyle.Bold);
                                //    dataGridView1.ColumnHeadersDefaultCellStyle = columnHeaderStyle;
                                //    break;
                                //case 3:
                                //    DataGridViewCellStyle columnHeaderStyle = new DataGridViewCellStyle();
                                //    columnHeaderStyle.BackColor = Color.Aqua;

                                //    columnHeaderStyle.Font =
                                //        new Font("Verdana", 10, FontStyle.Bold);
                                //    dataGridView1.ColumnHeadersDefaultCellStyle = columnHeaderStyle;
                                //    break;
                                //case 4:
                                //    DataGridViewCellStyle columnHeaderStyle = new DataGridViewCellStyle();
                                //    columnHeaderStyle.BackColor = Color.Aqua;

                                //    columnHeaderStyle.Font =
                                //        new Font("Verdana", 10, FontStyle.Bold);
                                //    dataGridView1.ColumnHeadersDefaultCellStyle = columnHeaderStyle;
                                //    break;
                                //case 5:
                                //    DataGridViewCellStyle columnHeaderStyle = new DataGridViewCellStyle();
                                //    columnHeaderStyle.BackColor = Color.Aqua;

                                //    columnHeaderStyle.Font =
                                //        new Font("Verdana", 10, FontStyle.Bold);
                                //    dataGridView1.ColumnHeadersDefaultCellStyle = columnHeaderStyle;
                                //    break;
                                //case 6:
                                //    DataGridViewCellStyle columnHeaderStyle = new DataGridViewCellStyle();
                                //    columnHeaderStyle.BackColor = Color.Aqua;

                                //    columnHeaderStyle.Font =
                                //        new Font("Verdana", 10, FontStyle.Bold);
                                //    dataGridView1.ColumnHeadersDefaultCellStyle = columnHeaderStyle;
                                //    break;
                                //case 7:
                                //    DataGridViewCellStyle columnHeaderStyle = new DataGridViewCellStyle();
                                //    columnHeaderStyle.BackColor = Color.Aqua;

                                //    columnHeaderStyle.Font =
                                //        new Font("Verdana", 10, FontStyle.Bold);
                                //    dataGridView1.ColumnHeadersDefaultCellStyle = columnHeaderStyle;
                                //    break;
                                //case 8:
                                //    DataGridViewCellStyle columnHeaderStyle = new DataGridViewCellStyle();
                                //    columnHeaderStyle.BackColor = Color.Aqua;

                                //    columnHeaderStyle.Font =
                                //        new Font("Verdana", 10, FontStyle.Bold);
                                //    dataGridView1.ColumnHeadersDefaultCellStyle = columnHeaderStyle;
                                //    break;
                                //case 9:
                                //    DataGridViewCellStyle columnHeaderStyle = new DataGridViewCellStyle();
                                //    columnHeaderStyle.BackColor = Color.Aqua;

                                //    columnHeaderStyle.Font =
                                //        new Font("Verdana", 10, FontStyle.Bold);
                                //    dataGridView1.ColumnHeadersDefaultCellStyle = columnHeaderStyle;
                                //    break;
                                //case 10:
                                //    DataGridViewCellStyle columnHeaderStyle = new DataGridViewCellStyle();
                                //    columnHeaderStyle.BackColor = Color.Aqua;

                                //    columnHeaderStyle.Font =
                                //        new Font("Verdana", 10, FontStyle.Bold);
                                //    dataGridView1.ColumnHeadersDefaultCellStyle = columnHeaderStyle;
                                //    break;
                                default:
                                    break;

                            }

                        }
                    }
                }
            }
        }

private void populate_opportunityDTGDV()
        {
            using (SqlConnection con = new SqlConnection(Connectionstring.Connection))
            {

                string query = "select Opportunity_Title as [Name of Opportunity],Rate as [Rate],Opportunity_ID  from Table_Opportunity";
                using (SqlDataAdapter ad = new SqlDataAdapter(query, con))
                {
                    DataSet ds = new DataSet();
                    ad.Fill(ds, "Table_Opportunity");
                    dataGridView1.DataSource = ds.Tables[0];

                    if (dataGridView1.Columns["RateStatus"] == null)
                    {
                        DataGridViewButtonColumn col = new DataGridViewButtonColumn();
                        col.HeaderText = "RateStatus";
                        col.Name = "RateStatus";
                        dataGridView1.Columns.Add(col);
                    }
                    
                    
                    foreach (DataGridViewRow dr in dataGridView1.Rows.Cast<DataGridViewRow>())
                    {
                        DataGridViewButtonCell btnCell = (DataGridViewButtonCell)dr.Cells["RateStatus"];
                        if (dr.Cells["Rate"].Value != null)
                        {
                            btnCell.Value = int.Parse(dr.Cells["Rate"].Value.ToString()) <= 5 ? "Low" : "High";
                                                                                    
                        }
                    }
                    if (dataGridView1.Columns["BusinessProspect"] == null)
                    {
                        DataGridViewButtonColumn col2 = new DataGridViewButtonColumn();
                        col2.HeaderText = "BusinessProspect";
                        col2.Name = "BusinessProspect";
                        dataGridView1.Columns.Add(col2);
                    }
                    foreach (DataGridViewRow dr in dataGridView1.Rows.Cast<DataGridViewRow>())
                    {
                        DataGridViewButtonCell btnCell = (DataGridViewButtonCell)dr.Cells["BusinessProspect"];
                        if (dr.Cells["Rate"].Value != null)
                        {
                            int BusinessProspectRate = 0;
                            BusinessProspectRate = int.Parse(dr.Cells["Rate"].Value.ToString());


                            switch (BusinessProspectRate)
                            {
                                case 1:
                                    //DataGridViewCell cell = new DataGridViewTextBoxCell();

                                    btnCell.Style.BackColor = Color.Pink;
                                    //cell.Style.ForeColor = Color.Green;
                                    //column.CellTemplate = cell;


                                    //DataGridViewCellStyle columnHeaderStyle = new DataGridViewCellStyle();
                                    //columnHeaderStyle.BackColor = Color.Aqua;

                                    //columnHeaderStyle.Font =
                                    //    new Font("Verdana", 10, FontStyle.Bold);
                                    //dataGridView1.ColumnHeadersDefaultCellStyle = columnHeaderStyle;
                                    break;
                                //case 2:
                                //    DataGridViewCellStyle columnHeaderStyle = new DataGridViewCellStyle();
                                //    columnHeaderStyle.BackColor = Color.Aqua;
                                //    columnHeaderStyle.Font =
                                //        new Font("Verdana", 10, FontStyle.Bold);
                                //    dataGridView1.ColumnHeadersDefaultCellStyle = columnHeaderStyle;
                                //    break;
                                //case 3:
                                //    DataGridViewCellStyle columnHeaderStyle = new DataGridViewCellStyle();
                                //    columnHeaderStyle.BackColor = Color.Aqua;

                                //    columnHeaderStyle.Font =
                                //        new Font("Verdana", 10, FontStyle.Bold);
                                //    dataGridView1.ColumnHeadersDefaultCellStyle = columnHeaderStyle;
                                //    break;
                                //case 4:
                                //    DataGridViewCellStyle columnHeaderStyle = new DataGridViewCellStyle();
                                //    columnHeaderStyle.BackColor = Color.Aqua;

                                //    columnHeaderStyle.Font =
                                //        new Font("Verdana", 10, FontStyle.Bold);
                                //    dataGridView1.ColumnHeadersDefaultCellStyle = columnHeaderStyle;
                                //    break;
                                //case 5:
                                //    DataGridViewCellStyle columnHeaderStyle = new DataGridViewCellStyle();
                                //    columnHeaderStyle.BackColor = Color.Aqua;

                                //    columnHeaderStyle.Font =
                                //        new Font("Verdana", 10, FontStyle.Bold);
                                //    dataGridView1.ColumnHeadersDefaultCellStyle = columnHeaderStyle;
                                //    break;
                                //case 6:
                                //    DataGridViewCellStyle columnHeaderStyle = new DataGridViewCellStyle();
                                //    columnHeaderStyle.BackColor = Color.Aqua;

                                //    columnHeaderStyle.Font =
                                //        new Font("Verdana", 10, FontStyle.Bold);
                                //    dataGridView1.ColumnHeadersDefaultCellStyle = columnHeaderStyle;
                                //    break;
                                //case 7:
                                //    DataGridViewCellStyle columnHeaderStyle = new DataGridViewCellStyle();
                                //    columnHeaderStyle.BackColor = Color.Aqua;

                                //    columnHeaderStyle.Font =
                                //        new Font("Verdana", 10, FontStyle.Bold);
                                //    dataGridView1.ColumnHeadersDefaultCellStyle = columnHeaderStyle;
                                //    break;
                                //case 8:
                                //    DataGridViewCellStyle columnHeaderStyle = new DataGridViewCellStyle();
                                //    columnHeaderStyle.BackColor = Color.Aqua;

                                //    columnHeaderStyle.Font =
                                //        new Font("Verdana", 10, FontStyle.Bold);
                                //    dataGridView1.ColumnHeadersDefaultCellStyle = columnHeaderStyle;
                                //    break;
                                //case 9:
                                //    DataGridViewCellStyle columnHeaderStyle = new DataGridViewCellStyle();
                                //    columnHeaderStyle.BackColor = Color.Aqua;

                                //    columnHeaderStyle.Font =
                                //        new Font("Verdana", 10, FontStyle.Bold);
                                //    dataGridView1.ColumnHeadersDefaultCellStyle = columnHeaderStyle;
                                //    break;
                                //case 10:
                                //    DataGridViewCellStyle columnHeaderStyle = new DataGridViewCellStyle();
                                //    columnHeaderStyle.BackColor = Color.Aqua;

                                //    columnHeaderStyle.Font =
                                //        new Font("Verdana", 10, FontStyle.Bold);
                                //    dataGridView1.ColumnHeadersDefaultCellStyle = columnHeaderStyle;
                                //    break;
                                default:
                                    break;

                            }

                        }
                    }
                }
            }
        }

Open in new window

you want the button itself colored, or the background of the cell of the gridview be colored?

Untitled.jpg
Avatar of ANINDYA

ASKER

button to be colored
ASKER CERTIFIED SOLUTION
Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ANINDYA

ASKER

Expert sedgwick
I have no word to express my gratitude towards you.
My problems are solved .
But the most important fact is the way you have done so is amazing as it is too in midnight hours of your country.

I will be grateful to you if you provide me your email id so that next time when I will put any query in EE I can provide you the URL and thereafter it is your wish to respond or not to do so.
But at least I want to provide you the URL.

If you think I am worth so then please provide me your email id . my email id is   anindya002@gmail.com

I do assure you I will not disturb you by any other means .
Only URL s of the EE  I will forward to you.
Please kindly consider my request.

In last 6 months I have not seen an expert like you who has rendered so much of help to me.

Any way Thanking you for your benevolence towards me.
Anindya Chatterjee
Bangalore
India  
dear Anindya

thank you for your warm gratitude.
it's not necessary for you to email me when you put new question on EE cause i'm tracking down all new questions which are in my favorite zones (C# is one of many).
i'll make sure to provide you a fast and proper solution to any of your future questions as well.
thanks again and take care.

-sedgwick
Avatar of ANINDYA

ASKER

Expert sedgwick
If possible please write a blog on EE on

1. How to connect ASP .net MVC to Sql server 2008.
Thanking you,
Anindya