事件代码:

private void btn_frmDicType_MoveUp_Click(object sender, EventArgs e)
{

int lstLength = this.lst_frmDic_Type_Property.Items.Count;
int ilstSelect = this.lst_frmDic_Type_Property.SelectedIndex;
if (ilstSelect == 0)
{
MessageBox.Show("已在当前最顶端,无法再移动...");
return;
}
else if (lstLength > ilstSelect && ilstSelect > 0)
{
DataTable dt = (DataTable)lst_frmDic_Type_Property.DataSource;
DataTable dtCopy = new DataTable();
dtCopy.Clear();
dtCopy = dt.Copy();//拷贝dt
dtCopy.Rows[ilstSelect].Delete();
dtCopy.Rows[ilstSelect - 1].Delete();

DataRow drClone1 = dtCopy.NewRow();
DataRow drClone2 = dtCopy.NewRow();

drClone1.ItemArray = dt.Rows[ilstSelect].ItemArray;//需要上移的
drClone2.ItemArray = dt.Rows[ilstSelect - 1].ItemArray;//被移到下面
dtCopy.Rows.InsertAt(drClone1, ilstSelect - 1);
dtCopy.Rows.InsertAt(drClone2, ilstSelect);

//删除未彻底删除的2行,需要使用datatable.AcceptChanges()方法来提交修改.
dtCopy.AcceptChanges();

lst_frmDic_Type_Property.DataSource = dtCopy;
this.lst_frmDic_Type_Property.SelectedIndex = ilstSelect - 1;
//操作数据库,修改顺序号

//得到id号
int id1 = Convert.ToInt32(dt.Rows[ilstSelect]["ID"].ToString());
int id2 = Convert.ToInt32(dt.Rows[ilstSelect - 1]["ID"].ToString());
// 根据id号,得到顺序号
int order1 = Convert.ToInt32(frmDic_BLL.FrmDic_Dic_GetDICOrderById(id1).Tables[0].Rows[0]["DICOrder"]);
int order2 = Convert.ToInt32(frmDic_BLL.FrmDic_Dic_GetDICOrderById(id2).Tables[0].Rows[0]["DICOrder"]);
//根据id号,修改顺序号
frmDic_BLL.FrmDic_Dic_UpdataPropertyDICOrderByID(id1, order2);
frmDic_BLL.FrmDic_Dic_UpdataPropertyDICOrderByID(id2, order1);
}
else
{
return;
}
}

private void btn_frmDicType_MoveDown_Click(object sender, EventArgs e)
{
int lstLength = this.lst_frmDic_Type_Property.Items.Count;
int ilstSelect = this.lst_frmDic_Type_Property.SelectedIndex;
if (ilstSelect == lstLength - 1)
{
MessageBox.Show("已在当前最末端,无法再移动...");
return;
}
else if (lstLength - 1 > ilstSelect && ilstSelect >= 0)
{
DataTable dt = (DataTable)lst_frmDic_Type_Property.DataSource;
DataTable dtCopy = new DataTable();
dtCopy.Clear();
dtCopy = dt.Copy();//拷贝dt

/////////
// delete和remove
//Delete的使用是 datatable.Rows[i].Delete();
//Remove的使用是datatable.Rows.Remove(datatable.Rows[i]);
//这两个的区别是,使用delete后,只是该行被标记为deleted,但是还存在,用Rows.Count来获取行数时,还是删除之前的行数.需要使用datatable.AcceptChanges()方法来提交修改.

dtCopy.Rows[ilstSelect].Delete();
dtCopy.Rows[ilstSelect + 1].Delete();
DataRow drClone1 = dtCopy.NewRow();
DataRow drClone2 = dtCopy.NewRow();

drClone1.ItemArray = dt.Rows[ilstSelect].ItemArray;//需要下移的
drClone2.ItemArray = dt.Rows[ilstSelect + 1].ItemArray;//被移到上面
dtCopy.Rows.InsertAt(drClone1, ilstSelect + 1);
dtCopy.Rows.InsertAt(drClone2, ilstSelect);
//删除未彻底删除的2行,需要使用datatable.AcceptChanges()方法来提交修改.
dtCopy.AcceptChanges();

lst_frmDic_Type_Property.DataSource = dtCopy;
this.lst_frmDic_Type_Property.SelectedIndex = ilstSelect + 1;
//操作数据库,修改顺序号

//得到id号
int id1 = Convert.ToInt32(dt.Rows[ilstSelect]["ID"].ToString());
int id2 = Convert.ToInt32(dt.Rows[ilstSelect + 1]["ID"].ToString());
// 根据id号,得到顺序号
int order1 = Convert.ToInt32(frmDic_BLL.FrmDic_Dic_GetDICOrderById(id1).Tables[0].Rows[0]["DICOrder"]);
int order2 = Convert.ToInt32(frmDic_BLL.FrmDic_Dic_GetDICOrderById(id2).Tables[0].Rows[0]["DICOrder"]);
//根据id号,修改顺序号
frmDic_BLL.FrmDic_Dic_UpdataPropertyDICOrderByID(id1, order2);
frmDic_BLL.FrmDic_Dic_UpdataPropertyDICOrderByID(id2, order1);
}
else
{
return;
}
}

