xtragrid 实现焦点行的上移下移主要还是对gridviw绑定的数据表的操作,如果只是简单的显示而不做保存操作那么比较简单,直接把绑定的数据表中的前后两行更换下数据,具体如下:

private void UpOrDown(DevExpress.XtraGrid.Views.Grid.GridView GridView, bool ISUpOrDown,string TabName)
{
DataRow row = GridView.GetFocusedDataRow();
if (ISUpOrDown)
GridView.MovePrev();
else
GridView.MoveNext();
DataRow Pretrow = GridView.GetFocusedDataRow();
DataRow IRow = lblPrintDS1.Tables[TabName].NewRow();
for (int k = 0; k < row.ItemArray.Length; k++)
IRow[k] = Pretrow[k];
for (int i = 0; i < row.ItemArray.Length; i++)
Pretrow[i] = row[i];
for (int j = 0; j < row.ItemArray.Length; j++)
row[j] = IRow[j];
}
{
DataRow row = GridView.GetFocusedDataRow();
if (ISUpOrDown)
GridView.MovePrev();
else
GridView.MoveNext();
DataRow Pretrow = GridView.GetFocusedDataRow();
DataRow IRow = lblPrintDS1.Tables[TabName].NewRow();
for (int k = 0; k < row.ItemArray.Length; k++)
IRow[k] = Pretrow[k];
for (int i = 0; i < row.ItemArray.Length; i++)
Pretrow[i] = row[i];
for (int j = 0; j < row.ItemArray.Length; j++)
row[j] = IRow[j];
}
-------------------------------------------------------------------------------------------------------------------------------------------
但是如果所作的上移下移要保存到数据库则要麻烦些,比如gridview绑定的表:datatable 包含“行号”一列作为主键,具体如下:

