• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1717
  • Last Modified:

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;
                               


 
                        }
                    }
                }
            }

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;
                                


 
                        }
                    }
                }
            }

Open in new window

error.JPG
error.JPG
error.JPG
0
ANINDYA
Asked:
ANINDYA
  • 8
  • 7
1 Solution
 
Meir RivkinFull stack Software EngineerCommented:
shouldn't you check the value of rate in the datagridview column, rather than in the datatable?does the datagridview is binded to to the datatable? so the Rate colum is already populated and all required is to add the DataGridViewButtonColumn and update Low/High text for each row?
0
 
ANINDYAAuthor Commented:
Expert sedgwick
Thanks for answering .
Sir it is in reference to my earlier post that is datagridviewcolumn button color .
Sir I am supposed to do both that is create a button and at the same time set the color for a button .
As you know that I asked for the color and now for rate.
Sir you provided me the code on friday .
But sir I am stuck here-----that is how to set it for each row by row .
I am trying but not getting.

Sir you have written
" all required is to add the DataGridViewButtonColumn and update Low/High text for each row?"

But still I am not been able to do that .
Would you please do a favour to me by providing a code for that .
I am actually a fresher so may be I am not catching you.
Thanking you ,
Anindya

0
 
Meir RivkinFull stack Software EngineerCommented:
basically, to create the extended column and add the cell buttons based on the rate value, this is the code you wanna use://first check if the column hasn't been created yet if (dataGridView1.Columns["colRateStatus"] == null)            {                DataGridViewButtonColumn col = new DataGridViewButtonColumn();                col.HeaderText = "RateStatus";                col.Name = "colRateStatus";                dataGridView1.Columns.Add(col);            }//run through all rows and set the cell button text based on the rate value            foreach (DataGridViewRow dr in dataGridView1.Rows.Cast())            {                DataGridViewButtonCell btnCell = (DataGridViewButtonCell) dr.Cells["colRateStatus"];                if (dr.Cells["colRate"].Value != null)                {                    btnCell.Value = (int)dr.Cells["colRate"].Value <= 5 ? "Low" : "High";                }            }
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Meir RivkinFull stack Software EngineerCommented:
@Anindyano need to call me Sir, sedgwick would be enough.i'll help you anyway... :)
0
 
ANINDYAAuthor Commented:
Expert sedgwick
One error is coming .
I have attached the screen shot of it please glance upon it.
Thanking you
Anindya
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["Rate"] == null)
                    {
                        DataGridViewButtonColumn col = new DataGridViewButtonColumn();
                        col.HeaderText = "Rate";
                        col.Name = "Rate";
                        dataGridView1.Columns.Add(col);
                    }

                    foreach (DataGridViewRow dr in dataGridView1.Rows.Cast<DataGridViewRow>())
                    {
                        DataGridViewButtonCell btnCell = (DataGridViewButtonCell)dr.Cells["Rate"];
                        if (dr.Cells["Rate"].Value != null)
                        {
                            btnCell.Value = (int)dr.Cells["Rate"].Value <= 5 ? "Low" : "High";
                        }
                    }
                }
            }
        }

Open in new window

error.JPG
0
 
ANINDYAAuthor Commented:
Expert sedgwick
I am waiting for your tips.
If you have a trifle bit of time please glance upon .
Thanking you,
Anindya
0
 
Meir RivkinFull stack Software EngineerCommented:
i modified your function, test it.
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)dr.Cells["Rate"].Value <= 5 ? "Low" : "High";
                        }
                    }
                }
            }
        }

Open in new window

0
 
ANINDYAAuthor Commented:
Expert sedgwick
Thanks for responding.
the error
Unable to cast object of type 'System.Windows.Forms.DataGridViewTextBoxCell' to type 'System.Windows.Forms.DataGridViewButtonCell'.

is still there.
Thanking you
Anindya
error.JPG
0
 
Meir RivkinFull stack Software EngineerCommented:
you didn't use my code...
0
 
Meir RivkinFull stack Software EngineerCommented:
change the line which triggers the error to this one:
DataGridViewButtonCell btnCell = (DataGridViewButtonCell)dr.Cells["RateStatus"];
0
 
