lundi 22 février 2016

how can i add checkbox in header column on top right corner to datagridview in c# windows applications

How to add Check Box to datagridview Header in c# and change the place of check box is top right corner to datagridview header column and select particular check box display color to datagridview cell in c# windows applications.

      private void Form1_Load_1(object sender, EventArgs e)
        {
 dgvcmb.HeaderText = "Select";
            dgvcmb.Name = "Chk";

            dgvcmb.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
            this.dataGridView1.Columns.Add(dgvcmb); 

            Rectangle rect = this.dataGridView1.GetCellDisplayRectangle(2, -1, true);

            ckBox.Size = new Size(18, 18);


            ckBox.Location = rect.Location;

            this.ckBox.CheckedChanged += new System.EventHandler(this.ckBox_CheckedChanged);


            this.dataGridView1.Controls.Add(ckBox);

}

        void ckBox_CheckedChanged(object sender, EventArgs e)
        {
            for (int j = 0; j < this.dataGridView1.RowCount; j++)
            {

                this.dataGridView1[2, j].Value = this.ckBox.Checked;

            }
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                DataGridViewCheckBoxCell check = row.Cells["Chk"] as DataGridViewCheckBoxCell;
                if (Convert.ToBoolean(check.Value) == true)
                    row.DefaultCellStyle.BackColor = Color.Wheat;
                else
                    row.DefaultCellStyle.BackColor = Color.White;

            }


            this.dataGridView1.EndEdit();
        }




Aucun commentaire:

Enregistrer un commentaire