Devexpress GridControl z

时间:2023-03-08 15:40:28
Devexpress GridControl  z
http://minmin86121.blog.163.com/blog/static/4968115720144194923578/
1 AllowNullInput=False;

--Devexpress GridControl GridView 点击 单元格 选中内容

1 GridView-OptionsBehavior-EditorShowMode设置成MouseUp
2
3 另外如果是treeList则设置treeList-OptionsBehavior-ShowEditorOnMouseUp为True

--Devexpress GridControl 向上 向下 最上 最下 排序

Devexpress GridControl  z
  1 private void toolStripButton_向上_Click(object sender, EventArgs e)
2 {
3 UpOrDown(gridView, true);
4 }
5
6 private void toolStripButton_向下_Click(object sender, EventArgs e)
7 {
8 UpOrDown(gridView, false);
9 }
10
11 private void toolStripButton_最下_Click(object sender, EventArgs e)
12 {
13 TopOrLast(gridView, false);
14 }
15
16 private void toolStripButton_最上_Click(object sender, EventArgs e)
17 {
18 TopOrLast(gridView, true);
19 }
20
21 private void UpOrDown(DevExpress.XtraGrid.Views.Grid.GridView GridView, bool ISUpOrDown)
22 {
23 DataRow row = GridView.GetFocusedDataRow();
24 if (ISUpOrDown)
25 {
26 if (!GridView.IsFirstRow)
27 GridView.MovePrev();
28 else
29 return;
30 }
31 else
32 {
33 if (!GridView.IsLastRow)
34 GridView.MoveNext();
35 else
36 return;
37 }
38 DataRow Pretrow = GridView.GetFocusedDataRow();
39 if (Pretrow != null && row != null)
40 {
41 DataRow IRow = dt_F_表格编号.NewRow();
42 for (int k = 0; k < row.ItemArray.Length; k++) IRow[k] = Pretrow[k];
43 for (int i = 0; i < row.ItemArray.Length; i++) Pretrow[i] = row[i];
44 for (int j = 0; j < row.ItemArray.Length; j++) row[j] = IRow[j];
45 }
46 }
47
48 private void TopOrLast(DevExpress.XtraGrid.Views.Grid.GridView GridView, bool ISTopOrLast)
49 {
50 int rowA = GridView.FocusedRowHandle;
51 int rowB = 0;
52 if (ISTopOrLast)
53 {
54 if (!GridView.IsFirstRow)
55 {
56 GridView.MoveFirst();
57 rowB = GridView.FocusedRowHandle;
58 }
59 else
60 return;
61 }
62 else
63 {
64 if (!GridView.IsLastRow)
65 {
66 GridView.MoveLast();
67 rowB = GridView.FocusedRowHandle;
68 }
69 else
70 return;
71 }
72
73 if (rowA!=rowB)
74 {
75 if (rowA < rowB) //最下
76 {
77 for (int z = rowA+1; z <= rowB; z++)
78 {
79 DataRow row = gridView.GetDataRow(rowA);
80 DataRow newrow = gridView.GetDataRow(z);
81 DataRow IRow = dt_F_表格编号.NewRow();
82 for (int k = 0; k < row.ItemArray.Length; k++) IRow[k] = newrow[k];
83 for (int i = 0; i < row.ItemArray.Length; i++) newrow[i] = row[i];
84 for (int j = 0; j < row.ItemArray.Length; j++) row[j] = IRow[j];
85 rowA++;
86 }
87 }
88 else //最上
89 {
90 for (int z = rowA-1; rowB>=0; z--)
91 {
92 DataRow row = gridView.GetDataRow(rowA);
93 DataRow newrow = gridView.GetDataRow(z);
94 DataRow IRow = dt_F_表格编号.NewRow();
95 for (int k = 0; k < row.ItemArray.Length; k++) IRow[k] = newrow[k];
96 for (int i = 0; i < row.ItemArray.Length; i++) newrow[i] = row[i];
97 for (int j = 0; j < row.ItemArray.Length; j++) row[j] = IRow[j];
98 rowA--;
99 if (z == 0) return;
100 }
101 }
102 }
103 }
Devexpress GridControl  z

--Devexpress GridControl 滚动条

1 GridControl-Run Designer-OptionsView-ColunmAutoWidth=False

--Devexpress GridControl 单元格 只读

Devexpress GridControl  z
 1 控件本身不支持单个单元格的只读效果,只能实现整列的只读,因此我们自己虚拟只读效果
