简体   繁体   中英

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;

反转行

To invert ie 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);

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;

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM