Friday, May 14, 2010

Resolving Datagrid View Scroll Problem while calling in thread

datagridview1.scrollBars=ScrollBars.None;
datagridview1.scrollBars=ScrollBars.Both;

Adding Checkbox in Column Header of DataGridView in .net using C#

private void Form_Load(object sender, EventArgs e)
{
c1 = new DataGridViewCheckBoxColumn();
c1.Name = " selection";
c1.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleLeft;
this.dataGridView1.Columns.Add(c1);

this.dataGridView1.Rows.Add();
this.dataGridView1.Rows.Add();
this.dataGridView1.Rows.Add();
this.dataGridView1.Rows.Add();

ckBox = new CheckBox();
//Get the column header cell bounds
Rectangle rect =
this.dataGridView1.GetCellDisplayRectangle(0, -1, true);
ckBox.Size = new Size(16, 16);
//Change the location of the CheckBox to make it stay on the header
ckBox.Location = rect.Location;
ckBox.CheckedChanged += new EventHandler(ckBox_CheckedChanged);
//Add the CheckBox into the DataGridView
this.dataGridView1.Controls.Add(ckBox);
}

void ckBox_CheckedChanged(object sender, EventArgs e)
{
for (int j = 0; j < this.dataGridView1.RowCount; j++)
{
this.dataGridView1[0, j].Value = this.ckBox.Checked;
}
this.dataGridView1.EndEdit();
}