事件代码:

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. Java线程的sleep方法

    sleep方法的签名: public static void sleep (long millis) sleep方法是Thread类的一个方法,作用是:在指定的毫秒内让正在执行的线程休眠(暂停执行) ...

  2. Java8(一)--lambda表达式

    相信作为一个Java程序员都会或多或少的了解过Java8中的lambda表达式.函数式编程等,本人也是用过lambda表达式,使用的都是比较简单 的实现 通过一个例子去都感受lambda: Compa ...

  3. RabbitMQ系列(七)--批量消息和延时消息

    批量消息发送模式 批量消息是指把消息放到一个集合统一进行提交,这种方案设计思路是希望消息在一个会话里,比如放到threadlocal里的集合,拥有相同 的会话ID,带有这次提交信息的size等属性,最 ...

  4. 01网页<head></head>常用标记及属性

    网页<head></head>常用标记及属性 <!DOCTYPE html> <html> <head> <!--网页标题--> ...

  5. vscode调试单个PHP脚本文件

    1.安装完vscode里的debug插件后, 在WorkSpace setting:添加上php的可执行文件路径: 2.下载适合自己PHP版本的Xdebug 3.在PHP目录下的php.ini配置文件 ...

  6. IN语句改写EXISTS

    -- IN SELECT T1.* FROM role_menu T1 WHERE T1.ROLEUUID IN ( SELECT T2.uuid FROM role T2 WHERE T2.UUID ...

  7. Python学习-算术运算符,赋值运算符和复合运算符

    算术运算符 常见的算术运算符有 : +     加法运算符 print(1 + 2); // 3 print('1' + '2'); //12 不仅可以进行2个数字的相加,还可以连接2个字符串 -   ...

  8. springboot+idea+jsp 404问题

    我是这么解决的 对于单一项目,加入以下jar包即可. <!--前台页面的支持--> <dependency> <groupId>javax.servlet</ ...

  9. Linux学习笔记(二) 文件管理

    了解 Linux 系统基本的文件管理命令可以帮助我们更好的使用 Linux 系统,以下介绍几个常用的文件管理命令 1.pwd pwd 是 Print Working Directory 的简写,用于显 ...

  10. Nginx + Lets'encrypt 实现HTTPS访问七牛空间资源

    上一篇文章 为七牛云存储空间绑定自定义域名,并使用七牛云提供的免费SSL证书,将自定义加名升级为HTTPS 我们提到利用七牛的免费SSL证书,将自定义加名升级为HTTPS的方法. 不知道有没有小伙伴会 ...