/// <summary> /// 得到分页数据,同时把绑定到DataGridView控件中。 /// </summary> /// <param name="dataControl">显示分页数据的控件(一般为DataGridView)</param> /// <param name="pageControl">分页控件名称</param> /// <param name="tableName">表名</param> /// <param name="pageDbType">数据库类型</param> /// <param name="primaryKey">主键名</param> /// <param name="orderField">排序字段</param> /// <param name="whereConditional">Where条件</param> /// <param name="queryFields">结果字段列表</param> /// <param name="connstring">数据库连接字符串</param> /// <returns></returns> public int BindPageData(UcDataGridView dataControl, UcPageControl pageControl, string tableName, CurrentDbType pageDbType , string primaryKey = "ID", string orderField = "ID", string whereConditional = "DELETEMARK = 0" , string queryFields = "*", string connstring = "") { pageData = null; pageDataTable = null; pageData = new PageData(); pageDataTable = new DataTable(); pageData.TableName = tableName; pageData.PrimaryKey = primaryKey; pageData.OrderStr = orderField; pageData.PageIndex = pageControl.PageCurrent; pageData.PageSize = SystemInfo.PageSize; //指定分页大小(注:此句一定不能省略) pageControl.PageSize = pageData.PageSize; //指定此页面的分页大小(注:此句一定不能省略) pageData.QueryCondition = whereConditional; pageData.QueryFieldName = queryFields; pageData.PageDbType = pageDbType; pageDataTable = pageData.QueryDataTable(!string.IsNullOrEmpty(connstring.Trim()) ? connstring : SystemInfo.RDIFrameworkDbConection); pageControl.bindingSource.DataSource = pageDataTable; pageControl.bindingNavigator.BindingSource = pageControl.bindingSource; //((DataGridView)dataControl).DataSource = null; //((DataGridView)dataControl).AutoGenerateColumns = false; //((DataGridView)dataControl).DataSource = ((DataTable)pageControl.bindingSource.DataSource).DefaultView; //this.AddCheckBoxColumn(((DataGridView)dataControl)); dataControl.DataSource = null; dataControl.AutoGenerateColumns = false; dataControl.DataSource = pageControl.bindingSource; this.AddCheckBoxColumn(dataControl); pageControl.PageCount = pageData.TotalCount; return(pageData.TotalCount); }
private void SetColIndex(UcDataGridView grid, string colName, int?displayIdx, int?width) { #region 变量设置 SYS_UserGridColIndex findGridIdx = null; SYS_UserGridColWidth findGridWdth = null; Guid?companyID = MySession.CompanyID; Guid?userGuid = MySession.UserGUID; #endregion #region 表格所在表单、容器、表格名称,获取表格对象 SYS_DataGridResult gridSet = new SYS_DataGridResult(); string formName = grid.FindForm().Name; string gridName = grid.Name; string parentName = ""; Control parentCon = grid.Parent; while (parentCon != null && parentCon.GetType().BaseType != typeof(ucBaseUserControl)) { parentCon = parentCon.Parent; } if (parentCon != null) { parentName = (parentCon as ucBaseUserControl).Name; } gridSet = GetGridSet(gridName, parentName, formName); #endregion #region 设置列显示索引值 if (displayIdx != null) { findGridIdx = gridSet.ColIndexList.Find(a => a.ColName == colName && a.CompanyID == companyID && a.UserGUID == userGuid); if (findGridIdx == null) { findGridIdx = new SYS_UserGridColIndex(); gridSet.ColIndexList.Add(findGridIdx); } findGridIdx.ColName = colName; findGridIdx.DisplayIndex = displayIdx; findGridIdx.CompanyID = companyID; findGridIdx.UserGUID = userGuid; findGridIdx.DGGUID = gridSet.DGGUID; } #endregion #region 设置列显示宽值 if (width != null) { findGridWdth = gridSet.ColWidthList.Find(a => a.ColName == colName && a.CompanyID == companyID && a.UserGUID == userGuid); if (findGridWdth == null) { findGridWdth = new SYS_UserGridColWidth(); gridSet.ColWidthList.Add(findGridWdth); } findGridWdth.ColName = colName; findGridWdth.ColWidth = width; findGridWdth.CompanyID = companyID; findGridWdth.UserGUID = userGuid; findGridWdth.DGGUID = gridSet.DGGUID; } #endregion }
/// <summary> /// 隐藏表格列 /// </summary> /// <param name="dgv"></param> /// <param name="colName"></param> public static void HideGridColumn(UcDataGridView dgv, string colName) { foreach (DataGridViewColumn dgvc in dgv.Columns) { if (dgvc.Name == colName) { dgvc.Visible = false; } } }
protected void SetGridCheck(UcDataGridView grid, string idFieldName, string chkFieldName) { string entityIDName = grid.Columns[idFieldName].DataPropertyName; foreach (DataGridViewRow row in grid.Rows) { if (_lstSelected.Exists(a => a.GetType().BaseType.GetProperty(entityIDName).GetValue(a, null).ToStringHasNull() == row.Cells[idFieldName].Value.ToStringHasNull())) { row.Cells[chkFieldName].Value = true; } } }
protected void SelectAll(UcDataGridView grid, UcCheckBox chkBox, string colChkName, string idColName, string chkColName) { if (!_isMutiSelect) { this.ShowMessage("您当前是单选模式,不允许全部选择!"); return; } for (int i = 0; i < grid.Rows.Count; i++) { grid.Rows[i].Cells[chkColName].Value = chkBox.Checked; SetCheck(grid, i, chkBox.Checked, idColName, chkColName); } }
protected void SelectAll(UcDataGridView grid, UcCheckBox chkAll, string idFieldName, string chkFieldName) { //if (!_isMutiSelect) //{ // this.ShowMessage("您当前是单选模式,不允许全部选择!"); // return; //} for (int i = 0; i < grid.Rows.Count; i++) { grid.Rows[i].Cells[chkFieldName].Value = chkAll.Checked; SetCheck(grid, i, chkAll.Checked, idFieldName, chkFieldName); } }
protected void InitBaseSelectForm(bool isMutiSelect, UcDataGridView grid, UcCheckBox chkAll, string idColName, string chkColName) { _isMutiSelect = isMutiSelect; DataGridViewCheckBoxColumn chkSlc = new DataGridViewCheckBoxColumn() { ReadOnly = false, Width = 30, Frozen = true, Name = chkColName, HeaderText = "" }; grid.Columns.Insert(0, chkSlc); #region 全选择功能控钮赋加事件 if (chkAll != null && _isMutiSelect) { chkAll.Visible = true; int x = 0, y = 0; x = grid.Left + grid.Columns[chkColName].Width; y = grid.Top + 10; //chkAll.Location = new Point(x + 5, y); chkAll.BringToFront(); chkAll.Visible = true; chkAll.Dock = DockStyle.None; //chkAll.Size = new Size(15, 15); chkAll.CheckedChanged += delegate(object sender, EventArgs e) { SelectAll(grid, chkAll, idColName, chkColName); }; } else if (chkAll != null && !_isMutiSelect) { chkAll.Visible = false; } #endregion #region 表格赋加事件 grid.CellContentClick += delegate(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex < 0 || e.ColumnIndex < 0 || grid == null || grid.Columns[e.ColumnIndex].Name.IndexOf(chkColName) < 0) { return; } bool isChk = grid[chkColName, e.RowIndex].EditedFormattedValue.ToBooleanHasNull(); SetCheck(grid, e.RowIndex, isChk, idColName, chkColName); }; #endregion }
protected void SetCheck(UcDataGridView grid, int rowIndex, bool isChecked, string idFieldName, string chkFieldName) { bool isChk = isChecked; #region 单选和多选控制不同状态 if (!_isMutiSelect) { for (int i = 0; i < grid.Rows.Count; i++) { if (i != rowIndex && isChk) { grid[chkFieldName, i].Value = false; } } } #endregion #region 设置选中状态的值 string entityIDName = grid.Columns[idFieldName].DataPropertyName; string id = grid.Rows[rowIndex].Cells[idFieldName].Value.ToStringHasNull(); object findInfo, findSlcInfo; findInfo = _lstDataSource.Find(a => a.GetType().BaseType.GetProperty(entityIDName).GetValue(a, null).ToStringHasNull() == id); if (findInfo == null) { return; } findSlcInfo = _lstSelected.Find(a => a.GetType().BaseType.GetProperty(entityIDName).GetValue(a, null).ToStringHasNull() == id); if (isChk) { if (!_isMutiSelect) { _lstSelected.Clear(); } if (findSlcInfo == null) { _lstSelected.Add(findInfo); } } else { if (findSlcInfo != null) { _lstSelected.Remove(findSlcInfo); } } #endregion }
public static void SetColName(UcDataGridView grid, string colNameCode, string colName, string customColName, bool isShow) { #region 变量设置 SYS_UserGridColName finGridName = null; Guid?companyID = MySession.CompanyID; Guid?userGuid = MySession.UserGUID; #endregion #region 表格所在表单、容器、表格名称,获取表格对象 SYS_DataGridResult gridSet = new SYS_DataGridResult(); string formName = grid.FindForm().Name; string gridName = grid.Name; string parentName = ""; Control parentCon = grid.Parent; while (parentCon != null && parentCon.GetType().BaseType != typeof(ucBaseUserControl)) { parentCon = parentCon.Parent; } if (parentCon != null) { parentName = (parentCon as ucBaseUserControl).Name; } gridSet = GetGridSet(gridName, parentName, formName); #endregion #region 设置列自定义列名 if (!string.IsNullOrEmpty(customColName)) { finGridName = gridSet.ColNameList.Find(a => a.ColNameCN_Default == colName && a.CompanyID == companyID); if (finGridName == null) { finGridName = new SYS_UserGridColName(); gridSet.ColNameList.Add(finGridName); } finGridName.ColNameCode = colNameCode; finGridName.ColNameCN_Default = colName; finGridName.ColNameCN_Custom = customColName; finGridName.ColIsShow = isShow; finGridName.CompanyID = companyID; finGridName.UserGUID = userGuid; finGridName.DGGUID = gridSet.DGGUID; } #endregion }
/// <summary> /// 设置页面控件上的多语言信息 /// </summary> /// <param name="targetForm">目标页面</param> /// <returns>设置多语言的控件个数</returns> public static int SetLanguageResource(Form targetForm) { int returnValue = 0; string key = string.Empty; string language = string.Empty; // 窗体的名字 key = targetForm.Name; language = ResourceManagerWrapper.Instance.Get(key); if (!string.IsNullOrEmpty(language)) { targetForm.Text = language; } //控件ContextMenuStrip不在controls集合里,所以必须用反射取得; System.Reflection.FieldInfo[] fieldInfo = targetForm.GetType().GetFields(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); for (int i = 0; i < fieldInfo.Length; i++) { if (fieldInfo[i].FieldType.Name.Equals("UcOrganizeSelect") || fieldInfo[i].FieldType.Name.Equals("UcUserSelect") || fieldInfo[i].FieldType.Name.Equals("UcPagerEx") || fieldInfo[i].FieldType.Name.Equals("UCStartPage") ) { UserControl userControl = (UserControl)fieldInfo[i].GetValue(targetForm); returnValue += SetLanguageResource(userControl); } if (fieldInfo[i].FieldType.Name.Equals("ToolStripMenuItem") || fieldInfo[i].FieldType.Name.Equals("ToolStripButton") || fieldInfo[i].FieldType.Name.Equals("ToolStripDropDownButton") || fieldInfo[i].FieldType.Name.Equals("ToolStripLabel")) { ToolStripItem toolStripMenuItem = (ToolStripItem)fieldInfo[i].GetValue(targetForm); key = targetForm.Name + "_" + toolStripMenuItem.Name; language = ResourceManagerWrapper.Instance.Get(key); if (!string.IsNullOrEmpty(language)) { toolStripMenuItem.Text = language; toolStripMenuItem.ToolTipText = Regex.Replace(language, @"\([^\)]+\)", "", RegexOptions.IgnoreCase); returnValue++; } } if (fieldInfo[i].FieldType.Name.Equals("NotifyIcon")) { NotifyIcon notifyIcon = (NotifyIcon)fieldInfo[i].GetValue(targetForm); key = targetForm.Name + "_notifyIcon"; language = ResourceManagerWrapper.Instance.Get(key); if (!string.IsNullOrEmpty(language)) { notifyIcon.BalloonTipText = language; notifyIcon.BalloonTipTitle = language; notifyIcon.Text = language; returnValue++; } } if ((fieldInfo[i].FieldType.Name.Equals("Label")) || (fieldInfo[i].FieldType.Name.Equals("LinkLabel")) || (fieldInfo[i].FieldType.Name.Equals("CheckBox")) || (fieldInfo[i].FieldType.Name.Equals("Button")) || (fieldInfo[i].FieldType.Name.Equals("GroupBox")) || (fieldInfo[i].FieldType.Name.Equals("RadioButton")) || (fieldInfo[i].FieldType.Name.Equals("TabPage")) || (fieldInfo[i].FieldType.Name.Equals("UcButton"))) { returnValue += ControlCommonLanageSet <Control>(targetForm, fieldInfo[i]); } if (fieldInfo[i].FieldType.Name.Equals("ButtonItem")) { returnValue += DevControlCommonLanageSet <ButtonItem>(targetForm, fieldInfo[i]); } if (fieldInfo[i].FieldType.Name.Equals("LabelItem")) { returnValue += DevControlCommonLanageSet <LabelItem>(targetForm, fieldInfo[i]); } if (fieldInfo[i].FieldType.Name.Equals("TabItem")) { TabItem control = (TabItem)fieldInfo[i].GetValue(targetForm); key = targetForm.Name + "_" + control.Name; language = ResourceManagerWrapper.Instance.Get(key); if (!string.IsNullOrEmpty(language)) { control.Text = language; returnValue++; } } if (fieldInfo[i].FieldType.Name.Equals("SuperTabItem")) { BaseItem control = (BaseItem)fieldInfo[i].GetValue(targetForm); key = targetForm.Name + "_" + control.Name; language = ResourceManagerWrapper.Instance.Get(key); if (!string.IsNullOrEmpty(language)) { control.Text = language; returnValue++; } } // 对表格的列名多语言处理 及列宽处理 if (fieldInfo[i].FieldType.Name.Equals("DataGridView")) { DataGridView targetDataGridView = (DataGridView)fieldInfo[i].GetValue(targetForm); for (int j = 0; j < targetDataGridView.ColumnCount; j++) { key = targetForm.Name + "_" + targetDataGridView.Columns[j].Name; language = ResourceManagerWrapper.Instance.Get(key); if (!string.IsNullOrEmpty(language)) { targetDataGridView.Columns[j].HeaderText = language; returnValue++; } } } if (fieldInfo[i].FieldType.Name.Equals("UcDataGridView")) { UcDataGridView targetDataGridView = (UcDataGridView)fieldInfo[i].GetValue(targetForm); for (int j = 0; j < targetDataGridView.ColumnCount; j++) { key = targetForm.Name + "_" + targetDataGridView.Columns[j].Name; language = ResourceManagerWrapper.Instance.Get(key); if (!string.IsNullOrEmpty(language)) { targetDataGridView.Columns[j].HeaderText = language; returnValue++; } } } } return(returnValue); }
/// <summary> /// 得到分页数据,同时把绑定到DataGridView控件中。 /// </summary> /// <param name="dataControl">显示分页数据的控件(一般为DataGridView)</param> /// <param name="pageControl">分页控件名称</param> /// <param name="tableName">表名</param> /// <param name="pageDbType">数据库类型</param> /// <param name="connstring">数据库连接字符串</param> /// <returns></returns> public int BindPageData(UcDataGridView dataControl, UcPageControl pageControl, string tableName, CurrentDbType pageDbType, string connstring) { return(this.BindPageData(dataControl, pageControl, tableName, pageDbType, BusinessLogic.FieldId, BusinessLogic.FieldId, "DELETEMARK = 0", "*", connstring)); }
public void BindApplyGridEvent(UcDataGridView grid, bool isDefaultCellMouseEvent = true) { if (grid == null) { return; } #region 统一设置表头高度及列头宽度 grid.RowHeadersWidth = 30; grid.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing; grid.ColumnHeadersHeight = 32; grid.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing; #endregion grid.ColumnWidthChanged += new DataGridViewColumnEventHandler(grid_ColumnWidthChanged); grid.ColumnDisplayIndexChanged += new DataGridViewColumnEventHandler(grid_ColumnDisplayIndexChanged); grid.DataBindingComplete += new DataGridViewBindingCompleteEventHandler(grid_DataBindingComplete); if (isDefaultCellMouseEvent) { if (grid.FindForm() != null && grid.FindForm().Name != "frmStockSearch") { grid.CellMouseEnter += new DataGridViewCellEventHandler(grid_CellMouseEnter); grid.CellMouseLeave += new DataGridViewCellEventHandler(grid_CellMouseLeave); } else { if (grid.Name != "grdStock") //库存查询表格需单独处理 { grid.CellMouseEnter += new DataGridViewCellEventHandler(grid_CellMouseEnter); grid.CellMouseLeave += new DataGridViewCellEventHandler(grid_CellMouseLeave); } } } ApplyGridSet(grid); #region 统一控制客户和供应商权限 if (!MySession.IsMainAccount && MySession.EnabledSubAccountRole) { if (MySession.OwnerRoles != null && MySession.OwnerRoles.Count > 0) { string extSupplierInfo = ",供应商,供应商名称,供应商信息,"; string extCustomerInfo = ",客户,客户名称,客户信息,"; foreach (DataGridViewColumn dgvc in grid.Columns) { if (((extSupplierInfo.IndexOf("," + dgvc.HeaderText + ",") != -1 || dgvc.DataPropertyName.IndexOf("SupplierName") != -1) && !MySession.OwnerRoles.Contains("Mod.供应商管理")) || (extCustomerInfo.IndexOf("," + dgvc.HeaderText + ",") != -1 || dgvc.DataPropertyName.IndexOf("CustomerName") != -1) && !MySession.OwnerRoles.Contains("Mod.客户管理")) { ZNLCRM.UI.Logic.Sys.GridUserSetting.HideGridColumn(grid, dgvc.Name); } } } } #endregion #region 汇总行处理 if (grid is UcDataGridViewSummary) { UcDataGridViewSummary ucDgvSummary = grid as UcDataGridViewSummary; if (ucDgvSummary.SummaryRowVisible == true && ucDgvSummary.DisplaySumRowHeader == true && ucDgvSummary.SummaryColumns.Length > 0) { ucDgvSummary.CreateSummaryRow(); //创建汇总行 } } #endregion }
public void ApplyGridSet(UcDataGridView grid) { #region 得到表格设置 _isSettingCol = true; if (grid == null || grid.FindForm() == null) { return; } string formName = grid.FindForm().Name; string gridName = grid.Name; string parentName = ""; Control parentCon = grid.Parent; while (parentCon != null && parentCon.GetType().BaseType != typeof(ucBaseUserControl)) { parentCon = parentCon.Parent; } if (parentCon != null) { parentName = (parentCon as ucBaseUserControl).Name; } SYS_DataGridResult gridSet = GetGridSet(gridName, parentName, formName); #endregion #region 设置表列显示索引值 if (gridSet.ColIndexList != null && gridSet.ColIndexList.Count > 0) { foreach (SYS_UserGridColIndex info in gridSet.ColIndexList) { if (grid.Columns[info.ColName] == null || grid.Columns[info.ColName].Frozen) { continue; //冻结列不处理 } if (grid.Columns.Count > info.DisplayIndex && grid.Columns[info.ColName] != null) { try { grid.Columns[info.ColName].DisplayIndex = info.DisplayIndex.GetValueOrDefault(0); } catch { } } } } #endregion #region 设置表列显示宽度值 if (gridSet.ColWidthList != null && gridSet.ColWidthList.Count > 0) { foreach (SYS_UserGridColWidth info in gridSet.ColWidthList) { if (grid.Columns[info.ColName] == null) { continue; } if (grid.Columns[info.ColName] != null) { grid.Columns[info.ColName].Width = info.ColWidth.GetValueOrDefault(5); } } } #endregion #region 设置自定义列名及是否显示 if (gridSet.ColNameList != null && gridSet.ColNameList.Count > 0) { foreach (SYS_UserGridColName info in gridSet.ColNameList) { if (grid.Columns[info.ColNameCode] == null) { continue; } if (grid.Columns[info.ColNameCode] != null) { if (!string.IsNullOrEmpty(info.ColNameCN_Custom)) { grid.Columns[info.ColNameCode].HeaderText = info.ColNameCN_Custom; } if (info.ColIsShow.HasValue) { grid.Columns[info.ColNameCode].Visible = info.ColIsShow.Value; } } } } #endregion _isSettingCol = false; }