2 private void gridView_CustomRowCellEdit(object sender, CustomRowCellEditEventArgs e)
3 {
4 if (e.Column != this.gc)
5 {
6 DataRow dataRow = this.gridView.GetDataRow(e.RowHandle);
7 if ((dataRow != null) && (dataRow[e.Column.FieldName] is DBNull))
8 {
9 e.RepositoryItem = this.repositoryItemHyperLinkEdit_ReadOnly; //这个是一个单元格控件设置成只读 控件为hyperLintEdit才可以设置成不可聚焦TextEidtStyle=HideTextEditor
10 }
11 }
12 }
13
14 private void gridView_RowCellStyle(object sender, RowCellStyleEventArgs e)
15 {
16 if (e.Column != this.gc)
17 {
18 DataRow dataRow = this.gridView.GetDataRow(e.RowHandle);
19 if ((dataRow != null) && (dataRow[e.Column.FieldName] is DBNull))
20 {
21 e.Appearance.BackColor = this.gc.AppearanceCell.BackColor; //设置单元格颜色为灰色 当然也可以不设置
22 }
23 }
24 }
25
26
27 private void InitializeComponent()
28 {
29 this.repositoryItemTextEdit = new RepositoryItemTextEdit();
30 this.repositoryItemTextEdit.BeginInit();
31 this.repositoryItemTextEdit.Appearance.BackColor = Color.FromArgb(0xdf, 0xdf, 0xdf);
32 this.repositoryItemTextEdit.Appearance.Options.UseBackColor = true;
33 this.repositoryItemTextEdit.AppearanceDisabled.BackColor = Color.FromArgb(0xdf, 0xdf, 0xdf);
34 this.repositoryItemTextEdit.AppearanceDisabled.Options.UseBackColor = true;
35 this.repositoryItemTextEdit.AppearanceFocused.BackColor = Color.FromArgb(0xdf, 0xdf, 0xdf);
36 this.repositoryItemTextEdit.AppearanceFocused.Options.UseBackColor = true;
37 this.repositoryItemTextEdit.AutoHeight = false;
38 this.repositoryItemTextEdit.Enabled = false;
39 this.repositoryItemTextEdit.Name = "repositoryItemTextEdit4";
40 this.repositoryItemTextEdit.ReadOnly = true;
41 this.repositoryItemTextEdit.AllowNullInput=false; //不允许空值
42 this.repositoryItemTextEdit.EndInit();
43 }
Devexpress GridControl  z

--Devexpress GridControl 多行复制

Devexpress GridControl  z
 1 Devexprss GridControl 多行复制:
2 1.GridControl设计器Views下
3 OptionsBehavior-CopyToClipboardWithColumnHeaders=False;
4 OptionsSelection-MultiSelect=True;
5 OptionsSelection-MultiSelectMode=RowSelect;
6 2.添加如下代码
7 private void Table3Three_gridView_ShowingEditor(object sender, CancelEventArgs e)
8 {
9 e.Cancel = isCancel;
10 }
11 bool isCancel = true;
12 private void Table3Three_gridView_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
13 {
14 isCancel = false;
15 }
16 private void Table3Three_gridView_KeyDown(object sender, KeyEventArgs e)
17 {
18 //Ctrl+V粘贴
19 if (e.Modifiers.CompareTo(Keys.Control) == 0 && e.KeyCode == Keys.V)
20 {
21 object _objData = null;
22 IDataObject dataObj = Clipboard.GetDataObject();
23
24 if (dataObj.GetDataPresent(DataFormats.Text))
25 _objData = dataObj.GetData(DataFormats.Text);
26
27 if (_objData != null)
28 {
29 int RowNumber = 0;
30
31 string _tempStr = _objData.ToString();
32
33 string[] _split = { "\r\n" };
34 string[] _arrayStr = _tempStr.Split(_split, StringSplitOptions.None);
35
36 for (int i = 0; i < _arrayStr.Length - 1; i++)
37 {
38 string[] _arrayStr2 = _arrayStr[i].Split('\t');
39
40 Table3Three_gridView.AddNewRow();
41 Table3Three_gridView.MoveLastVisible();
42 RowNumber = Table3Three_gridView.FocusedRowHandle;
43
44 Table3Three_gridView.SetRowCellValue(RowNumber, Table3Three_gridView.Columns["定额编号"], _arrayStr2[0]);
45 Table3Three_gridView.SetRowCellValue(RowNumber, Table3Three_gridView.Columns["定额名称"], _arrayStr2[1]);
46 Table3Three_gridView.SetRowCellValue(RowNumber, Table3Three_gridView.Columns["单位"], _arrayStr2[2]);
47 Table3Three_gridView.SetRowCellValue(RowNumber, Table3Three_gridView.Columns["名称"], _arrayStr2[3]);
48 Table3Three_gridView.SetRowCellValue(RowNumber, Table3Three_gridView.Columns["单位台班"], _arrayStr2[4]);
49 Table3Three_gridView.SetRowCellValue(RowNumber, Table3Three_gridView.Columns["计算数量"], _arrayStr2[5]);
50 Table3Three_gridView.SetRowCellValue(RowNumber, Table3Three_gridView.Columns["台班单价"], _arrayStr2[6]);
51 Table3Three_gridView.SetRowCellValue(RowNumber, Table3Three_gridView.Columns["备注"], _arrayStr2[7]);
52 Table3Three_gridView.SetRowCellValue(RowNumber, Table3Three_gridView.Columns["仪器仪表编码"], _arrayStr2[8]);
53 Table3Three_gridView.SetRowCellValue(RowNumber, Table3Three_gridView.Columns["总数量"], _arrayStr2[9]);
54 Table3Three_gridView.SetRowCellValue(RowNumber, Table3Three_gridView.Columns["台班合价"], _arrayStr2[10]);
55 Table3Three_gridView.SetRowCellValue(RowNumber, Table3Three_gridView.Columns["关联"], _arrayStr2[11]);
56 }
57 isCancel = true;
58 App_Code.Global.isChange = "5";
59 }
60 }
61 }
Devexpress GridControl  z

