selecting rows from DataGridView

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Luuk
    Recognized Expert Top Contributor
    • Mar 2012
    • 1043

    selecting rows from DataGridView

    I am filling a datagridview with picture data (names, sizes), and trying to select certain rows.

    Code:
    int[] s = new int[100];
    
    foreach (var fileName in Directory.GetFiles(folder))
    {
       FileStream stream = File.OpenRead(fileName);
       Image i = Image.FromStream(stream, false, false);
       values[3] = i.Width;
       values[4] = i.Height;
       stream.Close();
       int r = dataGridView1.Rows.Add(values);
       s[r] = (!(bool)values[5] && (int)values[3]>1000 ? r : 0);
    }
    var selection = from t in s where t > 0 select s[t];
    foreach (var item in selection)
    {
       dataGridView1.CurrentCell = dataGridView1.Rows[item].Cells[0];
       dataGridView1.Rows[item].Selected = true;
    }
    The selection is done bases on the code on line#11 (which is OK for me!)

    My problem is the fact that only 1 row is selected, but debugging the foreach on line #13 shows I should have at least two lines in my selection (today).

    A short explanation on why this code is wrong would be appreciated 😉
    Last edited by Luuk; Apr 20 '19, 07:30 AM. Reason: added line #5-#9 to make example easier to understand...
  • Luuk
    Recognized Expert Top Contributor
    • Mar 2012
    • 1043

    #2
    Thanks for thinking with me,
    I pushed CTRL+A, DEL, and rewrote this.
    Now it's doing what I think it should be doing.

    Comment

    • SioSio
      Contributor
      • Dec 2019
      • 272

      #3
      Code:
         dataGridView1.CurrentCell = dataGridView1.Rows[item].Cells[0];
      The previous selection is deselected for this line.
      Do you need this line?

      Comment

      Working...