Shadow89
Shadow89

Reputation: 57

How to reverse rows in a DataGridView

I am using a data grid, but the values do not display as I would like them to. My current code is below, how would I go about inverting the rows?

string[] strOutput = strLine.Split('_', ',','=');

int totalRows = Convert.ToInt16(strOutput[4]);
int totalCols = Convert.ToInt16(strOutput[5]);
int itemIndex = 8;

for (int i = 0; i < totalCols; i++){ 
    dataGridView1.Columns.Add("Col", "Col");
}
dataGridView1.Rows.Add(totalRows);
for (int i = 0; i < totalRows; i++) { 
    for (int j = 0; j < totalCols; j++) { 
        dataGridView1.Rows[i].Cells[j].Value = strOutput[itemIndex];
         itemIndex += 2;
    }                  
}
dataGridView1.Visible = true;

Invert Rows

Upvotes: 4

Views: 6621

Answers (3)

TaW
TaW

Reputation: 54433

To invert i.e. reverse DataGridViewRows you can use this:

void ReverseDGVRows(DataGridView dgv)
{
    List<DataGridViewRow> rows = new List<DataGridViewRow>();
    rows.AddRange(dgv.Rows.Cast<DataGridViewRow>());
    rows.Reverse();
    dgv.Rows.Clear();
    dgv.Rows.AddRange(rows.ToArray());
}

If you only need to do it once you could instead either:

  • loop over the lines of the source file in reverse
  • or instead of Adding the rows (to the end) Insert at the top:

dtnew.Rows.Insert(0, currentDataRowView.Row);

Upvotes: 7

fussy
fussy

Reputation: 37

Can't comment on Pavan's answer because I don't have 50 reputation, but are you getting the error because the loop should be something like:

int totalRows = Convert.ToInt16(strOutput[4]);
int totalCols = Convert.ToInt16(strOutput[5]);
int itemIndex = 8;

for (int i = 0; i < totalCols; i++)
{
    dataGridView1.Columns.Add("Col", "Col");
}
dataGridView1.Rows.Add(totalRows);
for (int i = 0; i < totalRows; i++) 
{
    for (int j = 0; j < totalCols; j++)
       { 
           dataGridView1.Rows[i].Cells[j].Value = strOutput[itemIndex];
           itemIndex += 2;
       }
       DataGridViewRowCollection dgRowColllection = dataGridView1.Rows;
       DataTable dtnew = new DataTable();
       for (i = dataGridView1.Items.Count; i < 1; i--)
       {
            DataRowView currentDataRowView = dgRowColllection[i].Row;
            dtnew.Rows.Add(currentDataRowView.Row);
       }
       dataGridView1.DataSource = dtnew;
   }    
   dataGridView1.Visible = true;
}

Upvotes: 0

Pavan Chandaka
Pavan Chandaka

Reputation: 12751

Inverting rows:

"DataGridView.Rows".- will give you "DataGridViewRowCollection"

Iterate the collection in reverse order and create a new datatable. (for loop from max size to zero)

Assign the new datatable to datagridview source.

This rough code written in note pad for your idea. I do not have IDE now.

DataGridViewRowCollection dgRowColllection = dataGridView1.Rows;
DataTable dtnew = new DataTable();
for(i = dataGridView1.RowCount; i  < 1 ; i--)
{
DataRowView currentDataRowView = dgRowColllection[i].Row;

dtnew.Rows.Add(currentDataRowView.Row);

}

dataGridView1.source = dtnew;

Upvotes: 0

Related Questions