can anyone help how to select the multiple cells in datagridview through code, now i am setting selected property as true for each cell before
load the control. if once the control loaded it shows only one cell as selected cell.
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VasanthPosted Mar 27, 2010, 5:58 AM
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex == this.dataGridView1.CurrentCell.ColumnIndex && e.RowIndex == this.dataGridView1.CurrentCell.RowIndex)
{
e.Paint(e.CellBounds, DataGridViewPaintParts.All & ~DataGridViewPaintParts.Border);
using (Pen p = new Pen(Color.Black, 4))
{
Rectangle rect = e.CellBounds;
rect.Width -= 2;
rect.Height -= 2;
e.Graphics.DrawRectangle(p, rect);
}
e.Handled = true;
}
}
Try other option as below , here we using selectionchanged event and highligted bold for selected cell.
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
foreach (DataGridViewRow row in this.dataGridView1.Rows)
{
foreach (DataGridViewCell c in row.Cells)
{
c.Style = this.dataGridView1.DefaultCellStyle;
}
}
DataGridViewCellStyle style = new DataGridViewCellStyle();
style.BackColor = Color.Red;
style.Font = new Font("Courier New", 14.4f, FontStyle.Bold);
foreach (DataGridViewCell cell in this.dataGridView1.SelectedCells)
{
cell.Style = style;
}
}
Please mark as answer , if its helps you.
kombsh mPosted Mar 29, 2010, 2:42 AM
kombsh mPosted Mar 29, 2010, 12:03 AM
In this way i can draw border for only one selected cell but here i need to draw border for all the selected cells.like the below image
Amit ChoudharyPosted Mar 27, 2010, 5:33 AM
the setting of selected cell's is only effective after the datasource is bound to the control so you have to have write add event handler for
Here is the code:
Please mark as answer if it helps.
kombsh mPosted Mar 27, 2010, 3:46 AM
VasanthPosted Mar 27, 2010, 3:07 AM
[C# Code]
private void Form1_Load(object sender, EventArgs e)
{
//Create temp datatable for bind values
DataTable dtTemp = new DataTable(); DataRow dr;
//create schema
dtTemp.Columns.Add("Name"); dtTemp.Columns.Add("City"); dtTemp.Columns.Add("State");
//Add temp datas
dr = dtTemp.NewRow(); dr["Name"] = "AAA"; dr["City"] = "CAAA"; dr["State"] = "SAAA"; dtTemp.Rows.Add(dr);
dr = dtTemp.NewRow(); dr["Name"] = "BBB"; dr["City"] = "CBBB"; dr["State"] = "SBBB"; dtTemp.Rows.Add(dr);
dr = dtTemp.NewRow(); dr["Name"] = "CCC"; dr["City"] = "CCCC"; dr["State"] = "SCCC"; dtTemp.Rows.Add(dr);
dr = dtTemp.NewRow(); dr["Name"] = "DDD"; dr["City"] = "CDDD"; dr["State"] = "SDDD"; dtTemp.Rows.Add(dr);
dr = dtTemp.NewRow(); dr["Name"] = "EEE"; dr["City"] = "CEEE"; dr["State"] = "SEEE"; dtTemp.Rows.Add(dr);
//Bind the datasourse
dataGridView1.DataSource = dtTemp;
//Alter the propertise
dataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect;
dataGridView1.MultiSelect = true;
//If u want to dynamically select the cells , just update the below code
dataGridView1[1,1].Selected = true;
dataGridView1[2, 1].Selected = true;
dataGridView1[0, 2].Selected = true;
dataGridView1[1, 2].Selected = true;
}
If it helps you , please mark as answer.