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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
VasanthRank #1,487 · 1.1K reputationPosted Mar 27, 2010, 1:58 AM EDT
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.
VasanthRank #1,487 · 1.1K reputationPosted Mar 26, 2010, 11:07 PM EDT
[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.
kombsh m18 reputationPosted Mar 26, 2010, 11:46 PM EDT
Amit ChoudharyRank #145 · 15.8K reputationPosted Mar 27, 2010, 1:33 AM EDT
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:
private string Selection_ColumnName = null; private List Selection_CellValues = null; public void SetSelectedValues(string ColumnName, List CellValues) { Selection_ColumnName = ColumnName; Selection_CellValues = CellValues; gridData.DataBindingComplete += new DataGridViewBindingCompleteEventHandler(gridData_DataBindingComplete); } private void gridData_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e) { bool CellFound = false; for (int ColumnIndex = 0; ColumnIndex if (gridData.Columns[0].Name == Selection_ColumnName) { gridData.ClearSelection(); for (int RowIndex = 0; RowIndex foreach (object CellValue in Selection_CellValues) { try { if (gridData.Rows[RowIndex].Cells[ColumnIndex].Value.ToString() == CellValue.ToString()) { gridData.Rows[RowIndex].Cells[ColumnIndex].Selected = true; if (!CellFound) gridData.CurrentCell = gridData.SelectedCells[0]; CellFound = true; } } catch { } } } break; } } gridData.DataBindingComplete -= new DataGridViewBindingCompleteEventHandler(gridData_DataBindingComplete); }Please mark as answer if it helps.
kombsh m18 reputationPosted Mar 28, 2010, 8:03 PM EDT
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
kombsh m18 reputationPosted Mar 28, 2010, 10:42 PM EDT