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,但是能对数据操作,故思路为数组中的一条数据,再重新添加一条数据,办法有点笨,但是好歹也是实现了,望有好的办法的,请留 ...
随机推荐
- R函数详解
字符串连接函数paste 1.字符串连接:paste(..., sep = " ", collapse = NULL)sep表示分隔符,默认为空格.collapse表示如果不指定值 ...
- restful风格url Get请求查询所有和根据id查询的合并成一个controller
restful风格url Get请求查询所有和根据id查询的合并成一个controller的方法 原代码 // 127.0.0.1:8080/dep/s @ApiOperation(value=&qu ...
- try catch影响Spring事务吗?
对于这个问题有两种情况: 1.catch只打印异常,不抛出异常 try { 数据库做添加订单表; /; 数据库减少库存; }catch (Exception e){ e.printStackTrace ...
- 本地搭建easy-mock
easy-mock要用nodejs启动,需要先安装nodejs ubuntu系统: apt install node centos系统: curl --silent --location https: ...
- 转来的--轻松自动化---selenium-webdriver(python) (七)---定位iframe——转来的
本节知识点: 多层框架或窗口的定位: switch_to_frame() switch_to_window() 智能等待: implicitly_wait() 对于一个现代的web应用,经常会出现框架 ...
- 通过注解配置Bean(2)
问:怎么用注解来配置bean与bean之间的引用关系? [组件装配] 1.<context:component-scan> 元素还会自动注册AutowiredAnnotationBeanP ...
- MySQL的Date()函数拼接
SELECT date_format(DATE(a.date_created),'%Y-%c-%d') As dateCreate, SUM(a.sm) As sumAmount, sum(order ...
- NYOJ5 Binary String Matching
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alp ...
- kafka 在阿里云部署
https://blog.csdn.net/chenyulancn/article/details/79499401 https://www.cnblogs.com/yangtianle/p/8761 ...
- poj—— 3037 Saving Beans
Saving Beans Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...