DataGridView用法合集(4)

19.DataGridView中的ContextMenuStrip属性

[VB.NET]

DataGridView1.ContextMenuStrip = Me.ContextMenuStrip1

DataGridView1.Columns(0).ContextMenuStrip = Me.ContextMenuStrip2

DataGridView1.Columns(0).HeaderCell.ContextMenuStrip = Me.ContextMenuStrip2

DataGridView1.Rows(0).ContextMenuStrip = Me.ContextMenuStrip3

DataGridView1(1, 0).ContextMenuStrip = Me.ContextMenuStrip4

[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[0, 1].ContextMenuStrip = this.ContextMenuStrip4;

也可以用CellContextMenuStripNeeded、RowContextMenuStripNeeded属性进行定义

[VB.NET]

Private Sub DataGridView1_CellContextMenuStripNeeded( _
        ByVal sender As Object, _
        ByVal e As DataGridViewCellContextMenuStripNeededEventArgs) _
        Handles DataGridView1.CellContextMenuStripNeeded
    Dim dgv As DataGridView = CType(sender, DataGridView)
    If e.RowIndex < 0 Then
        e.ContextMenuStrip = Me.ContextMenuStrip1
    ElseIf e.ColumnIndex < 0 Then
        e.ContextMenuStrip = Me.ContextMenuStrip2
    ElseIf TypeOf (dgv(e.ColumnIndex, e.RowIndex).Value) Is Integer Then
        e.ContextMenuStrip = Me.ContextMenuStrip3
    End If
End Sub

[C#]

private void DataGridView1_CellContextMenuStripNeeded(object sender,
    DataGridViewCellContextMenuStripNeededEventArgs e)
{
    DataGridView dgv = (DataGridView)sender;
    if (e.RowIndex < 0)
    {
        e.ContextMenuStrip = this.ContextMenuStrip1;
    }
    else if (e.ColumnIndex < 0)
    {
        e.ContextMenuStrip = this.ContextMenuStrip2;
    }
    else if (dgv[e.ColumnIndex, e.RowIndex].Value is int)
    {
        e.ContextMenuStrip = this.ContextMenuStrip3;
    }
}

20. DataGridView指定滚动框位置

[VB.NET]

DataGridView1.FirstDisplayedScrollingRowIndex = 0

DataGridView1.FirstDisplayedScrollingColumnIndex = 0

[C#]

DataGridView1.FirstDisplayedScrollingRowIndex = 0;

DataGridView1.FirstDisplayedScrollingColumnIndex = 0;

21. DataGridView手动追加列

[VB.NET]

DataGridView1.AutoGenerateColumns = False

DataGridView1.DataSource = BindingSource1

Dim textColumn As New DataGridViewTextBoxColumn()

textColumn.DataPropertyName = "Column1"

textColumn.Name = "Column1"

textColumn.HeaderText = "Column1"

DataGridView1.Columns.Add(textColumn)

[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全体分界线样式设置

[VB.NET]

DataGridView1.BorderStyle = BorderStyle.Fixed3D

[C#]

DataGridView1.BorderStyle = BorderStyle.Fixed3D;

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

[VB.NET]

DataGridView1.AdvancedCellBorderStyle.Top = DataGridViewAdvancedCellBorderStyle.InsetDouble

DataGridView1.AdvancedCellBorderStyle.Right = DataGridViewAdvancedCellBorderStyle.Inset

DataGridView1.AdvancedCellBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.Inset

DataGridView1.AdvancedCellBorderStyle.Left = DataGridViewAdvancedCellBorderStyle.InsetDouble

[C#]

DataGridView1.AdvancedCellBorderStyle.Top = DataGridViewAdvancedCellBorderStyle.InsetDouble;

DataGridView1.AdvancedCellBorderStyle.Right = DataGridViewAdvancedCellBorderStyle.Inset;

DataGridView1.AdvancedCellBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.Inset;

DataGridView1.AdvancedCellBorderStyle.Left = DataGridViewAdvancedCellBorderStyle.InsetDouble;

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

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

[VB.NET]

Private Sub DataGridView1_CellFormatting(ByVal sender As Object, _
        ByVal e As DataGridViewCellFormattingEventArgs) _
        Handles DataGridView1.CellFormatting
    Dim dgv As DataGridView = CType(sender, DataGridView)
    If dgv.Columns(e.ColumnIndex).Name = "Column1" AndAlso _
            TypeOf e.Value Is String Then
        Dim str As String = e.Value.ToString()
        e.Value = str.ToUpper()
        e.FormattingApplied = True
    End If
End Sub

[C#]

private void DataGridView1_CellFormatting(object sender,
    DataGridViewCellFormattingEventArgs e)
{
    DataGridView dgv = (DataGridView)sender;
    if (dgv.Columns[e.ColumnIndex].Name == "Column1" && e.Value is string)
    {
        string str = e.Value.ToString();
        e.Value = str.ToUpper();
        e.FormattingApplied = true;
    }
}

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

行高设置

[VB.NET]

DataGridView1.RowTemplate.Height = 50

DataGridView1.RowTemplate.MinimumHeight = 50

[C#]

DataGridView1.RowTemplate.Height = 50;

DataGridView1.RowTemplate.MinimumHeight = 50;

样式设置

[VB.NET]

DataGridView1.DefaultCellStyle.BackColor = Color.Yellow

[C#]

DataGridView1.DefaultCellStyle.BackColor = Color.Yellow;

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

[VB.NET]

Private Sub DataGridView1_DefaultValuesNeeded(ByVal sender As Object, _

        ByVal e As DataGridViewRowEventArgs) _

        Handles DataGridView1.DefaultValuesNeeded

    e.Row.Cells("Column1").Value = 0

    e.Row.Cells("Column2").Value = "-"

End Sub

[C#]

private void DataGridView1_DefaultValuesNeeded(object sender,

    DataGridViewRowEventArgs e)

{

    e.Row.Cells["Column1"].Value = 0;

    e.Row.Cells["Column2"].Value = "-";

}

欢迎微信技术交流:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xwLink1996

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值