Question : Dynamically creation of DataGridviewButtonColumn

Experts
Please check the image attached.

or see the code attached

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.text to be LOW if
                                //if case is from 0 to 5 else if rate is from 6 to 10 it should be  HIGH
                                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;
                               


 
                        }
                    }
                }
            }
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
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.text to be LOW if 
                                //if case is from 0 to 5 else if rate is from 6 to 10 it should be  HIGH
                                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;
                                


 
                        }
                    }
                }
            }
Attachments:
 
 
 
 
 
 

Answer : Dynamically creation of DataGridviewButtonColumn

take this function as is and replace it with yours and it will work.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
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";
                        }
                    }
                }
            }
        }
Random Solutions  
 
programming4us programming4us