BLL:

// 根据ID,返回Dic表的顺序号
public DataSet FrmDic_Dic_GetDICOrderById(int strID)
{
return (FrmDic_DAL.FrmDic_Dic_GetDICOrderById(strID));
}
// 根据ID,更新Dic表的顺序号
public void FrmDic_Dic_UpdataPropertyDICOrderByID(int strID, int DICOrder)
{
FrmDic_DAL.FrmDic_Dic_UpdataPropertyDICOrderByID(strID, DICOrder);
}

DAL:

  

#region "根据ID,更新Dic表的顺序号"
/// <summary>
/// 根据ID,更新Dic表的顺序号
/// </summary>
/// <param name="strID">被修改的id</param>
/// <param name="DICOrder">序号</param>
public static void FrmDic_Dic_UpdataPropertyDICOrderByID(int strID, int DICOrder)
{
string sqlCommand = "FrmDic_Dic_UpdataPropertyDICOrderByID";

SqlParameter[] param ={
new SqlParameter("@strID",SqlDbType.Int),
new SqlParameter("@DICOrder",SqlDbType.Int),
};
param[0].Value = strID;
param[1].Value = DICOrder;
SqlHelper.ExecuteNonQuery(GHGD.Conn.Conn.SqlConn, CommandType.StoredProcedure, sqlCommand, param);
}
#endregion

#region "根据ID,返回Dic表的顺序号"
/// <summary>
/// 根据ID,返回Dic表的顺序号
/// </summary>
/// <param name="strID"></param>
public static DataSet FrmDic_Dic_GetDICOrderById(int strID)
{
string sqlCommand = "FrmDic_Dic_GetDICOrderById";
DataSet ds = new DataSet();
SqlParameter[] param ={
new SqlParameter("@id",strID),
};
//param[0].Value = strID;

SqlHelper.ExecuteDataset(GHGD.Conn.Conn.SqlConn, ds, "FrmDic_Dic_GetDICOrderById", CommandType.StoredProcedure, sqlCommand, param);

if (ds.Tables[0].Rows.Count != 0)
{
ds.Dispose();
return ds;
}
else
{
return null;
}

}
#endregion

存储过程:

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

ALTER procedure [dbo].[FrmDic_Dic_UpdataPropertyDICOrderByID]
(@strID int,@DICOrder int)
as
begin
update Dic set DICOrder=@DICOrder where ID=@strID
end

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

ALTER PROCEDURE [dbo].[FrmDic_Dic_GetDICOrderById]
(@id int)
AS
BEGIN
SET NOCOUNT ON;
select DICOrder from Dic where ID = @id
END

