Disable DataGridView CheckBox

Experts,

I`m using C# .NET 3.5 VS2008.

I have a List<> of Custom Objects. Which I`m using as a DataSource to a DataGridView. Is there any way I can disable CheckBoxes in the DataGridView based on the Values in the List<>?

Here is a very simplified Example:

Only when the Value of Good is True. I want to disable the CheckBox, so that no choice can be made.


public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            LOrange = new List<Orange>();
        }

        public class Orange
        {
            public string Name { get; set; }
            public bool Good{ get; set; }

            public Orange(string name, bool good)
            {
                Name = name;
                Good = good;   //If the Value is "True". I want to Disable the CheckBox
            }
        }

        private List<Orange> LOrange = null;

        private void button1_Click(object sender, EventArgs e)
        {
            Orange O1 = new Orange("Or12", false);
            Orange O2 = new Orange("Or23", true);

            LOrange.Add(O1);
            LOrange.Add(O2);

            dataGridView1.DataSource = LOrange;
        }

        private void button2_Click(object sender, EventArgs e)
        {
    
        }
    }

Open in new window

San24Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nishant joshiTechnology Development ConsultantCommented:
in a DatagridView bind checked value to Good
means
you gave to use
Checked=Eval('Good');
for checkbox.


Regards,
nishant
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
alrosmarzCommented:
in the databoud event of the gridview you can get each row and compare the data and set checked or unchecked the check box.. of curse u need to create the control in the grid.. like this

   <asp:TemplateField HeaderText="Header">
                                <ItemTemplate>
                                    <center>
                                        <asp:CheckBox ID="chkControl" runat="server" />
                                    </center>
                                </ItemTemplate>
                            </asp:TemplateField>

in code behind you can compare data with a for each row and get the checkbox

((CheckBox)gridView.Rows[IndexRow].Cells[NumberOfCell].FindControl("chkControl")).Checked = true or false

i hope this help you

cheers
0
Nitesh LuharukaDirectorCommented:
You need to handle the RowBound event and set the checked value for the checkbox based on the value of column Good.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

San24Author Commented:
I`m working on WinForms. The WinForms DataGridView doesn`t have a RowBound event. Is there an alternate event I could use?
0
San24Author Commented:
Also, I`m looking to Disable the Checkbox completely and not allowing the users to check or uncheck it.
0
San24Author Commented:
I think this might work for now :
for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                Orange O = (Orange)dataGridView1.Rows[i].DataBoundItem;

                if (O.Good == false)
                {
                    dataGridView1.Rows[i].Cells[0].ReadOnly = true;
                }
            }

Open in new window

0
Nitesh LuharukaDirectorCommented:
OK.. I think the approach you have used shall work. You shall use the code after binding the data to datagrid. Also, use the below line:
dataGridView1.Rows[i].Cells[0].Enabled = true;

Open in new window


instead of
dataGridView1.Rows[i].Cells[0].ReadOnly = true;

Open in new window

0
Nitesh LuharukaDirectorCommented:
0
San24Author Commented:
@Nitesh : Enabled is not a property of a DataGridViewCell - atleast in WinForms.
0
Nitesh LuharukaDirectorCommented:
@San...OOps...You can get the checkbox control by typecasting the call and then make it disable. I did it some time ago, and I will check if I still have that code.
0
San24Author Commented:
The code will be helpful!
0
nishant joshiTechnology Development ConsultantCommented:
this.Column1.DataPropertyName = "Good";  //column1 is your check box column then go to designer.cs and add this code


Regards,
nishant
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.