Solved

Dynamically creation of DataGridviewButtonColumn

Posted on 2010-08-22
15
1,584 Views
Last Modified: 2013-12-17
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
Comment
Question by:ANINDYA
  • 8
  • 7
15 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 33498574
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
 

Author Comment

by:ANINDYA
ID: 33498609
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
 
LVL 42

Expert Comment

by:sedgwick
ID: 33498629
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 42

Expert Comment

by:sedgwick
ID: 33498634
@Anindyano need to call me Sir, sedgwick would be enough.i'll help you anyway... :)
0
 

Author Comment

by:ANINDYA
ID: 33498811
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
 

Author Comment

by:ANINDYA
ID: 33507714
Expert sedgwick
I am waiting for your tips.
If you have a trifle bit of time please glance upon .
Thanking you,
Anindya
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 33508096
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
 

Author Comment

by:ANINDYA
ID: 33508171
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
 
LVL 42

Expert Comment

by:sedgwick
ID: 33508177
you didn't use my code...
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 33508178
change the line which triggers the error to this one:
DataGridViewButtonCell btnCell = (DataGridViewButtonCell)dr.Cells["RateStatus"];
0
 

Author Comment

by:ANINDYA
ID: 33508239
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
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 33508292
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
 

Author Closing Comment

by:ANINDYA
ID: 33508365
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
 
LVL 42

Expert Comment

by:sedgwick
ID: 33508389
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
 

Author Comment

by:ANINDYA
ID: 33508775
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that undeā€¦
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question