datagridview multiselect without keyboard !

  • Thread starter Thread starter oliharvey
  • Start date Start date
O

oliharvey

howdy....

in a datagridview - the user can select multiple rows by holding the
Control key down as they click with the mouse.

I am looking for a way to achieve the same result without the use of
the keyboard (I have a touchscreen application).

any ideas? - I'm a bit stuck :)

thanks,
Oli.
 
The easiest way might be to toggle rows as selected/unselected when they
click (touch) each row.
 
You can try tracking selected rows yourself and toggle the selections
on and off in the CellClick event. You can use the RowPrePaint event
to set the backcolor of selected rows.

//in form.load
this.dataGridView1.DefaultCellStyle.SelectionBackColor =
Color.FromArgb(0, Color.Black); //hide normal color
this.dataGridView1.EditMode =
DataGridViewEditMode.EditProgrammatically;
this.dataGridView1.CurrentCell = null;

this.dataGridView1.CellClick += new
DataGridViewCellEventHandler(dataGridView1_CellClick);
this.dataGridView1.RowPrePaint += new
DataGridViewRowPrePaintEventHandler(dataGridView1_RowPrePaint);



private List<int> selectedRowIndexes = new List<int>();

void dataGridView1_RowPrePaint(object sender,
DataGridViewRowPrePaintEventArgs e)
{
if (this.selectedRowIndexes.IndexOf(e.RowIndex) > -1)

dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor =
Color.LightBlue;
else

dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor =
SystemColors.Window;
}

void dataGridView1_CellClick(object sender,
DataGridViewCellEventArgs e)
{
if (this.selectedRowIndexes.IndexOf(e.RowIndex) > -1)
this.selectedRowIndexes.Remove(e.RowIndex);
else
this.selectedRowIndexes.Add(e.RowIndex);
this.dataGridView1.CurrentCell = null;
}
==================
Clay Burch
Syncfusion, Inc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top