C# winform通过按钮上移下移 解决了datasource绑定问题
事件代码:
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绑定问题的更多相关文章
- Devexpress WinForm TreeList的三种数据绑定方式(DataSource绑定、AppendNode添加节点、VirtualTreeGetChildNodes(虚拟树加载模式))
第一种:DataSource绑定,这种绑定方式需要设置TreeList的ParentFieldName和KeyFieldName两个属性,这里需要注意的是KeyFieldName的值必须是唯一的. 代 ...
- 聊天界面使用IQKeyboardManager导航栏及整个页面上移的解决方法
问题: 使用第三方库IQKeyboardManager时会使整个页面上移,导航栏页偏移出了显示范围.在聊天界面就会使得上面的消息看不到. 解决方法: 首先说明:在聊天界面使用IQKeyboardMan ...
- jQuery实现表格行上移下移和置顶
jQuery实现表格行上移下移和置顶 我们在操作列表数据的时候,需要将数据行排列顺序进行调整,如上移和下移行,将行数据置顶等,这些操作都可以在前端通过点击按钮来完成,并且伴随着简单的动态效果,轻松实现 ...
- jqgrid 上移下移单元格
在表格中常常需要调整表格中数据的显示顺序,我用的是jqgrid,实现原理就是将表中的行数保存到数据库中,取数据时按行进行排序 1.上移,下移按钮 <a href="javascript ...
- 05_jquery 操作table使tr(数据)整行上移下移
1:ajax请求数据到页面 function GetWorkSpaceList() { GetServerData("get", GetEnterpriseUrl() + &quo ...
- bootstrap与jqueryui按钮冲突的解决
bootstrap与jqueryui按钮冲突的解决 (2013-10-15 14:09:36)转载▼ 标签: 情感 分类: jQuery 参考: http://getbootstrap.com/jav ...
- php修改排序,上移下移
php修改排序,上移下移 /** $UpDown //移动方向,up或down $table //表名 $id //当前移动的ID $id_col //ID字段的名称 $ ...
- JS移动li行数据,点击上移下移(是位置的互换,不是top的偏移量改变)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- table中实现数据上移下移效果
html 由于vue+Element项目中的table,没有开放的上移下移的api,但是能对数据操作,故思路为数组中的一条数据,再重新添加一条数据,办法有点笨,但是好歹也是实现了,望有好的办法的,请留 ...
随机推荐
- 浅谈:nodejs在cmd提示不是内部或外部命令
今天用cmd安装个库,结果发现node不是内部命令,检查后发现上次重装nodejs换了个安装位置,path环境变量忘改了. 找到变量值中node的安装地址,比如C:develop\nodejs,如果不 ...
- 用meta name="renderer" content="webkit|ie-comp|ie-stand"来切换360双核安全浏览器的极速模式和兼容模式
以下信息摘自360官方网站: 浏览模式:极速模式.兼容模式及IE9高速模式是360浏览器显示网页时使用的三种模式:极速模式表示极速模式兼容模式表示兼容模式IE9IE10模式表示IE9/IE10模式(仅 ...
- Docker私有仓库的构建
[root@localhost ~]# vim /etc/sysconfig/docker #INSECURE_REGISTRY='--insecure-registry' INSECURE_REGI ...
- ArrayAccess(数组式访问)
实现该接口后,可以像访问数组一样访问对象. 接口摘要: ArrayAccess { abstract public boolean offsetExists ( mixed $offset ) abs ...
- iOS APP EuclidStudy Service Support
Hi,If you have any questions, you can send a message here or send them to us. We will answer you as ...
- JAVA基础——Date和Calendar类
1计算某一月份的最大天数 Calendar time=Calendar.getInstance(); time.clear(); time.set(Calendar.YEAR,year); //yea ...
- 在PL/SQL DEV里面有把锁一样的按钮,点击它会跳出“these query result are not updateable,include the ROWID to get updateab
在PL/SQL DEV里面有把锁一样的按钮,点击它会跳出“these query result are not updateable,include the ROWID to get updateab ...
- 洛谷——P2158 [SDOI2008]仪仗队
P2158 [SDOI2008]仪仗队 找规律大水题嘛,如果你做过P1170 兔八哥与猎人 这题得到的规律是$a,b,c,d$,若$gcd(a-b,c-d)==1$ 那么$a,b$就能看到$c,d$ ...
- Python基础—面向对象(进阶篇)
通过上一篇博客我们已经对面向对象有所了解,下面我们先回顾一下上篇文章介绍的内容: 上篇博客地址:http://www.cnblogs.com/phennry/p/5606718.html 面向对象是一 ...
- N的阶乘 mod P
基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 输入N和P(P为质数),求N! Mod P = ? (Mod 就是求模 %) 例如:n = 10, P = 11,10 ...