private void ItemRowUpOrDown(bool isUP)
{
DataRow fRow = GridView.GetDataRow(GridView.FocusedRowHandle);
DataRow row;
if (isUP)
{//上移
row = GridView.GetDataRow(GridView.FocusedRowHandle - 1);
}
else
{//下移
row = GridView.GetDataRow(GridView.FocusedRowHandle + 1);
}
if (fRow == null || row == null) return;
if (!fRow["itemid"].ToString().Trim().Equals(row["itemid"].ToString().Trim()))
{
if (isUP)
MessageBoxShow.ShowProMessage("选择的商品已经移到改单据的第一行", StrTitle);
else
MessageBoxShow.ShowProMessage("选择的商品已经移到改单据的最后一行", StrTitle);
return;
}
int fNO = Convert.ToInt32(fRow["SORTNO"]);
int NO = Convert.ToInt32(row["SORTNO"]);
fRow["SORTNO"] = NO;
row["SORTNO"] = fNO;
gridControl1.Refresh();
gridControl1.RefreshDataSource();
GridView.RefreshData();
DataView dv = (DataView)GridView.DataSource;
if (dv != null)
{
dv.Sort = "SORTNO asc";
}
GridView.ClearSelection();
if (isUP)
{
GridView.FocusedRowHandle = NO - 1;
GridView.SelectRow(NO - 1);
}
else
{
GridView.FocusedRowHandle = fNO;
GridView.SelectRow(fNO);
}
}
{
DataRow fRow = GridView.GetDataRow(GridView.FocusedRowHandle);
DataRow row;
if (isUP)
{//上移
row = GridView.GetDataRow(GridView.FocusedRowHandle - 1);
}
else
{//下移
row = GridView.GetDataRow(GridView.FocusedRowHandle + 1);
}
if (fRow == null || row == null) return;
if (!fRow["itemid"].ToString().Trim().Equals(row["itemid"].ToString().Trim()))
{
if (isUP)
MessageBoxShow.ShowProMessage("选择的商品已经移到改单据的第一行", StrTitle);
else
MessageBoxShow.ShowProMessage("选择的商品已经移到改单据的最后一行", StrTitle);
return;
}
int fNO = Convert.ToInt32(fRow["SORTNO"]);
int NO = Convert.ToInt32(row["SORTNO"]);
fRow["SORTNO"] = NO;
row["SORTNO"] = fNO;
gridControl1.Refresh();
gridControl1.RefreshDataSource();
GridView.RefreshData();
DataView dv = (DataView)GridView.DataSource;
if (dv != null)
{
dv.Sort = "SORTNO asc";
}
GridView.ClearSelection();
if (isUP)
{
GridView.FocusedRowHandle = NO - 1;
GridView.SelectRow(NO - 1);
}
else
{
GridView.FocusedRowHandle = fNO;
GridView.SelectRow(fNO);
}
}
----------------------------------------------------------------------------------------------------------------------------------
一行一行的上移下移还是比较简单的比较只是换下行号,但如果gridview根据某一两列分组 要实现一组一组的上移下移比较麻烦!
原因是gridview 按某列分组后,(目前不知道什么原因),gridview会根据分组列自动升序排序 (强制的)。解决的办法比较麻烦死板 是在分组的第一列自动添加
一个序列。 如下图 选择测试单据后上移效果
假设一张表 datatable dt 有七列列 :
“单据名称 slipname”、“单据名称旧(和单据名称完全一样)oldslipName ”、“单据日期 slipdate”、“商品编码 itemcode”、“商品id itemid”、“单据id slipid”、“行号 sortno”。
现在 根据 单据名称、单据日期分组(一条单据下有一条或多条商品)代码如下:
第一步:
private void InertOrderValue()
{
int no = 0;
string strCD = string.Empty;
bool isSame = false;
for (int i = 0; i < dt.Rows.Count; i++)
{
DataRow dr = dt.Rows[i];
if (dr["slipid"].ToString().Trim() == strCD)
{
isSame = true;
}
else
isSame = false;
strCD = dr["slipid"].ToString().Trim();
if (isSame)
{//自定义 【000000000】
string str = no.ToString().PadLeft(9, '0');
str = string.Format("[{0}]", str);
dr["slipname"] = str + dr["oldslipName"];
}
else
{
no = no + 1;
string str = no.ToString().PadLeft(9, '0');
str = string.Format("[{0}]", str);
dr["slipname"] = str + dr["oldslipName"];
}
}
}
{
int no = 0;
string strCD = string.Empty;
bool isSame = false;
for (int i = 0; i < dt.Rows.Count; i++)
{
DataRow dr = dt.Rows[i];
if (dr["slipid"].ToString().Trim() == strCD)
{
isSame = true;
}
else
isSame = false;
strCD = dr["slipid"].ToString().Trim();
if (isSame)
{//自定义 【000000000】
string str = no.ToString().PadLeft(9, '0');
str = string.Format("[{0}]", str);
dr["slipname"] = str + dr["oldslipName"];
}
else
{
no = no + 1;
string str = no.ToString().PadLeft(9, '0');
str = string.Format("[{0}]", str);
dr["slipname"] = str + dr["oldslipName"];
}
}
}
第二步:上移下移

