Solved

Dynamically creation of DataGridviewButtonColumn

Posted on 2010-08-22
15
1,549 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 42

Expert Comment

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

Author Comment

by:ANINDYA
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:ANINDYA
Comment Utility
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
Comment Utility
you didn't use my code...
0
 
LVL 42

Expert Comment

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

Author Comment

by:ANINDYA
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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 connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now