Solved

Dynamically creation of DataGridviewButtonColumn

Posted on 2010-08-22
15
1,568 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
 
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to get the web content with different encodings 2 28
Export import database 4 43
C# Json POSt to Rest API 4 37
C#.NET and microsoft certification. 3 34
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

895 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

18 Experts available now in Live!

Get 1:1 Help Now