DataGrid multi selection

5 views
Skip to first unread message

el_sid

unread,
Aug 19, 2005, 11:15:03 AM8/19/05
to
I was wondering if anyone knew of a way to do multi-row selections in a data
grid with a simple mouse click without using the shift key or the row
headers. Simply we want our users to be able to select several rows by
clicking on them with their mouse and have them all remain highlighted. We
also would like to do the inverse and allow them to click on a highlighted
row and that row would then become unselected.

We have attempted to do this by inheritting the .NET datagrid and
implementing the functions below but this does not seem to work. Any help
would be appreciated.

The version of .NET we are using is 1.1.

public const int gintNO_SELECTION = -1;

protected bool mblnFullRowSelect = true;

protected System.Windows.Forms.DataGridTableStyle gtsTableStyle;

protected int mintCurrentDataGridRowIndex = 0; // Used by
certain derived forms for keeping track of current data grid row edits.

private ArrayList maintSelectedIndexes = new ArrayList();

private bool mblnMultiSelect = true;

public new void Select(int vintIndex)

{

int lintIndex = 0;

BaseTraceManagement.LogFunctionEntry(TraceLevel.Info);

try

{

// If we don't allow multi select then unselect the
current row first.

if(!mblnMultiSelect)

{

UnSelect(mintCurrentDataGridRowIndex);

}

// Only continue if we are doing full row selection.

if(!mblnFullRowSelect)

{

return;

}

// Loop through our array list of indexes and check
to see if the row is already selected.

// NOTE: Can't use IsSelected() as it always returns
false.

for(lintIndex = 0; lintIndex <
maintSelectedIndexes.Count; ++lintIndex)

{

// If we have a match then unselect the row
and return.

if(vintIndex ==
Convert.ToInt32(maintSelectedIndexes[lintIndex]))

{

UnSelect(vintIndex);

return;

}

}

// Select the row.

base.Select(vintIndex);

// Keep track of the new selection.

maintSelectedIndexes.Add(vintIndex);

// Indicate the current new row selected.

mintCurrentDataGridRowIndex = vintIndex;

}

finally

{

BaseTraceManagement.LogFunctionExit(TraceLevel.Info);

}

}

public new void UnSelect(int vintIndex)

{

int lintIndex = 0;

BaseTraceManagement.LogFunctionEntry(TraceLevel.Info);

try

{

// Loop through all items being tracked and remove
the relevant one.

for(lintIndex = 0; lintIndex <
maintSelectedIndexes.Count - 1; ++lintIndex)

{

// If we don't allow multi-select or the row
index matches then remove the row.

if(!mblnMultiSelect ||
(Convert.ToInt32(maintSelectedIndexes[lintIndex]) == vintIndex))

{

// Unselect the row.

base.UnSelect(vintIndex);

// Remove the selection.

maintSelectedIndexes.RemoveAt(lintIndex);

break;

}

}

// If the current row index matches the supplied
index (or we don't allow for multi-select) then reset it.

if(mblnMultiSelect || (mintCurrentDataGridRowIndex
== vintIndex))

mintCurrentDataGridRowIndex = gintNO_SELECTION;

}

finally

{

BaseTraceManagement.LogFunctionExit(TraceLevel.Info);

}

}

el_sid

unread,
Aug 23, 2005, 6:42:05 AM8/23/05
to
Sorry, the problem was described incorrectly to me and I was given the wrong
code.

What we are trying to do is multi-row selections in a data grid with a
simple mouse click using the shift and ctrl keys (don't know if this helps
but we don't show the row headers). Simply we want our users to be able to

select several rows by clicking on them with their mouse and have them all
remain highlighted. We also would like to do the inverse and allow them to
click on a highlighted row and that row would then become unselected.

Jeffrey Tan[MSFT]

unread,
Sep 27, 2005, 3:54:14 AM9/27/05
to
Hi el_sid,

Thanks for your post.

To do row selection in cell mouse click operation, we can handle MouseUp
event of DataGrid, and programmatically select certain row. Please refer to
the link below:
"5.11 How can I select the entire row when the user clicks on a cell in the
row?"
http://64.78.52.104/FAQ/WinForms/FAQ_c44c.asp#q689q

To implement the Shift/Ctrl multiselection function, we should do the
selection logic ourselves. I have implemented a sample for Shift key, I
think you can use this sample code as a reference and implement Ctrl key
function. Sample code listed below:
private void dataGrid1_MouseUp(object sender,
System.Windows.Forms.MouseEventArgs e)
{

System.Drawing.Point pt = new Point(e.X, e.Y);
DataGrid.HitTestInfo hti = dataGrid1.HitTest(pt);
if(hti.Type == DataGrid.HitTestType.Cell)
{
if((Control.ModifierKeys&Keys.Control)==Keys.Control)
{
bool funselect=false;
for(int i=0;i<al.Count;i++)
{
if(hti.Row==(int)al[i])
{
funselect=true;
}
else
{
this.dataGrid1.Select((int)al[i]);
}
}
if(!funselect)
{
this.dataGrid1.Select(hti.Row);
}
}
else
{
dataGrid1.CurrentCell = new DataGridCell(hti.Row, hti.Column);
dataGrid1.Select(hti.Row);
}
}
}

//Record original selected rows in arraylist in MouseDown event
ArrayList al=new ArrayList();
private void dataGrid1_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
al.Clear();
for(int i=0;i<this.dataGrid1.BindingContext[this.dataGrid1.DataSource,
this.dataGrid1.DataMember].Count;i++)
{
if(this.dataGrid1.IsSelected(i))
{
al.Add(i);
}
}
}
This works well on my side. Hope it helps.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

el_sid

unread,
Sep 29, 2005, 5:42:02 AM9/29/05
to
Thanks we will give it a try in the next couple of weeks and let you know how
we get on.

Jeffrey Tan[MSFT]

unread,
Sep 29, 2005, 9:59:32 PM9/29/05
to
Hi el_sid,

Ok, I will wait for your further feedback. Thanks

Reply all
Reply to author
Forward
0 new messages