DataRow移動技巧

/// <summary>
/// 上移
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ctBtnRevSave5_Click(object sender, EventArgs e)
{
    if (gridView1.GetSelectedRows().Length == 0)
    {
        return;
    }
    int selectRow = gridView1.GetSelectedRows()[0];

    if (selectRow > 0)
    {
        DataTable dt = dataSetMain2.Tables[AaaTableName];
        DataRow dr = dt.Rows[selectRow];
        DataRow drPrev = dt.Rows[selectRow - 1];
        foreach (DataColumn dc in dt.Columns)
        {
            if (dc.ColumnName.ToLower() == "morder")
            {
                continue;
            }
            object dataTemp = dr[dc.ColumnName];
            dr[dc.ColumnName] = drPrev[dc.ColumnName];
            drPrev[dc.ColumnName] = dataTemp;
        }
        gridView1.FocusedRowHandle = selectRow - 1;
    }
}
---------------------------------
多謝如下頁面的提示:
http://blog.csdn.net/czh4869623/article/details/6598866

protected void setRowToRow(DataRow dataRow, DataRow tempRow)
{
    //tempRow["id"] = dataRow["id"];//主键唯一约束
    tempRow["type"] = dataRow["type"];
}
/// <summary>
/// 上移
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnUp_Click(object sender, EventArgs e)
{
    DataRow currentRow = ((DataTable)ViewState["dtTitles"]).Rows.Find(102);//根据主键获取当前row
    DataRow[] rows = ((DataTable)ViewState["dtTitles"]).Select();//获取table中所有的row
    int currentIndex = Array.IndexOf(rows, currentRow); //获取当前row的index
    DataRow tempRow = ((DataTable)ViewState["dtTitles"]).NewRow();
    setRowToRow(((DataTable)ViewState["dtTitles"]).Rows[currentIndex - 1], tempRow);
    setRowToRow(((DataTable)ViewState["dtTitles"]).Rows[currentIndex], ((DataTable)ViewState["dtTitles"]).Rows[currentIndex - 1]);
    setRowToRow(tempRow, ((DataTable)ViewState["dtTitles"]).Rows[currentIndex]);
    BindData();
}

你可能感兴趣的:(Data)