--Devexpress GridControl AdvBandedGridView 数据多行显示

Devexpress GridControl  z
 1 实现效果:
2 |基本信息| 费率 |
3 | 名称 | 运杂费 |采购费 |
4 | |优惠系数|优惠系数|
5 | 第1段 | 2.2% | 1.0% |
6 | | 80% |90% |
7
8 操作:
9 1.GridView设计器“ClickHereToChangeView-ConvertTo-advBandedGridView”
10 2.GridControl设计器中Bands新增“基本信息”和“费率”两个Band
11 3.GridControl设计器中Bands中ShowColumnsSelector并拖动“运杂费”“采购费”“优惠系数”“优惠系数”到各自的Bands
12 3.GridControl设计器中Bands拖动2个优惠系数分别到第二行
13 4.GridControl设计器中Columns修改“名称”列rowcount属性为2
14 5.GridControl设计器中Bands和Columns所有列的MinWidth=100
15 6.GridControl设计器中Bands和Columns所有列的AllowMove=false
16 7.GridControl设计器中Bands中AutoWidth的勾去掉
Devexpress GridControl  z

--Devexpress GridControl RepositoryItemComboBox 只读

1 GridControl 的RepositoryItemComboBox 控件,如果需要设置“只读+下拉”方法如下:
2 GridControl设计器-Columns-某字段-Column properties-ColumnEdit-TextEditStyle=DisableTextEditor

--Devexpress GridControl 统计技巧

1 比如对“数量”列进行统计,只要在GridControl的设计器中设置SummaryItem:
2 SummaryItem.DisplayFormat = "{0:f2}";
3 SummaryItem.FieldName = "数量";
4 SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Sum;

--Devexpress GridControl 公式使用技巧

Devexpress GridControl  z
 1 1.GridControl设计器 //由于数据库的列“合价”不能既有值又绑定公式,所以我们引入一个虚拟的列“小计”
2 SQL语句“select 单价,数量,合价 from 表名”
3 数据库真实列“单价”“数量”“合价”,非真实列“小计”
4 其中“小计”UnboundExpression=[单价] * [数量]
5 其中“小计”Visible=False //隐藏该虚拟列
6 2.后台代码 //把“小计”计算的值写入“合价”单元格
7 private void gridView_CellValueChanged(object sender, CellValueChangedEventArgs e)
8 {
9 if (gridView.FocusedRowHandle >= 0)
10 {
11 if (e.Column.Caption.Equals("数量") || e.Column.Caption.Equals("单价"))
12 {
13 //设置结果值
14 gridView.SetRowCellValue(gridView.FocusedRowHandle, gridView.Columns["合价"], gridView.GetFocusedRowCellValue("小计").ToString());
15 }
16 }
17 }
Devexpress GridControl  z

--Devexpress GridControl 屏蔽右键

1 optionmenu-》enablegrouppanelmenu类似参数为false

--Devexpress GridControl 全选

Devexpress GridControl  z
1.如图添加DevExpress.XtraEditors.CheckEdit,设置Size(18, 19)
2.设置GridControl首列“check”属性MinWidth,MaxWidth为20
3.添加代码

1         private void checkBox_All_CheckStateChanged(object sender, EventArgs e)
2 {
3 foreach (DataRow dr in dt_EvaluateProjectT.Rows)
4 dr["check"]=(sender as DevExpress.XtraEditors.CheckEdit).CheckState;
5 }