Results 1 to 2 of 2

Thread: [RESOLVED] Selected row to multi array

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2007
    Posts
    21

    Resolved [RESOLVED] Selected row to multi array

    Hi all,

    ANother newb question.
    Is there a way to loop through the selected rows and store them in an array?
    I've been working at it and have come up with this but it isnt working correctly.

    2 Code:
    1. int rCount = gridOffice.Rows.GetRowCount(DataGridViewElementStates.Selected);
    2.             if (rCount> 0)
    3.             {
    4.                 String[,]s = new String[rCount,cCount];
    5.                 for (int i = 0; i < rCount; i++)
    6.                 {
    7.                     for (int j = 0; j < 2; j++)
    8.                     {
    9.                         s[i,j] = gridOffice.Rows[gridOffice.SelectedCells[i].RowIndex].Cells[j].FormattedValue.ToString();
    10.                     }
    11.                 }

    Thanks in advance

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

    Re: Selected row to multi array

    This shows you how to get each cell value from each selected row. What you do with those values is then up to you.
    vb Code:
    1. for (int row = 0; row < this.DataGridView1.SelectedRows.Count; row++)
    2. {
    3.     for (int column = 0; column < this.DataGridView1.ColumnCount; column++)
    4.     {
    5.         MessageBox.Show(this.DataGridView1.SelectedRows[row].Cells[column].Value.ToString());
    6.     }
    7. }
    Last edited by jmcilhinney; Aug 29th, 2007 at 06:04 PM. Reason: Changed VB.NET code to C#.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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