ANINDYAAuthor Commented:
Expert sedgwick
please see the code which I am using .
It is your's only .
moreover please see the images attached also.
and code attached also.
Thanks for responding.
I am giving you the full code of the function here

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["Status"] == null)
                    {
                        DataGridViewButtonColumn col = new DataGridViewButtonColumn();
                        col.HeaderText = "Status";
                        col.Name = "Status";
                        dataGridView1.Columns.Add(col);
                    }

                    //foreach (DataGridViewRow dr in dataGridView1.Rows.Cast<DataGridViewRow>())
                    //{
                    //    //DataGridViewButtonCell btnCell = (DataGridViewButtonCell)dr.Cells["Rate"];
                    //    DataGridViewButtonCell btnCell = new DataGridViewButtonCell();
                    //    //if (dr.Cells["Rate"].Value != null)
                    //    //{
                    //    //    //btnCell.Value = (int)dr.Cells["Rate"].Value <= 5 ? "Low" : "High";
                    //    //    btnCell.ToolTipText =Convert.ToInt32( dr.Cells["Rate"].Value.ToString() )<= 5 ? "Low" : "High";

                    //    //}
                       

                    //}
                    foreach (DataGridViewRow dr in dataGridView1.Rows.Cast<DataGridViewRow>())
                    {
                        //DataGridViewButtonCell btnCell = (DataGridViewButtonCell)dr.Cells["Rate"];
                        DataGridViewButtonCell btnCell = (DataGridViewButtonCell)dr.Cells["RateStatus"];
                        if (dr.Cells["Rate"].Value != null)
                        {
                            btnCell.Value = (int)dr.Cells["Rate"].Value <= 5 ? "Low" : "High";
                        }
                    }
                }
            }
        }
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["Status"] == null)
                    {
                        DataGridViewButtonColumn col = new DataGridViewButtonColumn();
                        col.HeaderText = "Status";
                        col.Name = "Status";
                        dataGridView1.Columns.Add(col);
                    }

                    //foreach (DataGridViewRow dr in dataGridView1.Rows.Cast<DataGridViewRow>())
                    //{
                    //    //DataGridViewButtonCell btnCell = (DataGridViewButtonCell)dr.Cells["Rate"];
                    //    DataGridViewButtonCell btnCell = new DataGridViewButtonCell();
                    //    //if (dr.Cells["Rate"].Value != null)
                    //    //{
                    //    //    //btnCell.Value = (int)dr.Cells["Rate"].Value <= 5 ? "Low" : "High";
                    //    //    btnCell.ToolTipText =Convert.ToInt32( dr.Cells["Rate"].Value.ToString() )<= 5 ? "Low" : "High";

                    //    //}
                        

                    //}
                    foreach (DataGridViewRow dr in dataGridView1.Rows.Cast<DataGridViewRow>())
                    {
                        //DataGridViewButtonCell btnCell = (DataGridViewButtonCell)dr.Cells["Rate"];
                        DataGridViewButtonCell btnCell = (DataGridViewButtonCell)dr.Cells["Rate"];
                        if (dr.Cells["Rate"].Value != null)
                        {
                            btnCell.Value = (int)dr.Cells["Rate"].Value <= 5 ? "Low" : "High";
                        }
                    }
                }
            }
        }

Open in new window

error.JPG
1.JPG
0
 
Meir RivkinFull stack Software EngineerCommented:
take this function as is and replace it with yours and it will work.
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";
                        }
                    }
                }
            }
        }

Open in new window

0
 
ANINDYAAuthor Commented:
Expert sedgwick
Thanks for the debugging .
It is working fine .
I am too sorry that I have troubled you a lot.
hope to get your support in future.
I have to implement the code which you provided for the color of the cell .
I hope to get your help .
If I face any problem I will let you know.
I know in US it is midnight so I will not disturb you today anymore.
but in the morning when you will get up please have a glance if I posted any comment on your code on the setting of cell color.
Again thanks.
Anindya Chatterjee
Bangalore
India
 
0
 
Meir RivkinFull stack Software EngineerCommented:
don't worry about it cause i'm working 24 hrs a day :)
can u post the link to your question about the color?
0
 
ANINDYAAuthor Commented:
Expert sedgwick
Thanks for supporting me .
here is the link
http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_26414264.html#a33508766
you had given me some help with code
I have tried .
But sedgwick the cell is not getting colored.
the code is attached there in that link.
Thanking you
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 8
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now