Results 1 to 3 of 3

Thread: Selecting DataGridView Rows Programatically

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2013
    Posts
    200

    Selecting DataGridView Rows Programatically

    Hi I have some code in my datagridview's CellMouseDown event to select multiple rows.

    It takes back my selection, after doing multiple selections over 1 time. How can I fix this? Note: I don't want to use mouseup event because it will make things worse imo.

    Here's my code:

    VB.NET Code:
    1. private void SelectDgvItems(bool ClearRequest)
    2. {
    3.     // Check if there's a line
    4.     if (programs_dgv.RowCount <= 0)
    5.     {
    6.         return;
    7.     }
    8.  
    9.     // Sort by name
    10.     programs_dgv.Sort(programs_dgv.Columns[2], ListSortDirection.Ascending);
    11.  
    12.     // Select required rows and set scrolling position
    13.     if (SelectedIndexes != null)
    14.     {
    15.         if (ClearRequest == true)
    16.         {
    17.             programs_dgv.ClearSelection();
    18.         }
    19.  
    20.         for (int i = 0; i <= SelectedIndexes.Count() - 1; i++)
    21.         {
    22.             programs_dgv[1, SelectedIndexes[i]].Selected = true;
    23.         }
    24.     }
    25.  
    26.     MessageBox.Show(LastSelectedIndex.ToString());
    27. }
    28.  
    29. private void programs_dgv_CellContentClick(object sender, DataGridViewCellEventArgs e)
    30. {
    31.     programs_dgv.CommitEdit(DataGridViewDataErrorContexts.Commit);
    32. }
    33.  
    34. private void programs_dgv_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
    35. {
    36.     CurrentRowIndex = e.RowIndex;
    37.  
    38.     Console.WriteLine(LastSelectedIndex.ToString() + "    " + CurrentRowIndex.ToString());
    39.  
    40.     // Check if there's a line
    41.     if (programs_dgv.Rows == null)
    42.     {
    43.         return;
    44.     }
    45.  
    46.     SelectedIndexes.Clear();
    47.     SelectedIndexes = null;
    48.     SelectedIndexes = new List<int>();
    49.  
    50.     // Reset SelectedIndexes
    51.     for (int i = 0; i <= programs_dgv.RowCount - 1; i++)
    52.     {
    53.         if (programs_dgv.Rows[i].Selected == true)
    54.         {
    55.             SelectedIndexes.Add(i);
    56.         }
    57.     }
    58.  
    59.     // When shift+click
    60.     if (e.Button == MouseButtons.Left && (ModifierKeys & Keys.Shift) == Keys.Shift)
    61.     {
    62.         if (CurrentRowIndex >= 0)
    63.         {
    64.             SelectedIndexes.Clear();
    65.             SelectedIndexes = null;
    66.             SelectedIndexes = new List<int>();
    67.  
    68.             // Set SelectedIndexes
    69.             for (int i = LastSelectedIndex; i <= CurrentRowIndex; i++)
    70.             {
    71.                 SelectedIndexes.Add(i);
    72.                 Console.WriteLine(SelectedIndexes[i].ToString() + Environment.NewLine);
    73.             }
    74.  
    75.             // Select dgv items (ClearRequest = false)
    76.             SelectDgvItems(false);
    77.             return;
    78.         }
    79.  
    80.         SelectDgvItems(false);
    81.         return;
    82.     }
    83.  
    84.  
    85.  
    86.     // When ctrl is not pressed
    87.     if (e.Button == MouseButtons.Left && (ModifierKeys & Keys.None) == Keys.None)
    88.     {
    89.  
    90.         SelectedIndexes.Clear();
    91.         SelectedIndexes = null;
    92.         SelectedIndexes = new List<int>
    93.         {
    94.             CurrentRowIndex
    95.         };
    96.  
    97.         // Set dgv items (ClearRequest = true)
    98.         SelectDgvItems(true);
    99.  
    100.         return;
    101.     }
    102.  
    103.  
    104.  
    105.     // right click
    106.     if (e.Button == MouseButtons.Right)
    107.     {
    108.         // Open context menu
    109.         int currentMouseOverRow = programs_dgv.HitTest(e.X, e.Y).RowIndex;
    110.  
    111.         if (currentMouseOverRow >= 0)
    112.         {
    113.             Cms_DatagridView.Items[0].Text = "düzenle (" + SelectedIndexes.Count().ToString() + ")";
    114.         }
    115.  
    116.         Cms_DatagridView.Show(programs_dgv, programs_dgv.PointToClient(System.Windows.Forms.Control.MousePosition));
    117.  
    118.         // -----------------------------------------------------------------------------
    119.  
    120.         // If current row isn't selected add it to the list
    121.         if (programs_dgv.Rows[CurrentRowIndex].Selected == false)
    122.         {
    123.             SelectedIndexes.Add(CurrentRowIndex);
    124.         }
    125.  
    126.         SelectedIndexes.Clear();
    127.         SelectedIndexes = null;
    128.         SelectedIndexes = new List<int>
    129.             {
    130.                 CurrentRowIndex
    131.             };
    132.  
    133.         SelectDgvItems(true);
    134.  
    135.         LastSelectedIndex = CurrentRowIndex;
    136.         return;
    137.     }
    138.  
    139. }
    Last edited by nikel; Jun 29th, 2018 at 04:49 PM. Reason: I updated code
    I'm not a man of too many faces
    The mask I wear is one

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Selecting DataGridView Rows Programatically

    Perhaps start with a FULL and CLEAR explanation of exactly what you expect the code to do, exactly what it does do and where exactly in the code your debugging told you that the behaviour deviated from your expectation.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Oct 2013
    Posts
    200

    Re: Selecting DataGridView Rows Programatically

    Hi, thanks for the reply any warning. I need rows of dgv to be selected programmatically. For example if run the code above,

    1. Open the program, row: 0 is selected by default
    2. I select multiple rows with shift+click (starting from row 0), this works well (my code only supports up to down selections yet).
    3. I do shift+click on another row to expand the selection (I expect start index to be 0)
    4. I'm not getting any errors
    4b. It clears my selection and applies 3rd action's start index, not 0.

    How can I fix it to make multiple selections without losing start index 0, or a start index created by single clicking but not shift+click?

    I get:

    Name:  2018_06_dgv_02.jpg
Views: 334
Size:  47.5 KB

    I expect:
    Name:  2018_06_dgv_01.jpg
Views: 374
Size:  43.1 KB
    Last edited by nikel; Jun 29th, 2018 at 04:40 PM. Reason: I corrected one word
    I'm not a man of too many faces
    The mask I wear is one

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width