C# 如何反转DataGridView中的行

C# 如何反转DataGridView中的行,c#,winforms,datagridview,C#,Winforms,Datagridview,我使用的是一个数据网格,但这些值并不像我希望的那样显示。下面是我当前的代码,我将如何反转行 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++){

我使用的是一个数据网格,但这些值并不像我希望的那样显示。下面是我当前的代码,我将如何反转行

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;
string[]strOutput=strLine.Split(“",“,”,“=”);
int totalRows=Convert.ToInt16(strOutput[4]);
int totalCols=转换为16(strOutput[5]);
int itemIndex=8;
对于(inti=0;i

反转行:

“DataGridView.Rows”。-将为您提供“DataGridViewRowCollection”

以相反的顺序迭代集合并创建新的datatable。(用于从最大大小到零的循环)

将新datatable分配给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;
DataGridViewRowCollection dGrowCollection=dataGridView1.Rows;
DataTable dtnew=新DataTable();
对于(i=dataGridView1.RowCount;i<1;i--)
{

DataRowView currentDataRowView=dGrowCollection[i]。行; 添加(currentDataRowView.Row); } dataGridView1.source=dtnew;
无法对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;
}
inttotalrows=Convert.ToInt16(strOutput[4]);
int totalCols=转换为16(strOutput[5]);
int itemIndex=8;
for(int i=0;iDataRowView currentDataRowView=dGrowCollection[i]。行;
添加(currentDataRowView.Row);
}
dataGridView1.DataSource=dtnew;
}    
dataGridView1.Visible=true;
}

要反转,即反转
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());
}

我现在没有IDE。我正在添加粗略的源代码。没有。。这两行修改/添加了DataRowView currentDataRowView=dGrowCollection[i].Row;添加(currentDataRowView.Row);DataRowView currentDataRowView=dGrowCollection[i]。行;因为没有一个真正的老板,排错了。我没有IDE。。。我正在电话中,希望您的代码包括我的代码,有两个错误,---for(I=dataGridView1.Items.Count;I<1;I--)--Items正在查找定义,--DataRowView currentDataRowView=dGrowCollection[I]。行;-----Row正在寻找一个定义如果您的意思是要反转srot顺序,您应该这样做:更改排序顺序。如果你没有,你可能应该。如果你的意思是转90度,你可以用它作为起点。我如何颠倒行排序顺序呢?我不需要转置行和列,我只需要换行,如果你有一个排序顺序,你可以像往常一样恢复。如果是DBMS排序,则为降序排序。如果是Linq排序,则为ODERDESENDING()。如果是BindingSource.Sort,则为field DESC。但您从未告诉我们记录来自何处。所以您直接添加了行,而没有数据源?那么我的答案就行了。注意我答案末尾的备注!完美的非常感谢你。
dtnew.Rows.Insert(0, currentDataRowView.Row);