创建一个列,其数据与DataTable绑定的Datagridview中的另一列数据相反

时间:2017-01-07 20:42:50

标签: c# winforms datagridview reverse

我需要将datagridview列的数据反转,并将其放在同一datagridview的另一列上。

datagridview有一个Datatable作为源。

for (int i = 1; i <= plazo; i++)
{
    DataRow row = dtDetalle.NewRow();
    //#
    row["nrocuota"] = i.ToString();
    //Fecha de vencimiento
    fecha_pago = fecha_pago.AddMonths(1);
    row["vencimiento"] = fecha_pago.ToString("dd/MM/yyyy");
    //Plazo
    FPlazo = Convert.ToDateTime(row["vencimiento"]);
    plazodias = (decimal)(FPlazo - dtpkrfecha.Value).TotalDays;
    row["plazo"] = Convert.ToInt32(plazodias); //This is the column that I need to reverse

    dtDetalle.Rows.Add(row);
}               

1 个答案:

答案 0 :(得分:0)

试试这个:

public DataTable ReverseRowsInDataTable(DataTable inputTable)
{
    DataTable outputTable = inputTable.Clone();

    for (int i = inputTable.Rows.Count - 1; i >= 0; i--)
    {
        outputTable.ImportRow(inputTable.Rows[i]);
    }

    return outputTable;
}

所以基本上你用旧表填充新表,但是从最后一个索引填充。

编辑:我错过了你的问题。对于反转列,它与整个数据表的原理相同,但不是行,而是使用单行中的列。

所以只需创建新的DataRow和

for int(i = yourDataRow.Cells.Count - 1; i >= 0; i--)
{
    //and here insert value in your new DataRow column by column but calculate if max i = 5, then when i = 5, new column in newDataRow needs to be 0, 4-1 etc.
}
相关问题