Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Controls / September 2005

Tip: Looking for answers? Try searching our database.

DataGrid multi selection

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
el_sid - 19 Aug 2005 16:15 GMT
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 - 23 Aug 2005 11:42 GMT
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.

> 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
[quoted text clipped - 175 lines]
>
>  
"Jeffrey Tan[MSFT]" - 27 Sep 2005 08:54 GMT
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
Signature

Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

el_sid - 29 Sep 2005 10:42 GMT
Thanks we will give it a try in the next couple of weeks and let you know how
we get on.

> Hi el_sid,
>
[quoted text clipped - 68 lines]
> Get Secure! - www.microsoft.com/security
> This posting is provided "as is" with no warranties and confers no rights.
"Jeffrey Tan[MSFT]" - 30 Sep 2005 02:59 GMT
Hi el_sid,

Ok, I will wait for your further feedback. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Signature

Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.