private void SlipRowUpOrDown(bool isUP)
{
DataRow fRow = GridView.GetDataRow(GridView.FocusedRowHandle);
DataRow row;
int fRowHandle = GridView.FocusedRowHandle;
if (isUP)
{//上移
row = GridView.GetDataRow(GridView.FocusedRowHandle + 1);
fRowHandle = GridView.FocusedRowHandle + 1;
}
else
{//下移
row = GridView.GetDataRow(GridView.FocusedRowHandle - 1);
fRowHandle= GridView.FocusedRowHandle -1;
}
if (fRow != null && row == null)
{
return;
}
object selGroupRow = GridView.GetGroupRowValue(GridView.FocusedRowHandle);
if (selGroupRow != null)
{
if (string.IsNullOrEmpty(selGroupRow.ToString().Trim()))
return;
if (!selGroupRow.ToString().Trim().Contains("["))
return;
}
int start = fRow["slipname"].ToString().Trim().IndexOf("]");
string uperName = fRow["slipname"].ToString().Trim().Substring(0, start+1);//上一行的单据名称
string downName = row["slipname"].ToString().Trim().Substring(0, start+1);//下一行的单据名称
if (!fRow["slipid"].ToString().Trim().Equals(row["slipid"].ToString().Trim()))
{
DataRow[] thisDr = dt.Select(string.Format("slipid='{0}'", fRow["slipid"].ToString().Trim()));
DataRow[] uperDr = dt.Select(string.Format("slipid='{0}'", row["slipid"].ToString().Trim()));
for (int i = 0; i < thisDr.Length; i++)
{
thisDr[i]["slipname"] = downName + fRow["oldslipName "];
}
for (int i = 0; i < uperDr.Length; i++)
{
uperDr[i]["slipname"] = uperName + row["oldslipName "];
}
}
GridView.ClearSelection();
GridView.FocusedRowHandle = fRowHandle;
GridView.SelectRow(fRowHandle);
}
{
DataRow fRow = GridView.GetDataRow(GridView.FocusedRowHandle);
DataRow row;
int fRowHandle = GridView.FocusedRowHandle;
if (isUP)
{//上移
row = GridView.GetDataRow(GridView.FocusedRowHandle + 1);
fRowHandle = GridView.FocusedRowHandle + 1;
}
else
{//下移
row = GridView.GetDataRow(GridView.FocusedRowHandle - 1);
fRowHandle= GridView.FocusedRowHandle -1;
}
if (fRow != null && row == null)
{
return;
}
object selGroupRow = GridView.GetGroupRowValue(GridView.FocusedRowHandle);
if (selGroupRow != null)
{
if (string.IsNullOrEmpty(selGroupRow.ToString().Trim()))
return;
if (!selGroupRow.ToString().Trim().Contains("["))
return;
}
int start = fRow["slipname"].ToString().Trim().IndexOf("]");
string uperName = fRow["slipname"].ToString().Trim().Substring(0, start+1);//上一行的单据名称
string downName = row["slipname"].ToString().Trim().Substring(0, start+1);//下一行的单据名称
if (!fRow["slipid"].ToString().Trim().Equals(row["slipid"].ToString().Trim()))
{
DataRow[] thisDr = dt.Select(string.Format("slipid='{0}'", fRow["slipid"].ToString().Trim()));
DataRow[] uperDr = dt.Select(string.Format("slipid='{0}'", row["slipid"].ToString().Trim()));
for (int i = 0; i < thisDr.Length; i++)
{
thisDr[i]["slipname"] = downName + fRow["oldslipName "];
}
for (int i = 0; i < uperDr.Length; i++)
{
uperDr[i]["slipname"] = uperName + row["oldslipName "];
}
}
GridView.ClearSelection();
GridView.FocusedRowHandle = fRowHandle;
GridView.SelectRow(fRowHandle);
}
第三步:重构数据集

private void OrderDataSet(string orderColumn)
{
DataTable dtt = dt.Clone();
dtt.Merge(dt);
DataView dvv = new DataView(dtt);
dvv.Sort = orderColumn;
dt.Clear();
dt.Merge(dvv.ToTable());
SortRowNo(dt, "MSTE937_SORTNO");//排序
}
{
DataTable dtt = dt.Clone();
dtt.Merge(dt);
DataView dvv = new DataView(dtt);
dvv.Sort = orderColumn;
dt.Clear();
dt.Merge(dvv.ToTable());
SortRowNo(dt, "MSTE937_SORTNO");//排序
}
第四步:隐藏列名

private void GridVPrm_CustomColumnDisplayText(object sender,DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
{
if (e.Column.Equals(slipname))
{//单据名称
StringBuilder sb = new StringBuilder();
sb.Append(dr["oldslipName"].ToString().Trim());
e.DisplayText = sb.ToString();
}
}
{
if (e.Column.Equals(slipname))
{//单据名称
StringBuilder sb = new StringBuilder();
sb.Append(dr["oldslipName"].ToString().Trim());
e.DisplayText = sb.ToString();
}
}