Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Dynamically creation of DataGridviewButtonColumn

Posted on 2010-08-22
15
Medium Priority
?
1,684 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
15 Comments
 
LVL 42

Expert Comment

by:Meir Rivkin
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:Meir Rivkin
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 42

Expert Comment

by:Meir Rivkin
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:Meir Rivkin
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:Meir Rivkin
ID: 33508177
you didn't use my code...
0
 
LVL 42

Expert Comment

by:Meir Rivkin
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:
Meir Rivkin earned 2000 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:Meir Rivkin
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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

618 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