【问题标题】:How to reverse rows in a DataGridView如何反转 DataGridView 中的行
【发布时间】:2016-10-07 01:13:28
【问题描述】:

我正在使用数据网格,但值未按我希望的方式显示。我当前的代码如下,我将如何反转行?

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;

【问题讨论】:

  • 如果您的意思是要反转 srot 顺序,您应该这样做:更改排序顺序。如果你没有,你可能应该。如果您的意思是转 90°,您可以使用 this post 作为开始..
  • 我将如何反转行排序顺序?我不需要转置行和列,我只需要倒置的行
  • 如果你真的一个排序顺序,你可以像往常一样恢复。如果是 DBMS 排序,则为 ORDER DESCENDING。如果是 Linq 排序是 OderDescending()。如果它是 BindingSource.Sort,它是字段 DESC。但是你从来没有告诉我们这些记录是从哪里来的……
  • 所以你直接添加了行,没有数据源?那么我的答案应该有效。
  • 请注意我回答末尾的备注!

标签: c# winforms datagridview


【解决方案1】:

要反转,即反转DataGridViewRows,您可以使用:

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

如果您只需要执行一次,则可以:

  • 反向循环遍历源文件的行
  • 或者在顶部使用Insert 代替Adding 行(到最后):

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

【讨论】:

  • 完美!非常感谢。
【解决方案2】:

反转行:

“DataGridView.Rows”.- 会给你“DataGridViewRowCollection”

以相反的顺序迭代集合并创建一个新的数据表。 (从最大尺寸到零的for循环)

将新数据表分配给datagridview源。

这个粗略的代码写在记事本上,供你参考。我现在没有 IDE。

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;

【讨论】:

  • 我现在没有 IDE。我正在添加粗略的源代码。
  • no.. 这两行修改/添加 DataRowView currentDataRowView = dgRowColllection[i].Row; dtnew.Rows.Add(currentDataRowView.Row);
  • DataRowView currentDataRowView = dgRowColllection[i].Row;没有定义的行错误
【解决方案3】:

无法评论 Pavan 的回答,因为我没有 50 名声望,但您是否收到错误,因为循环应该类似于:

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

【讨论】:

  • 真正的老板。我没有 IDE...我正在使用应用程序打电话
  • 编辑你的代码以包含我的,有两个错误,---- for (i = dataGridView1.Items.Count; i
猜你喜欢
  • 2013-06-12
  • 2013-11-08
  • 1970-01-01
  • 1970-01-01
  • 2021-12-14
  • 2010-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多