1 using System;
2 using System.Collections.Generic;
3 using System.Drawing;
4 using System.Linq;
5 using System.Text;
6 using System.Threading.Tasks;
7 using System.Windows.Forms;
8
9 namespace Formes
10 {
11
12 //*DataGridView控件用法合集 */
13 class DataGridView控件用法合集 : System.Windows.Forms.Form
14 {
15 // public DataGridView dataGridView1 = new DataGridView();
16 // protected override void OnLofdad(EventArgs e)
17 // {
18
19
20 // this.AutoSize = true;
21 // this.Controls.Add(this.dataGridView1);
22 // this.Text = "DataGridView virtual-mode just-in-time demo";
23
24 // // Complete the initialization of the DataGridView.
25 // this.dataGridView1.Size = new Size(800, 250);
26 // this.dataGridView1.Dock = DockStyle.Fill;
27 // this.dataGridView1.VirtualMode = true;
28 // this.dataGridView1.ReadOnly = true;
29 // this.dataGridView1.AllowUserToAddRows = false;
30 // this.dataGridView1.AllowUserToOrderColumns = false;
31 // this.dataGridView1.SelectionMode =
32 // DataGridViewSelectionMode.FullRowSelect;
33 // }
34 protected void OnLodad()
35 {
36
37 #region
38 // 1. DataGridView当前的单元格属性取得、变更
39
40 //2. DataGridView编辑属性
41
42 //3. DataGridView最下面一列新追加行非表示
43
44 //4. DataGridView判断当前选中行是否为新追加的行
45
46 //5. DataGridView删除行可否设定
47
48 //6. DataGridView行列不表示和删除
49
50 #endregion
51
52 #region 1.当前的单元格属性取得、变更
53
54 //[C#]
55
56 //'当前选中单元的值
57
58 //Console.WriteLine(this.DataGridView1.CurrentCell.Value);
59
60 ////'当前列的Index值
61
62 //Console.WriteLine(this.DataGridView1.CurrentCell.ColumnIndex);
63
64 //'当前单元的行Index值
65
66 //Console.WriteLine(DataGridView1.CurrentCell.RowIndex)
67
68 //'将控件中(0, 0)处的值,赋给当前单元格.
69
70 //DataGridView1.CurrentCell = DataGridView1[0, 0]
71
72 #endregion
73
74 #region 2.DataGridView编辑属性
75
76 //全部单元格编辑属性
77
78 //[C#]
79
80 //'DataGridView1只读属性
81
82 //DataGridView1.ReadOnly = True
83
84 //指定行列单元格编辑属性
85
86 //[C#]
87
88
89
90 //DataGridView1.Columns[1]ReadOnly = True
91
92
93
94 //DataGridView1.Rows[2].ReadOnly = True
95
96
97
98 //DataGridView1[0, 0].ReadOnly = True
99
100 //根据条件判断单元格的编辑属性
101
102 //下例中column2的值是True的时候,Column1设为可编辑
103
104 // [C#]
105
106
107
108 //代码
109
110
111
112
113 #endregion
114
115 #region 3.DataGridView最下面一列新追加行非表示
116
117 //[C#]
118
119
120
121 //DataGridView1.AllowUserToAddRows = False
122
123 #endregion
124
125 #region 4.判断当前选中行是否为新追加的行
126
127 //[C#]
128
129 //if (DataGridView1.CurrentRow.IsNewRow)
130 //{
131 // Console.WriteLine("当前行,是新添加的行");
132 //}
133 // else
134 //{
135 // Console.WriteLine("当前行,不是新添加的行");
136 //}
137
138 #endregion
139
140 #region 5. DataGridView删除行可否设定
141
142 //[C#]
143
144
145
146 //DataGridView1.AllowUserToDeleteRows = False
147
148 //根据条件判断当前行是否要删除
149
150 //[C#]
151
152
153
154
155
156 //复制代码
157 //代码
158 // 1 private void DataGridView1_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
159 // 2 {
160 // 3
161 // 4
162 // 5
163 // 6 if (MessageBox.Show("确定要删除吗?", "删除确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Question).Equals(System.Windows.Forms.DialogResult.OK))
164 // 7 {
165 // 8
166 // 9 }
167 //10 else
168 //11 {
169 //12 e.Cancel = true;
170 //13 }
171 //14 }
172 //复制代码
173
174
175
176
177 #endregion
178
179 #region 6. DataGridView行列不表示和删除
180
181 //行列不表示
182
183 //[C#]
184
185 //'DataGridView1的第一列不表示
186
187 //DataGridView1.Columns[0].Visible = False
188
189 //'DataGridView1的第一行不表示
190
191 //DataGridView1.Rows[0].Visible = False
192
193 //行列表头部分不表示
194
195 //[C#]
196 //DataGridView1.ColumnHeadersVisible = False
197
198
199
200 //DataGridView1.RowHeadersVisible = False
201
202 //指定行列删除
203
204 //[C#]
205
206
207
208 //DataGridView1.Columns.Remove("Column1")
209
210
211
212 //DataGridView1.Columns.RemoveAt(0)
213
214
215
216 //DataGridView1.Rows.RemoveAt(0)
217
218 //选择的行列删除(多行列)
219
220 //[C#]
221
222 //'DataGridView1删除选中的行
223
224 //foreach (DataGridViewRow r in DataGridView1.SelectedRows)
225 // {
226 // if (!r.IsNewRow)
227 // {
228 // DataGridView1.Rows.Remove(r);
229 // }
230 // }
231
232 #endregion
233
234 #region 7. DataGridView行列宽度高度设置为不能编辑
235
236 //8. DataGridView行高列幅自动调整
237
238 //9. DataGridView指定行列冻结
239
240 //10. DataGridView列顺序变更可否设定
241
242 //11. DataGridView行复数选择
243
244 //12. DataGridView选择的行、列、单元格取得
245
246
247
248 #endregion
249
250 #region 7. DataGridView行列宽度高度设置为不能编辑
251
252 // [C#]
253
254 //'DataGridView1的列的宽设为不能编辑
255
256 //DataGridView1.AllowUserToResizeColumns = False
257
258 //'DataGridView1的行的高设为不能编辑
259
260 //DataGridView1.AllowUserToResizeRows = False
261
262 //指定行列宽度高度设置为不能编辑
263
264 //[C#]
265
266 //'DataGridView1指定列宽度设置为不能编辑
267
268 //DataGridView1.Columns[0].Resizable = DataGridViewTriState.False
269
270 //'DataGridView1指定行高度设置为不能编辑
271
272 //DataGridView1.Rows[0].Resizable = DataGridViewTriState.False
273
274 //列幅行高最小值设定
275
276 //[C#]
277
278 //'列幅最小值设定为100
279
280 //DataGridView1.Columns[0].MinimumWidth = 100
281
282 //'行高最小值设定为50
283
284 //DataGridView1.Rows[0].MinimumHeight = 50
285
286 //行列表头部分行高列幅设置为不能编辑
287
288 //[C#]
289
290 //行列表头部分行高设置为不能编辑
291
292 //DataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing
293
294 //行列表头部分列幅设置为能编辑
295
296 //DataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.EnableResizing
297
298 #endregion
299
300 #region 8.DataGridView行高列幅自动调整
301
302 //[C#]
303
304 //根据内容, 列幅自动调整
305
306 //DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
307
308 //根据内容, 行高自动调整
309
310 //DataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
311
312 //表头部分行高列幅自动调整
313
314 //[C#]
315
316 //'表头列高自動調整
317
318 //DataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize
319
320 //'表头行幅自動調整
321
322 //DataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders
323
324 //指定列自动调整
325
326 //[C#]
327
328 //'指定列的列幅自動調整
329
330 //DataGridView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
331
332 #endregion
333
334 #region 9.DataGridView指定行列冻结
335
336 //列冻结(当前列以及左侧做所有列)
337
338 //[C#]
339
340 //'DataGridView1的左側2列固定
341
342 //DataGridView1.Columns[1].Frozen = True
343
344 //行冻结(当前行以及上部所有行)
345
346 //[C#]
347
348 //'DataGridView1的上部2行固定
349
350 //DataGridView1.Rows[2].Frozen = True
351
352 //指定单元格冻结(单元格所在行上部分所有行,列左侧所有列)
353
354 //[C#]
355
356 //DataGridView1[0, 0].Frozen = True
357
358 #endregion
359
360 #region 10.DataGridView列顺序变更可否设定
361
362 //[C#]
363
364 //'DataGridView1的列的位置设定为允许改变
365
366 //DataGridView1.AllowUserToOrderColumns = True
367
368 //但是如果列冻结的情况下,冻结的部分不能变更到非冻结的部分。
369
370 //变更后列位置取得
371
372 //[C#]
373
374 //'取得列"Column1"現在的位置
375
376 //Console.WriteLine(DataGridView1.Columns["Column1"].DisplayIndex)
377
378 //'列"Column1"移動到最前面
379
380 //DataGridView1.Columns["Column1"].DisplayIndex = 0
381
382 #endregion
383
384
385 #region 11.DataGridView行复数选择
386
387 //不可选择多行
388
389 //[C#]
390
391 //'DataGridView1不可选择多行
392
393 //DataGridView1.MultiSelect = False
394
395 //单元格选择的时候默认为选择整行
396
397 //[C#]
398
399 //'单元格选择的时候默认为选择整行
400
401 //DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
402
403 #endregion
404
405 #region 12.DataGridView选择的行、列、单元格取得
406
407 //[C#]
408
409 //'输出选择的单元格位置
410
411 //Console.WriteLine("选择的单元格位置")
412
413 //foreach (DataGridViewCell c in DataGridView1.SelectedCells)
414
415 //{
416
417 // Console.WriteLine(c.ColumnIndex + "," + c.RowIndex);
418
419 //}
420
421 //'输出选择的行位置
422
423 //Console.WriteLine("选择的行位置")
424
425 //foreach (DataGridViewRow r in dgvBeforeStep.SelectedRows)
426
427 //{
428
429 // Console.WriteLine(r.Index);
430
431 //}
432
433 //''输出选择的列位置
434
435 //foreach (DataGridViewColumn col in dgvBeforeStep.SelectedColumns)
436
437 //{
438
439 // Console.WriteLine(col.Index);
440
441 //}
442
443 //指定行、列、单元格取得
444
445 //[C#]
446
447 //'(0, 0)的选中
448
449 //DataGridView1[0, 0].Selected = True
450
451 //'Index为1的行选中
452
453 //DataGridView1.Rows[1].Selected = True
454
455 //'Index为2的列选中
456
457 //DataGridView1.Columns[2].Selected = True
458
459 #endregion
460
461 #region 13.DataGridView指定单元格是否表示
462
463 //14.DataGridView表头部单元格取得
464
465 //15.DataGridView表头部单元格文字列设定
466
467 //16.DataGridView选择的部分拷贝至剪贴板
468
469 //17.DataGridView粘贴
470
471 //18.DataGridView单元格上ToolTip表示设定(鼠标移动到相应单元格上时,弹出说明信息)
472
473
474
475 #endregion
476
477 #region 13.DataGridView指定单元格是否表示
478
479 //[C#]
480
481
482
483 //if (DataGridView1[2, 0].Displayed && DataGridView1[2, 0].Visible)
484 //{
485 // DataGridView1.CurrentCell = DataGridView1[2, 0];
486 //}
487
488
489 #endregion
490
491 #region 14. DataGridView表头部单元格取得
492
493 //[C#]
494
495 //'DataGridView1第一列表头改变
496
497 //DataGridView1.Columns[0].HeaderCell.Value = "第一列"
498
499 //'DataGridView1第一行表头改变
500
501 //DataGridView1.Rows[0].HeaderCell.Value = "第一行"
502
503 //'DataGridView1左上角单元格值改变
504
505 //DataGridView1.TopLeftHeaderCell.Value = "左上"
506
507 #endregion
508
509 #region 15.DataGridView表头部单元格文字列设定
510
511 //更改列Header表示文字列
512
513 //[C#]
514
515 //'DataGridView1改变第一列头部单元格文字
516
517 //DataGridView1.Columns[0].HeaderText = "第一列"
518
519 //更改行Header表示文字列
520
521 //[C#]
522
523 //'DataGridView1行的头部单元格为序号
524
525
526
527 //for (int i = 0; i<DataGridView1.Rows.Count-1; i++)
528 //{
529 // DataGridView1.Rows[i].HeaderCell.Value=i.ToString();
530 //}
531
532
533 //'行的宽度自动调节
534
535 //DataGridView1.AutoResizeRowHeadersWidth( DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders)
536
537 //最左上Header单元格文字列
538
539 //[C#]
540
541 //'修改最左上单元格
542
543 //DataGridView1.TopLeftHeaderCell.Value = "/"
544
545 #endregion
546
547 #region 16.DataGridView选择的部分拷贝至剪贴板
548
549 //拷贝模式设定
550
551 //[C#]
552
553 //DataGridView1.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithoutHeaderText
554
555 //选中部分拷贝
556
557 //[C#]
558
559 //Clipboard.SetDataObject(DataGridView1.GetClipboardContent())
560
561 #endregion
562
563 #region 17.DataGridView粘贴
564
565 //[C#]
566
567
568
569 //代码
570
571
572 #endregion
573
574 #region 18.DataGridView单元格上ToolTip表示设定(鼠标移动到相应单元格上时,弹出说明信息)
575
576 //[C#]
577
578 //指定单元格
579
580 //DataGridView1[0, 0].ToolTipText = "指定单元格"
581
582 //指定列
583
584 //DataGridView1.Columns[0].ToolTipText = "指定列"
585
586 //指定行
587
588 //DataGridView1.Rows[0].HeaderCell.ToolTipText = "指定行"
589
590 //CellToolTipTextNeeded事件,在多个单元格使用相同的ToolTips的时候,可以用该事件,下例为显示当前单元格的行号和列号
591
592 //[C#]
593
594 //'CellToolTipTextNeeded事件
595
596
597
598 //private void DataGridView1_CellToolTipTextNeeded(object sender, DataGridViewCellToolTipTextNeededEventArgs e)
599 //{
600 // e.ToolTipText = e.RowIndex.ToString() + "," + e.ColumnIndex.ToString();
601 //}
602
603
604 #endregion
605
606 #region 19. DataGridView中的ContextMenuStrip属性
607
608 //20. DataGridView指定滚动框位置
609
610 //21. DataGridView手动追加列
611
612 //22. DataGridView全体分界线样式设置
613
614 //23. DataGridView根据单元格属性更改显示内容
615
616 //24. DataGridView新追加行的行高样式设置る
617
618 //25. DataGridView新追加行单元格默认值设置
619
620
621
622 #endregion
623
624 #region 19. DataGridView中的ContextMenuStrip属性
625
626 //[C#]
627
628 //DataGridView1.ContextMenuStrip = this.ContextMenuStrip1
629
630 //DataGridView1.Columns[0)].ContextMenuStrip = this.ContextMenuStrip2
631
632 //DataGridView1.Columns[0].HeaderCell.ContextMenuStrip = this.ContextMenuStrip2
633
634 //DataGridView1.Rows[0].ContextMenuStrip = this.ContextMenuStrip3
635
636 //DataGridView1[1, 0].ContextMenuStrip = this.ContextMenuStrip4
637
638 //也可以用CellContextMenuStripNeeded、RowContextMenuStripNeeded属性进行定义
639
640 //[C#]
641
642
643
644 //复制代码
645 // 1 private void DataGridView1_CellContextMenuStripNeeded(object sender, DataGridViewCellContextMenuStripNeededEventArgs e)
646 // 2 {
647 // 3 if (e.RowIndex<0)
648 // 4 {
649 // 5 e.ContextMenuStrip = this.contextMenuStrip1;
650 // 6 }
651 // 7 else if (e.ColumnIndex<0)
652 // 8 {
653 // 9 e.ContextMenuStrip = this.contextMenuStrip2;
654 //10 }
655 //11 }
656 //12
657 //复制代码
658
659
660 #endregion
661
662 #region 20. DataGridView指定滚动框位置
663
664 //[C#]
665
666 //DataGridView1.FirstDisplayedScrollingRowIndex = 0
667
668 //DataGridView1.FirstDisplayedScrollingColumnIndex = 0
669
670 #endregion
671
672 #region 21.DataGridView手动追加列
673
674 //[C#]
675
676 //DataGridView1.AutoGenerateColumns = False
677
678 //DataGridView1.DataSource = BindingSource1
679
680 //DataGridViewTextBoxColumn textColumn = new DataGridViewTextBoxColumn()
681
682 //textColumn.DataPropertyName = "Column1"
683
684 //textColumn.Name = "Column1"
685
686 //textColumn.HeaderText = "Column1"
687
688 //DataGridView1.Columns.Add(textColumn)
689
690 #endregion
691
692 #region 22.DataGridView全体分界线样式设置
693
694 //[C#]
695
696 //DataGridView1.BorderStyle = BorderStyle.Fixed3D
697
698 //单元格上下左右分界线样式设置
699
700 //[C#]
701
702 //DataGridView1.AdvancedCellBorderStyle.Top = DataGridViewAdvancedCellBorderStyle.InsetDouble
703
704 //DataGridView1.AdvancedCellBorderStyle.Right = DataGridViewAdvancedCellBorderStyle.Inset
705
706 //DataGridView1.AdvancedCellBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.Inset
707
708 //DataGridView1.AdvancedCellBorderStyle.Left = DataGridViewAdvancedCellBorderStyle.InsetDouble
709
710 #endregion
711
712 #region 23.DataGridView根据单元格属性更改显示内容
713
714 //如下例,当该列是字符串时,自动转换文字大小写
715
716 //[C#]
717
718
719
720 //复制代码
721 //if (DataGridView1.Columns[e.ColumnIndex].Name.Equals("Column1") && e.Value.GetType().Equals("String"))
722 //{
723 // string str = e.Value.ToString();
724 //e.Value = str.ToUpper();
725 // e.FormattingApplied = true;
726 //}
727 //复制代码
728
729
730 #endregion
731
732 #region 24. DataGridView新追加行的行高样式设置
733
734 ////行高设置
735
736 ////[C#]
737
738 //DataGridView1.RowTemplate.Height = 50
739
740 //DataGridView1.RowTemplate.MinimumHeight = 50
741
742 //样式设置
743
744 //[C#]
745
746 //'设置背景色为黄色
747
748 //DataGridView1.DefaultCellStyle.BackColor = Color.Yellow
749
750 #endregion
751
752 #region 25.DataGridView新追加行单元格默认值设置
753
754 //[C#]
755 #endregion
756
757 }
758 }
759 }