如何在DataGridView中反转行 [英] How to reverse rows in a DataGridView
本文介绍了如何在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;
推荐答案
要反转即反转 DataGridViewRows
,您可以使用以下方法:
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);
这篇关于如何在DataGridView中反转行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文