Data manipulation in datagrid

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lildiapaz
    New Member
    • Jul 2007
    • 38

    Data manipulation in datagrid

    Hi everyone,

    i have a problem. I am working on a c# windows application that requires me to swap row values in a datagrid. Let's say for example, A column is with names is displayed in the datagrid, what I want to do is take row 1 (jack) and exchange it with row 15(paul) and exchange the values within the actual cells.

    Here's what I tried:
    Code:
     void swap_datagrid_values()
            {
                int ct;
                int last = dataGridView1.RowCount;
                string temp;
                for(int i=0; i<dataGridView1.RowCount; i++)
                {
                    foreach (DataGridViewRow row in dataGridView1.SelectedRows)
                    {
                        temp = dataGridView1.SelectedCells[i].ToString();
                        dataGridView1.SelectedCells[i].ToString() = dataGridView1.SelectedCells[last].ToString();
                        dataGridView1.SelectedCells[last].ToString() = temp;
                        last--;
                    }
                }
            }
    results, I get a compile error when I try this method
    Any suggestions?
    Please help
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Originally posted by lildiapaz
    Hi everyone,

    i have a problem. I am working on a c# windows application that requires me to swap row values in a datagrid. Let's say for example, A column is with names is displayed in the datagrid, what I want to do is take row 1 (jack) and exchange it with row 15(paul) and exchange the values within the actual cells.
    ...results, I get a compile error when I try this method
    Any suggestions?
    Please help
    What is the compile error that you are getting.

    Your logic isn't making much sense to me.
    [CODE=cpp] void swap_datagrid_v alues()
    {
    int ct;
    //you are setting the integer "last" to be the Number Of Rows in
    //your dataGridView (Not the number of columns)
    int last = dataGridView1.R owCount;
    string temp;
    for(int i=0; i<dataGridView1 .RowCount; i++)
    { //for every row in the dataGridView you are going to
    //go through every selected row in the dataGridView... .

    foreach (DataGridViewRo w row in dataGridView1.S electedRows)
    { //for every selected row in the dataGridView you are grabbing the selected cell at the row index you are at in the outer loop and storing them into the string called temp. You are probably getting an out of bounds excpetion here
    temp = dataGridView1.S electedCells[i].ToString();


    //here you are trying to access the selected cell at the integer last.....but last is set to the number of rows in your dataGridView... .you're probably getting an out of bounds exception here as well.
    dataGridView1.S electedCells[i].ToString() = dataGridView1.S electedCells[last].ToString();

    //and you are probably getting an out of bounds exception here too.
    dataGridView1.S electedCells[last].ToString() = temp;
    last--;
    }
    }
    }[/CODE]

    I suggest writing down in words what you need to do for the swap and then try to write the code for the swap again...then if you're still having problems ask us :)

    -Frinny

    Comment

    • lildiapaz
      New Member
      • Jul 2007
      • 38

      #3
      Originally posted by Frinavale
      What is the compile error that you are getting.

      Your logic isn't making much sense to me.
      [CODE=cpp] void swap_datagrid_v alues()
      {
      int ct;
      //you are setting the integer "last" to be the Number Of Rows in
      //your dataGridView (Not the number of columns)
      int last = dataGridView1.R owCount;
      string temp;
      for(int i=0; i<dataGridView1 .RowCount; i++)
      { //for every row in the dataGridView you are going to
      //go through every selected row in the dataGridView... .

      foreach (DataGridViewRo w row in dataGridView1.S electedRows)
      { //for every selected row in the dataGridView you are grabbing the selected cell at the row index you are at in the outer loop and storing them into the string called temp. You are probably getting an out of bounds excpetion here
      temp = dataGridView1.S electedCells[i].ToString();


      //here you are trying to access the selected cell at the integer last.....but last is set to the number of rows in your dataGridView... .you're probably getting an out of bounds exception here as well.
      dataGridView1.S electedCells[i].ToString() = dataGridView1.S electedCells[last].ToString();

      //and you are probably getting an out of bounds exception here too.
      dataGridView1.S electedCells[last].ToString() = temp;
      last--;
      }
      }
      }[/CODE]

      I suggest writing down in words what you need to do for the swap and then try to write the code for the swap again...then if you're still having problems ask us :)

      -Frinny
      My logic for the swap was to take the same approach I would take if I were using a bubblesort, create a temp value, select a specific row value, select another row value, then swap. The only difference b/w my approach and the bubble sort is I'm not trying to compare, just swap. I'm confused, can you help? I want to do something like this swap row value 1 with the last row value, then swap row value 2 with the second to last row value, etc.

      Any suggestions

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Originally posted by lildiapaz
        My logic for the swap was to take the same approach I would take if I were using a bubblesort, create a temp value, select a specific row value, select another row value, then swap. The only difference b/w my approach and the bubble sort is I'm not trying to compare, just swap. I'm confused, can you help? I want to do something like this swap row value 1 with the last row value, then swap row value 2 with the second to last row value, etc.

        Any suggestions
        Have you seen this article on Sorting a GridView Manually?

        -Frinny

        Comment

        Working...