[英]How to interchange/reorder rows in gridview containing input entries
在所附的圖像中,gridview有幾列,其中包含特定的用戶所需行。 我只需要以以下方式重新排列行
Rowindex SOLID/YARN Rowindex SOLID/YARN
0 AOP 0 AOP
1 Solid => 1 AOP
2 AOP 2 Solid
使用列大小范圍,“描述”和“實心/紗線”互換行和rowindex。 那可能嗎???
此代碼交換行:
class DgvTools
{
public static void MoveRow(DataGridView dgv, int dgvRowIndex0, int dgvRowIndex1)
{
if (dgv.Rows != null)
if (dgv.DataSource != null)
{
DataTable dt = null;
if (dgv.DataSource is BindingSource)
{
dt = (DataTable)((BindingSource)dgv.DataSource).DataSource;
}else if (dgv.DataSource is DataTable)
{
dt = (DataTable)dgv.DataSource;
}
DataRow row0 = ((DataRowView)dgv.Rows[dgvRowIndex0].DataBoundItem).Row;
int row0Index = dt.Rows.IndexOf(row0);
DataRow row1 = ((DataRowView)dgv.Rows[dgvRowIndex1].DataBoundItem).Row;
int row1Index = dt.Rows.IndexOf(row1);
MoveRow(dt, row0Index, row1Index);
}
}
public static void MoveRow(DataTable dt, int row0Index, int row1Index)
{
if (dt != null)
{
int lowIndex = Math.Min(row0Index, row1Index);
int highIndex = Math.Max(row0Index, row1Index);
if (dt.Rows.Count >= 2)
if (lowIndex != highIndex)
if ((lowIndex >= 0) && (highIndex < dt.Rows.Count))
{
DataRow oldRow = dt.NewRow();
oldRow.ItemArray = dt.Rows[lowIndex].ItemArray;
dt.Rows.Remove(dt.Rows[lowIndex]);
dt.Rows.InsertAt(oldRow, highIndex);
}
}
}
}
用法:
DgvTools.MoveRow(dgvDisabled, 1, 2);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.