C#----DataGridView控件60招(四)

DataGridView控件用法合集()

19. DataGridView中的ContextMenuStrip属性

20. DataGridView指定滚动框位置

21. DataGridView手动追加列

22. DataGridView全体分界线样式设置

23. DataGridView根据单元格属性更改显示内容

24. DataGridView新追加行的行高样式设置る

25. DataGridView新追加行单元格默认值设置

 

19. DataGridView中的ContextMenuStrip属性

[C#]

DataGridView1.ContextMenuStrip = this.ContextMenuStrip1

DataGridView1.Columns[0)].ContextMenuStrip = this.ContextMenuStrip2

DataGridView1.Columns[0].HeaderCell.ContextMenuStrip = this.ContextMenuStrip2

DataGridView1.Rows[0].ContextMenuStrip = this.ContextMenuStrip3

DataGridView1[1, 0].ContextMenuStrip = this.ContextMenuStrip4

也可以用CellContextMenuStripNeededRowContextMenuStripNeeded属性进行定义

[C#]


1 private void DataGridView1_CellContextMenuStripNeeded( object sender, DataGridViewCellContextMenuStripNeededEventArgs e)
2 {
3 if (e.RowIndex < 0 )
4 {
5 e.ContextMenuStrip = this .contextMenuStrip1;
6 }
7 else if (e.ColumnIndex < 0 )
8 {
9 e.ContextMenuStrip = this .contextMenuStrip2;
10 }
11 }
12


 

20. DataGridView指定滚动框位置

[C#]

DataGridView1.FirstDisplayedScrollingRowIndex = 0

DataGridView1.FirstDisplayedScrollingColumnIndex = 0

21. DataGridView手动追加列

[C#]

DataGridView1.AutoGenerateColumns = False

DataGridView1.DataSource = BindingSource1

DataGridViewTextBoxColumn textColumn=new DataGridViewTextBoxColumn()

textColumn.DataPropertyName = "Column1"

textColumn.Name = "Column1"

textColumn.HeaderText = "Column1"

DataGridView1.Columns.Add(textColumn)

22. DataGridView全体分界线样式设置

[C#]

DataGridView1.BorderStyle = BorderStyle.Fixed3D

单元格上下左右分界线样式设置

[C#]

DataGridView1.AdvancedCellBorderStyle.Top = DataGridViewAdvancedCellBorderStyle.InsetDouble

DataGridView1.AdvancedCellBorderStyle.Right = DataGridViewAdvancedCellBorderStyle.Inset

DataGridView1.AdvancedCellBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.Inset

DataGridView1.AdvancedCellBorderStyle.Left = DataGridViewAdvancedCellBorderStyle.InsetDouble

23. DataGridView根据单元格属性更改显示内容

如下例,当该列是字符串时,自动转换文字大小写

[C#]


if (DataGridView1.Columns[e.ColumnIndex].Name.Equals( " Column1 " ) && e.Value.GetType().Equals( " String " ))
{
string str = e.Value.ToString();
e.Value
= str.ToUpper();
e.FormattingApplied
= true ;
}


 

24. DataGridView新追加行的行高样式设置

行高设置

[C#]

DataGridView1.RowTemplate.Height = 50

DataGridView1.RowTemplate.MinimumHeight = 50

样式设置

[C#]

'设置背景色为黄色

DataGridView1.DefaultCellStyle.BackColor = Color.Yellow

25. DataGridView新追加行单元格默认值设置

[C#]


private void DataGridView1_DefaultValuesNeeded( object sender, DataGridViewRowEventArgs e)
{
e.Row.Cells[
" Column1 " ].Value = 0 ;
e.Row.Cells[
" Column2 " ].Value = " - " ;
}

http://www.cnblogs.com/MicroZhao/archive/2010/04/21/1717615.html
 

你可能感兴趣的:(职场,C#,控件,datagridview,休闲)