Click here to Skip to main content
15,914,165 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm implementing a function of two forms. While choosing any of datagridviewcheckboxcolumn in form1 and show form2 when click button to close , in the form1 datagridviewcheckboxcolumn will is false ?
Please help on this.

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        Form2 Frm2 = new Form2();
        public Form1()
        {
            InitializeComponent();
            DataGridViewCheckColumn();

            Frm2.newEvent += new FrmCheckbox(Frm2_Event);
           
        }
        public void DataGridViewCheckColumn()
        {

            dataGridView1.ColumnCount = 6;

            dataGridView1.AllowUserToAddRows = false;

            DataGridViewCheckBoxColumn colCB = new 

DataGridViewCheckBoxColumn();
            colCB.Name = "chkcol";
            colCB.HeaderText = "Select";
            colCB.Width = 40;
            dataGridView1.Columns.Add(colCB);


            dataGridView1.Columns[0].Name = "C0";
            dataGridView1.Columns[1].Name = "C1";
            dataGridView1.Columns[2].Name = "C2";
            dataGridView1.Columns[3].Name = "C3";
            dataGridView1.Columns[4].Name = "C4";
            dataGridView1.Columns[5].Name = "C5";

            dataGridView1.Columns[0].HeaderText = "C0";
            dataGridView1.Columns[1].HeaderText = "C1";
            dataGridView1.Columns[2].HeaderText = "C2";
            dataGridView1.Columns[3].HeaderText = "C3";
            dataGridView1.Columns[4].HeaderText = "C4";
            dataGridView1.Columns[5].HeaderText = "C5";

            dataGridView1.Columns[0].Width = 40;
            dataGridView1.Columns[1].Width = 40;
            dataGridView1.Columns[2].Width = 40;
            dataGridView1.Columns[3].Width = 40;
            dataGridView1.Columns[4].Width = 40;
            dataGridView1.Columns[5].Width = 40;


            dataGridView1.Rows.Add(new string[] { "A1", "B1", "C1", 

"D1", "E1", "F1" });
            dataGridView1.Rows.Add(new string[] { "A2", "B2", "C2", 

"D2", "E2", "F2" });
            dataGridView1.Rows.Add(new string[] { "A3", "B3", "C3", 

"D3", "E3", "F3" });
            dataGridView1.Rows.Add(new string[] { "A4", "B4", "C4", 

"D4", "E4", "F4" });
            dataGridView1.Rows.Add(new string[] { "A5", "B5", "C5", 

"D5", "E5", "F5" });
            dataGridView1.Rows.Add(new string[] { "A6", "B6", "C6", 

"D6", "E6", "F6" });
            dataGridView1.Rows.Add(new string[] { "A7", "B7", "C7", 

"D7", "E7", "F7" });




            dataGridView1.AutoResizeColumns();
        }

        void Frm2_Event()
        {
          
            //for (int i = 0; i <= dataGridView1.Rows.Count - 1; i++)
            //{

            //    if (Convert.ToBoolean(dataGridView1.Rows[i].Cells

["chkcol"]"Value) == true )
            //    {
            //        dataGridView1.Rows[i].Cells["chkcol"].Value = false ;
            //    }

            //}
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (Convert.ToBoolean(row.Cells["chkcol"].Value) == true)
                {
                    row.Cells["chkcol"].Value = false;
                }
            }
            
        }

        private void dataGridView1_CellContentClick(object sender, 

DataGridViewCellEventArgs e)
        {
             for (int i = 0; i <= dataGridView1.RowCount - 1; i++)
            {
                if (Convert.ToBoolean(dataGridView1.Rows

[e.RowIndex].Cells["chkcol"].Value = true))
                {
                    Frm2.ShowDialog();
                    break;
                }
            }
           
        }

    }
}

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

 namespace WindowsFormsApplication1
 {
     public delegate void FrmCheckbox();
     public partial class Form2 : Form
     {
         public event FrmCheckbox newEvent;
         public Form2()
         {
             InitializeComponent();
            //button1.Click +=new EventHandler (button1_Click );
         }

         private void button1_Click(object sender, EventArgs e)
         {
             if (newEvent != null)
             {
                 newEvent();

             }
         }

        

        
     }
 }
Posted
Comments
[no name] 19-Aug-13 13:07pm    
Huh? What?
Not clear. Please remove unnecessary codes.
[no name] 20-Aug-13 8:05am    
http://stackoverflow.com/questions/13338837/check-uncheck-a-checkbox-on-datagridview

Look at your code:
C#
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    for (int i = 0; i <= dataGridView1.RowCount - 1; i++)
    {
        if (Convert.ToBoolean(dataGridView1.Rows[e.RowIndex].Cells["chkcol"].Value = true))
        {
            Frm2.ShowDialog();
            break;
        }
    }
}
Are you sure you meant to say "= true"?

And why are you converting what should be a bool value to a bool value anyway?
And, just to rub it in a bit, what is the difference between:
C#
if (myBool == true)

and
C#
if (myBool)
Apart from the obvious "the first one is longer, less readable and prone to error"?
 
Share this answer
 
Comments
Rob Philpott 20-Aug-13 7:20am    
Looks like the bracket is in the wrong place and hence the conversion should be from object to bool. Maybe? I dunno.

My favourite:

bool y = x ? true : false;

which I see far too often!
I'm implementing a function. While choosing any of datagridviewcheckboxcolumn in form1 and show form2 when click button to close, datagridviewcheckboxcolumn in the form1 will is false ?
http://vanhuynh.comoj.com/home/all%20pictrure/Ask%20Image/help%20delegate%20in%20csharp.png[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900