GridView实现自定义按钮及中间插入记录排序功能
GridView实现自定义按钮及中间插入记录排序功能GridView实现自定义按钮及插入记录排序功能
这命题有点长否则很难说明本文内容,这并不重要。下面详细讲解我想表达的内容。 /// <summary> /// 增加3个自定义按钮 /// </summary> private void AddCustomButton() { gcDetail.BeginInit(); gcDetail.EmbeddedNavigator.Buttons.ImageList = this.imageList1; ControlNavigatorButtons btns = gcDetail.EmbeddedNavigator.Buttons; btns.CustomButtons.Clear(); NavigatorCustomButton btnDtlAdd = new NavigatorCustomButton((int)DetailButtons.Add, NavigatorCustomButton btnDtlInsert = new NavigatorCustomButton((int)DetailButtons.Insert, NavigatorCustomButton btnDtlDelete = new NavigatorCustomButton((int)DetailButtons.Delete, btns.CustomButtons.AddRange(new NavigatorCustomButton[] { btnDtlAdd, btnDtlInsert, gcDetail.EndInit(); }
/// <summary> /// 明细表Grid自定义按钮编号(ImageList图片编号,又用作自定义按钮编号,一箭双雕!) /// </summary> public class DetailButtons { public const int Add = 0; public const int Insert = 1; public const int Delete = 2; }
/// <summary>
/// 生成排序编号 /// </summary> public class GenerateSortID { public readonly string DEF_FIRST_SORTING_PRIOR = "0.0"; public readonly string DEF_SORTING_SEPRATOR = "."; public readonly string DEF_FIRST_SORTING = "1"; public readonly decimal DEF_STEP = (decimal)0.2; //步辐. public readonly int DEF_MAX_SORTING_LENGTH = 20; public GenerateSortID() { } /// <summary> /// 获取指定列的唯一数值. /// </summary> public ArrayList GetMRUFilters(GridView view, GridColumn col) { ArrayList list = new ArrayList(); try { for (int i = 0; i < view.RowCount; i++) { object o = view.GetRowCellValue(i, col); if (!list.Contains(o) && !o.ToString().Equals(string.Empty)) list.Add(o); } list.Sort(); //默认为Ascedent return list; } catch { return list; } } /// <summary> /// 获取最大的排序编号 /// </summary> public int GetMaxSortingID(ArrayList sortingFilters) { try { sortingFilters.Sort(); string str = (string)sortingFilters[sortingFilters.Count - 1]; string[] arr = str.Split(Convert.ToChar(DEF_SORTING_SEPRATOR)); return Convert.ToInt16(arr[0]); } catch { return int.Parse(DEF_FIRST_SORTING); } } /// <summary> /// 生成一个新的排序编号 /// </summary> /// <param name="currSortingID">当前行的Sort编号</param> /// <param name="prevSortingID">前一行的Sort编号</param> public string GetNewSortingID(string currSortingID, string prevSortingID) { try { if (prevSortingID == null) prevSortingID = DEF_FIRST_SORTING_PRIOR; decimal prefix = decimal.Parse(prevSortingID); decimal curr = decimal.Parse(currSortingID); decimal newid = (curr - prefix) / 2 + prefix; string ret = newid.ToString(); return ret.Replace("5", "4"); } catch { return currSortingID; } } /// <summary> /// 生成排序字符串. /// Insert方法生成规则,假设当前记录为n: /// Append方法生成规则:生成的排序字符串为max(n)+1 /// </summary> public string Generate(GridView view, GridColumn sortingColumn) { try { ArrayList list = GetMRUFilters(view, sortingColumn); if (view.RowCount <= 0) return DEF_FIRST_SORTING; //设置默认排序ID int pos = view.FocusedRowHandle; int value = 0; if (pos < 0) //没有当前记录. 以Append方式生成排序字符串 { value = GetMaxSortingID(list); return value.ToString() + DEF_SORTING_SEPRATOR; } else if (pos == view.RowCount - 1) // 视为Append. { string currID = view.GetRowCellValue(pos, sortingColumn).ToString(); decimal append = decimal.Parse(currID) + DEF_STEP; return append.ToString(); } else { object currobj = view.GetRowCellValue(pos, sortingColumn); //Sorting最大长度为20. 如果生成的SortingID大於20.则取当前的SortingID if (currobj.ToString().Length >= DEF_MAX_SORTING_LENGTH) return currobj.ToString(); object preobj = view.GetRowCellValue(pos - 1, sortingColumn); if (Convert.ToString(preobj) == "") preobj = (object)0; //如果当前记录是第一条, 那麽上一条记录为0. return GetNewSortingID(currobj.ToString(), preobj.ToString()); } } catch { return DEF_FIRST_SORTING; } } } //原创文章,如转载请注明出处。by www.csframework.com C/S框架网
/// <summary> /// 创建一条明细记录。buttonIndex是Append或Insert的按钮编号 /// </summary> /// <param name="buttonIndex"></param> private void CreateOneDetail(int buttonIndex) { //因需要取OrderID,Append狀態下要將光標移到最後一行方可獲取正确OrderID if (buttonIndex == DetailButtons.Add) gvDetail.MoveLast(); DataRow row = _dataSource.NewRow(); row["Qty"] = 0;//set a default value if (gvDetail.RowCount == 0) buttonIndex = DetailButtons.Add; string order = new GenerateSortID().Generate(gvDetail, colOrderField); row["OrderField"] = order; //排序字段赋值 if (buttonIndex == DetailButtons.Add) { _dataSource.Rows.Add(row); gcDetail.RefreshDataSource(); gvDetail.FocusedRowHandle = gvDetail.RowCount - 1; } else if (buttonIndex == DetailButtons.Insert) { _dataSource.Rows.InsertAt(row, gvDetail.FocusedRowHandle); gvDetail.FocusedRowHandle = gvDetail.FocusedRowHandle - 1; } gvDetail.FocusedColumn = gvDetail.VisibleColumns[0]; } //原创文章,如转载请注明出处。by www.csframework.com C/S框架网
select * from t_product order by OrderField
参考文档:
表格栏位自定义功能窗体(frmGridCustomize.cs) Winform开发框架 - 特殊权限控制(扩展权限和自定义按钮功能) [转帖]FreeTextBox添加自定义按钮 详述IComparer,IComparable接口,实现自定义方法比较对象大小并排序(C#) GridControl/GridView 单元格的值输入非法自定义错误提示文本 C# GridView 自定义单元格的背景色 关于扩展权限、扩展功能按钮及自定义功能点的命名|C/S框架网 如何进行菜单管理、扩展权限、更改自定义按钮名称|C/S框架网 如何扩展窗体的自定义权限按钮 如何删除Toolbar的自定义按钮? 如审核|反审|根据按钮名称删除 解决方案:扩展按钮自定义权限没有出现在权限树给用户勾选 DevExpress GridView表格记录位置调整,实现DataRow记录排序 DevExpress GridControl组件的EmbeddedNavigator属性添加用户自定义按钮 C/S架构快速开发平台提供高级查询功能 - 自定义查询条件搜索数据 模块主窗体怎样控制自定义按钮的权限
其它资料:
什么是C/S结构? | C/S框架核心组成部分 | C/S框架-WebService部署图 | C/S框架-权限管理 | C/S结构系统框架 - 5.1旗舰版介绍 | C/S结构系统框架 - 功能介绍 | C/S结构系统框架 - 产品列表 | C/S结构系统框架 - 应用展示(图) | 三层体系架构详解 | C/S架构轻量级快速开发框架 | C/S框架网客户案例 | WebApi快速开发框架 | C/S框架代码生成器 | 用户授权注册软件系统 | 版本自动升级软件 | 数据库底层应用框架 | CSFramework.CMS内容管理系统 | |