C# winform通过按钮上移下移 解决了datasource绑定问题的更多相关文章

  1. Devexpress WinForm TreeList的三种数据绑定方式(DataSource绑定、AppendNode添加节点、VirtualTreeGetChildNodes(虚拟树加载模式))

    第一种:DataSource绑定,这种绑定方式需要设置TreeList的ParentFieldName和KeyFieldName两个属性,这里需要注意的是KeyFieldName的值必须是唯一的. 代 ...

  2. 聊天界面使用IQKeyboardManager导航栏及整个页面上移的解决方法

    问题: 使用第三方库IQKeyboardManager时会使整个页面上移,导航栏页偏移出了显示范围.在聊天界面就会使得上面的消息看不到. 解决方法: 首先说明:在聊天界面使用IQKeyboardMan ...

  3. jQuery实现表格行上移下移和置顶

    jQuery实现表格行上移下移和置顶 我们在操作列表数据的时候,需要将数据行排列顺序进行调整,如上移和下移行,将行数据置顶等,这些操作都可以在前端通过点击按钮来完成,并且伴随着简单的动态效果,轻松实现 ...

  4. jqgrid 上移下移单元格

    在表格中常常需要调整表格中数据的显示顺序,我用的是jqgrid,实现原理就是将表中的行数保存到数据库中,取数据时按行进行排序 1.上移,下移按钮 <a href="javascript ...

  5. 05_jquery 操作table使tr(数据)整行上移下移

    1:ajax请求数据到页面 function GetWorkSpaceList() { GetServerData("get", GetEnterpriseUrl() + &quo ...

  6. bootstrap与jqueryui按钮冲突的解决

    bootstrap与jqueryui按钮冲突的解决 (2013-10-15 14:09:36)转载▼ 标签: 情感 分类: jQuery 参考: http://getbootstrap.com/jav ...

  7. php修改排序,上移下移

    php修改排序,上移下移 /**    $UpDown //移动方向,up或down    $table //表名    $id //当前移动的ID    $id_col //ID字段的名称    $ ...

  8. JS移动li行数据,点击上移下移(是位置的互换,不是top的偏移量改变)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. table中实现数据上移下移效果

    html 由于vue+Element项目中的table,没有开放的上移下移的api,但是能对数据操作,故思路为数组中的一条数据,再重新添加一条数据,办法有点笨,但是好歹也是实现了,望有好的办法的,请留 ...

随机推荐

  1. 梦想CAD控件关于id与handle问题

    ID和句柄具有各自的特点: (1) ID:在一个任务中,本次任务中都是独一无二的.在不同的任务中,同一个图形对象的ID可能不同. (2) 句柄:在一个任务中,不能保证每个对象的句柄都唯一,但是在一个图 ...

  2. 洛谷——P3173 [HAOI2009]巧克力

    P3173 [HAOI2009]巧克力 题目描述 有一块n*m的矩形巧克力,准备将它切成n*m块.巧克力上共有n-1条横线和m-1条竖线,你每次可以沿着其中的一条横线或竖线将巧克力切开,无论切割的长短 ...

  3. [USACO06JAN] 牛的舞会 The Cow Prom

    题目描述 The N (2 <= N <= 10,000) cows are so excited: it's prom night! They are dressed in their ...

  4. linux hexdump-显示文件十六进制格式

    博主推荐:获取更多 linux文件内容查看命令 收藏:linux命令大全 hexdump命令一般用来查看“二进制”文件的十六进制编码,但实际上它能查看任何文件,而不只限于二进制文件. 语法 hexdu ...

  5. display math in cnblog

    $a=b+c$ this is a example \(a=\frac{b}{c}\)

  6. ebay 店铺状态

    OrderStatusCodeType @XmlEnumValue("Active") @XmlEnumValue("Inactive") @XmlEnumVa ...

  7. RequestMapping注解_修饰类

    [使用RequestMapping映射请求] 1.Spring MVC使用 @RequestMapping 注解为控制器指定可以处理哪些URL请求. 2.在控制器的类定义及方法定义处都可以标注. @R ...

  8. [luoguP1095] 守望者的逃离(DP)

    传送门 这题....得考虑一些奇奇怪怪的复杂情况 不过也有简便方法. 枚举时间,先算出来只用魔法走的时间. 然后再枚举一遍时间,再算只走的时间,两个比较一下,取最游值. 代码 #include < ...

  9. noip模拟赛 立方数

    题目描述LYK定义了一个数叫“立方数”,若一个数可以被写作是一个正整数的3次方,则这个数就是立方数,例如1,8,27就是最小的3个立方数.现在给定一个数P,LYK想要知道这个数是不是立方数.当然你有可 ...

  10. springMVC 返回中文字符串时乱码

    转载自:https://blog.csdn.net/yaov_yy/article/details/51819567