更新您的网格,当你有几千行,几十列的方式可能有表现不佳更是如此。此代码会为你工作,但是你想,真是奇怪的事情,不得以最专业的项目中遇到的:
//First we need to get all the non-empty cell values in some List<string>
var cells = dataGridView1.Rows.Cast<DataGridViewRow>()
.Where(row=>!row.IsNewRow)
.SelectMany(row=>dataGridView1.Columns.Cast<DataGridViewColumn>()
.Select(col=>row.Cells[col]))
.OrderBy(cell=>cell.ColumnIndex)
.ThenBy(cell=>cell.RowIndex)
.Where(cell=>Convert.ToString(cell.Value)!="").ToList();
//update the cells to make the grid look like sorted
for(int i = 0; i < dataGridView1.ColumnCount; i++){
for(int j = 0; j < 8; j++){
if(dataGridView1.Rows[j].IsNewRow) continue;
int k = i*8+j;
dataGridView1[i,j].Value = k < cells.Count ? cells[k] : null;
}
}
这是不排序的GridView控件,它完全改变你的GridView。 –
是的,就像我只想要 – Anjali
确定吗?你不需要单列中的数据? –