DataGridView控件用法合集(八)

   DataGridView控件用法合集(八)

整理 By Tyouvivi   整理日 2007年06月13日  第八部分

近期将DataGridView常用的一些用法做了一个整理。为防止页面过长,现分批贴出来,此为第八部分。 
 

DataGridView Owner描画

41. DataGridView设置单元格背景颜色
42. DataGridView行样式描画
43. DataGridView显示行号
44. DataGridView焦点所在单元格焦点框不显示的设定

41. DataGridView设置单元格背景颜色

[VB.NET]

'CellPaintingイベントハンドラ

Private Sub DataGridView1_CellPainting(ByVal sender As Object, _

        ByVal e As DataGridViewCellPaintingEventArgs) _

        Handles DataGridView1.CellPainting

    'ヘッダー以外のセルで、背景を描画する時

    If e.ColumnIndex >= 0 AndAlso e.RowIndex >= 0 AndAlso _

        (e.PaintParts And DataGridViewPaintParts.Background) = _

            DataGridViewPaintParts.Background Then

 

        '選択されているか調べ、色を決定する

        'bColor1が開始色、bColor2が終了色

        Dim bColor1, bColor2 As Color

        If (e.PaintParts And DataGridViewPaintParts.SelectionBackground) = _

                DataGridViewPaintParts.SelectionBackground AndAlso _

            (e.State And DataGridViewElementStates.Selected) = _

                DataGridViewElementStates.Selected Then

            bColor1 = e.CellStyle.SelectionBackColor

            bColor2 = Color.Black

        Else

            bColor1 = e.CellStyle.BackColor

            bColor2 = Color.LemonChiffon

        End If

 

        'グラデーションブラシを作成

        Dim b As New System.Drawing.Drawing2D.LinearGradientBrush( _

            e.CellBounds, bColor1, bColor2, _

            System.Drawing.Drawing2D.LinearGradientMode.Horizontal)

        Try

            'セルを塗りつぶす

            e.Graphics.FillRectangle(b, e.CellBounds)

        Finally

            b.Dispose()

        End Try

 

        '背景以外が描画されるようにする

        Dim paintParts As DataGridViewPaintParts = _

            e.PaintParts And Not DataGridViewPaintParts.Background

        'セルを描画する

        e.Paint(e.ClipBounds, paintParts)

 

        '描画が完了したことを知らせる

        e.Handled = True

    End If

End Sub

[C#]

//CellPaintingイベントハンドラ

private void DataGridView1_CellPainting(object sender,

    DataGridViewCellPaintingEventArgs e)

{

    //ヘッダー以外のセルで、背景を描画する時

    if (e.ColumnIndex >= 0 && e.RowIndex >= 0 &&

        (e.PaintParts & DataGridViewPaintParts.Background) ==

            DataGridViewPaintParts.Background)

    {

        //選択されているか調べ、色を決定する

        //bColor1が開始色、bColor2が終了色

        Color bColor1, bColor2;

        if ((e.PaintParts & DataGridViewPaintParts.SelectionBackground) ==

                DataGridViewPaintParts.SelectionBackground &&

            (e.State & DataGridViewElementStates.Selected) ==

                DataGridViewElementStates.Selected)

        {

            bColor1 = e.CellStyle.SelectionBackColor;

            bColor2 = Color.Black;

        }

        else

        {

            bColor1 = e.CellStyle.BackColor;

            bColor2 = Color.LemonChiffon;

        }

        //グラデーションブラシを作成

        using (System.Drawing.Drawing2D.LinearGradientBrush b =

            new System.Drawing.Drawing2D.LinearGradientBrush(

            e.CellBounds, bColor1, bColor2,

            System.Drawing.Drawing2D.LinearGradientMode.Horizontal))

        {

            //セルを塗りつぶす

            e.Graphics.FillRectangle(b, e.CellBounds);

        }

 

        //背景以外が描画されるようにする

        DataGridViewPaintParts paintParts =

           e.PaintParts & ~DataGridViewPaintParts.Background;

        //セルを描画する

        e.Paint(e.ClipBounds, paintParts);

 

        //描画が完了したことを知らせる

        e.Handled = true;

    }

}


单元格背景显示图像

[VB.NET]

'セルの背景に表示する画像

Private cellBackImage As New Bitmap("C:/back.gif")

 

'CellPaintingイベントハンドラ

Private Sub DataGridView1_CellPainting(ByVal sender As Object, _

        ByVal e As DataGridViewCellPaintingEventArgs) _

        Handles DataGridView1.CellPainting

    'ヘッダー以外のセルで、背景を描画する時

    If e.ColumnIndex >= 0 AndAlso e.RowIndex >= 0 AndAlso _

        (e.PaintParts And DataGridViewPaintParts.Background) = _

            DataGridViewPaintParts.Background Then

        '背景だけを描画する

        Dim backParts As DataGridViewPaintParts = _

            e.PaintParts And (DataGridViewPaintParts.Background Or _

                DataGridViewPaintParts.SelectionBackground)

        e.Paint(e.ClipBounds, backParts)

 

        '画像をセルの真ん中に描画する

        Dim x As Integer = e.CellBounds.X + _

            (e.CellBounds.Width - cellBackImage.Width) / 2

        Dim y As Integer = e.CellBounds.Y + _

            (e.CellBounds.Height - cellBackImage.Height) / 2

        e.Graphics.DrawImage(cellBackImage, x, y)

 

        '背景以外が描画されるようにする

        Dim paintParts As DataGridViewPaintParts = _

            e.PaintParts And Not backParts

        'セルを描画する

        e.Paint(e.ClipBounds, paintParts)

 

        '描画が完了したことを知らせる

        e.Handled = True

    End If

End Sub

[C#]

//セルの背景に表示する画像

private Bitmap cellBackImage = new Bitmap("C://back.gif");

 

//CellPaintingイベントハンドラ

private void DataGridView1_CellPainting(object sender,

    DataGridViewCellPaintingEventArgs e)

{

    //ヘッダー以外のセルで、背景を描画する時

    if (e.ColumnIndex >= 0 && e.RowIndex >= 0 &&

        (e.PaintParts & DataGridViewPaintParts.Background) ==

            DataGridViewPaintParts.Background)

    {

        //背景だけを描画する

        DataGridViewPaintParts backParts = e.PaintParts &

            (DataGridViewPaintParts.Background |

            DataGridViewPaintParts.SelectionBackground);

        e.Paint(e.ClipBounds, backParts);

 

        //画像をセルの真ん中に描画する

        int x = e.CellBounds.X +

            (e.CellBounds.Width - cellBackImage.Width) / 2;

        int y = e.CellBounds.Y +

            (e.CellBounds.Height - cellBackImage.Height) / 2;

        e.Graphics.DrawImage(cellBackImage, x, y);

 

        //背景以外が描画されるようにする

        DataGridViewPaintParts paintParts =

            e.PaintParts & ~backParts;

        //セルを描画する

        e.Paint(e.ClipBounds, paintParts);

 

        //描画が完了したことを知らせる

        e.Handled = true;

    }

}

42. DataGridView行样式描画

利用RowPostPaint事件描画

[VB.NET]

'RowPostPaintイベントハンドラ

Private Sub DataGridView1_RowPostPaint(ByVal sender As Object, _

        ByVal e As DataGridViewRowPostPaintEventArgs) _

        Handles DataGridView1.RowPostPaint

    Dim dgv As DataGridView = CType(sender, DataGridView)

 

    '線の色を決定する

    Dim linePen As Pen

    Select Case e.RowIndex Mod 3

        Case 0

            linePen = Pens.Blue

        Case 1

            linePen = Pens.Green

        Case Else

            linePen = Pens.Red

    End Select

 

    '線を引く位置を計算する

    Dim startX As Integer = IIf(dgv.RowHeadersVisible, dgv.RowHeadersWidth, 0)

    Dim startY As Integer = e.RowBounds.Top + e.RowBounds.Height - 1

    Dim endX As Integer = startX + _

        dgv.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) - _

        dgv.HorizontalScrollingOffset

 

    '線を引く

    e.Graphics.DrawLine(linePen, startX, startY, endX, startY)

End Sub

[C#]

//RowPostPaintイベントハンドラ

private void DataGridView1_RowPostPaint(object sender,

    DataGridViewRowPostPaintEventArgs e)

{

    DataGridView dgv = (DataGridView)sender;

 

    //線の色を決定する

    Pen linePen;

    switch (e.RowIndex % 3)

    {

        case 0:

            linePen = Pens.Blue;

            break;

        case 1:

            linePen = Pens.Green;

            break;

        default:

            linePen = Pens.Red;

            break;

    }

 

    //線を引く位置を計算する

    int startX = dgv.RowHeadersVisible ? dgv.RowHeadersWidth : 0;

    int startY = e.RowBounds.Top + e.RowBounds.Height - 1;

    int endX = startX + dgv.Columns.GetColumnsWidth(

        DataGridViewElementStates.Visible) -

        dgv.HorizontalScrollingOffset;

    //線を引く

    e.Graphics.DrawLine(linePen,

        startX, startY, endX, startY);

}


利用
RowPrePaint事件描画

[VB.NET]

'RowPrePaintイベントハンドラ

Private Sub DataGridView1_RowPrePaint(ByVal sender As Object, _

        ByVal e As DataGridViewRowPrePaintEventArgs) _

        Handles DataGridView1.RowPrePaint

    '背景を描画するか

    If (e.PaintParts And DataGridViewPaintParts.Background) = _

            DataGridViewPaintParts.Background Then

        '選択されているか調べ、色を決定する

        'bColor1が開始色、bColor2が終了色

        Dim bColor1, bColor2 As Color

        If (e.PaintParts And DataGridViewPaintParts.SelectionBackground) = _

                DataGridViewPaintParts.SelectionBackground AndAlso _

            (e.State And DataGridViewElementStates.Selected) = _

                DataGridViewElementStates.Selected Then

            bColor1 = e.InheritedRowStyle.SelectionBackColor

            bColor2 = Color.Black

        Else

            bColor1 = e.InheritedRowStyle.BackColor

            bColor2 = Color.YellowGreen

        End If

 

        'グラデーションの範囲を計算する

        'ヘッダーを除くセルの部分だけ描画する

        Dim dgv As DataGridView = CType(sender, DataGridView)

        Dim rectLeft2 As Integer = _

            IIf(dgv.RowHeadersVisible, dgv.RowHeadersWidth, 0)

        Dim rectLeft As Integer = _

            rectLeft2 - dgv.HorizontalScrollingOffset

        Dim rectWidth As Integer = _

            dgv.Columns.GetColumnsWidth(DataGridViewElementStates.Visible)

        Dim rect As New Rectangle(rectLeft, e.RowBounds.Top, _

            rectWidth, e.RowBounds.Height - 1)

 

        'グラデーションブラシを作成

        Using b As New System.Drawing.Drawing2D.LinearGradientBrush( _

            rect, bColor1, bColor2, _

            System.Drawing.Drawing2D.LinearGradientMode.Horizontal)

 

            '描画する範囲を計算する

            rect.X = rectLeft2

            rect.Width -= dgv.HorizontalScrollingOffset

            'セルを塗りつぶす

            e.Graphics.FillRectangle(b, rect)

        End Using

 

        'ヘッダーを描画する

        e.PaintHeader(True)

 

        '背景を描画しないようにする

        e.PaintParts = _

            e.PaintParts And Not DataGridViewPaintParts.Background

    End If

End Sub

 

'ColumnWidthChangedイベントハンドラ

Private Sub DataGridView1_ColumnWidthChanged(ByVal sender As Object, _

        ByVal e As DataGridViewColumnEventArgs) _

        Handles DataGridView1.ColumnWidthChanged

    Dim dgv As DataGridView = CType(sender, DataGridView)

    dgv.Invalidate()

End Sub

[C#]

//RowPrePaintイベントハンドラ

private void DataGridView1_RowPrePaint(object sender,

    DataGridViewRowPrePaintEventArgs e)

{

    //背景を描画するか

    if ((e.PaintParts & DataGridViewPaintParts.Background) ==

        DataGridViewPaintParts.Background)

    {

        //選択されているか調べ、色を決定する

        //bColor1が開始色、bColor2が終了色

        Color bColor1, bColor2;

        if ((e.PaintParts & DataGridViewPaintParts.SelectionBackground) ==

                DataGridViewPaintParts.SelectionBackground &&

            (e.State & DataGridViewElementStates.Selected) ==

                DataGridViewElementStates.Selected)

        {

            bColor1 = e.InheritedRowStyle.SelectionBackColor;

            bColor2 = Color.Black;

        }

        else

        {

            bColor1 = e.InheritedRowStyle.BackColor;

            bColor2 = Color.YellowGreen;

        }

 

        //グラデーションの範囲を計算する

        //ヘッダーを除くセルの部分だけ描画する

        DataGridView dgv = (DataGridView)sender;

        int rectLeft2 = dgv.RowHeadersVisible ? dgv.RowHeadersWidth : 0;

        int rectLeft = rectLeft2 - dgv.HorizontalScrollingOffset;

        int rectWidth = dgv.Columns.GetColumnsWidth(

            DataGridViewElementStates.Visible);

        Rectangle rect = new Rectangle(rectLeft, e.RowBounds.Top,

            rectWidth, e.RowBounds.Height - 1);

 

        //グラデーションブラシを作成

        using (System.Drawing.Drawing2D.LinearGradientBrush b =

            new System.Drawing.Drawing2D.LinearGradientBrush(

            rect, bColor1, bColor2,

            System.Drawing.Drawing2D.LinearGradientMode.Horizontal))

        {

            //描画する範囲を計算する

            rect.X = rectLeft2;

            rect.Width -= dgv.HorizontalScrollingOffset;

            //セルを塗りつぶす

            e.Graphics.FillRectangle(b, rect);

        }

 

        //ヘッダーを描画する

        e.PaintHeader(true);

 

        //背景を描画しないようにする

        e.PaintParts &= ~DataGridViewPaintParts.Background;

    }

}

 

//ColumnWidthChangedイベントハンドラ

private void DataGridView1_ColumnWidthChanged(object sender,

    DataGridViewColumnEventArgs e)

{

    DataGridView dgv = (DataGridView)sender;

    dgv.Invalidate();

}


43. DataGridView显示行号

[VB.NET]

'CellPaintingイベントハンドラ

Private Sub DataGridView1_CellPainting(ByVal sender As Object, _

        ByVal e As DataGridViewCellPaintingEventArgs) _

        Handles DataGridView1.CellPainting

    '列ヘッダーかどうか調べる

    If e.ColumnIndex < 0 And e.RowIndex >= 0 Then

        'セルを描画する

        e.Paint(e.ClipBounds, DataGridViewPaintParts.All)

 

        '行番号を描画する範囲を決定する

        'e.AdvancedBorderStylee.CellStyle.Paddingは無視しています

        Dim indexRect As Rectangle = e.CellBounds

        indexRect.Inflate(-2, -2)

        '行番号を描画する

        TextRenderer.DrawText(e.Graphics, _

            (e.RowIndex + 1).ToString(), _

            e.CellStyle.Font, _

            indexRect, _

            e.CellStyle.ForeColor, _

            TextFormatFlags.Right Or TextFormatFlags.VerticalCenter)

        '描画が完了したことを知らせる

        e.Handled = True

    End If

End Sub

[C#]

//CellPaintingイベントハンドラ

private void DataGridView1_CellPainting(object sender,

    DataGridViewCellPaintingEventArgs e)

{

    //列ヘッダーかどうか調べる

    if (e.ColumnIndex < 0 && e.RowIndex >= 0)

    {

        //セルを描画する

        e.Paint(e.ClipBounds, DataGridViewPaintParts.All);

 

        //行番号を描画する範囲を決定する

        //e.AdvancedBorderStylee.CellStyle.Paddingは無視しています

        Rectangle indexRect = e.CellBounds;

        indexRect.Inflate(-2, -2);

        //行番号を描画する

        TextRenderer.DrawText(e.Graphics,

            (e.RowIndex + 1).ToString(),

            e.CellStyle.Font,

            indexRect,

            e.CellStyle.ForeColor,

            TextFormatFlags.Right | TextFormatFlags.VerticalCenter);

        //描画が完了したことを知らせる

        e.Handled = true;

    }

}

利用RowPostPaint事件描画

[VB.NET]

'RowPostPaintイベントハンドラ

Private Sub DataGridView1_RowPostPaint(ByVal sender As Object, _

        ByVal e As DataGridViewRowPostPaintEventArgs) _

        Handles DataGridView1.RowPostPaint

    Dim dgv As DataGridView = CType(sender, DataGridView)

    If dgv.RowHeadersVisible Then

        '行番号を描画する範囲を決定する

        Dim rect As New Rectangle(e.RowBounds.Left, e.RowBounds.Top, _

            dgv.RowHeadersWidth, e.RowBounds.Height)

        rect.Inflate(-2, -2)

        '行番号を描画する

        TextRenderer.DrawText(e.Graphics, _

            (e.RowIndex + 1).ToString(), _

            e.InheritedRowStyle.Font, _

            rect, _

            e.InheritedRowStyle.ForeColor, _

            TextFormatFlags.Right Or TextFormatFlags.VerticalCenter)

    End If

End Sub

[C#]

//RowPostPaintイベントハンドラ

private void DataGridView1_RowPostPaint(object sender,

    DataGridViewRowPostPaintEventArgs e)

{

    DataGridView dgv = (DataGridView)sender;

    if (dgv.RowHeadersVisible)

    {

        //行番号を描画する範囲を決定する

        Rectangle rect = new Rectangle(

            e.RowBounds.Left, e.RowBounds.Top,

            dgv.RowHeadersWidth, e.RowBounds.Height);

        rect.Inflate(-2, -2);

        //行番号を描画する

        TextRenderer.DrawText(e.Graphics,

            (e.RowIndex + 1).ToString(),

            e.InheritedRowStyle.Font,

            rect,

            e.InheritedRowStyle.ForeColor,

            TextFormatFlags.Right | TextFormatFlags.VerticalCenter);

    }

}

44. DataGridView焦点所在单元格焦点框不显示的设定

[VB.NET]

'CellPaintingイベントハンドラ

Private Sub DataGridView1_CellPainting(ByVal sender As Object, _

        ByVal e As DataGridViewCellPaintingEventArgs) _

        Handles DataGridView1.CellPainting

    'ヘッダー以外のとき

    If e.ColumnIndex >= 0 And e.RowIndex >= 0 Then

        'フォーカス枠以外が描画されるようにする

        Dim paintParts As DataGridViewPaintParts = _

            e.PaintParts And Not DataGridViewPaintParts.Focus

        'セルを描画する

        e.Paint(e.ClipBounds, paintParts)

 

        '描画が完了したことを知らせる

        e.Handled = True

    End If

End Sub

[C#]

//CellPaintingイベントハンドラ

private void DataGridView1_CellPainting(object sender,

    DataGridViewCellPaintingEventArgs e)

{

    //ヘッダー以外のとき

    if (e.ColumnIndex >= 0 && e.RowIndex >= 0)

    {

        //フォーカス枠以外が描画されるようにする

        DataGridViewPaintParts paintParts =

            e.PaintParts & ~DataGridViewPaintParts.Focus;

        //セルを描画する

        e.Paint(e.ClipBounds, paintParts);

 

        //描画が完了したことを知らせる

        e.Handled = true;

    }

}

利用RowPrePaint事件实现

[VB.NET]

'RowPrePaintイベントハンドラ

Private Sub DataGridView1_RowPrePaint(ByVal sender As Object, _

        ByVal e As DataGridViewRowPrePaintEventArgs) _

        Handles DataGridView1.RowPrePaint

    'フォーカス枠を描画しない

    e.PaintParts = e.PaintParts And Not DataGridViewPaintParts.Focus

End Sub

[C#]

//RowPrePaintイベントハンドラ

private void DataGridView1_RowPrePaint(object sender,

    DataGridViewRowPrePaintEventArgs e)

{

    //フォーカス枠を描画しない

    e.PaintParts &= ~DataGridViewPaintParts.Focus;

}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
vb.net操作DataGridView控件用法的集合,包括: 1. DataGridView当前的单元格属性取得、变更 2. DataGridView编辑属性 3. DataGridView最下面一列新追加行非表示 4. DataGridView判断当前选中行是否为新追加的行 5. DataGridView删除行可否设定 6. DataGridView行列不表示和删除 DataGridView控件用法合集(二) 7. DataGridView行列宽度高度设置为不能编辑 8. DataGridView行高列幅自动调整 9. DataGridView指定行列冻结 10. DataGridView列顺序变更可否设定 11. DataGridView行复数选择 12. DataGridView选择的行、列、单元格取得 DataGridView控件用法合集(三) 13. DataGridView指定单元格是否表示 14. DataGridView表头部单元格取得 15. DataGridView表头部单元格文字列设定 16. DataGridView选择的部分拷贝至剪贴板 17.DataGridView粘贴 18. DataGridView单元格上ToolTip表示设定(鼠标移动到相应单元格上时,弹出说明信息) DataGridView控件用法合集(四) 19. DataGridView中的ContextMenuStrip属性 20. DataGridView指定滚动框位置 21. DataGridView手动追加列 22. DataGridView全体分界线样式设置 23. DataGridView根据单元格属性更改显示内容 24. DataGridView新追加行的行高样式设置る 25. DataGridView新追加行单元格默认值设置 DataGridView中输入错误数据的处理(五) 26. DataGridView单元格数据错误标签表示 27. DataGridView单元格内输入值正确性判断 28. DataGridView单元格输入错误值事件的捕获 DataGridView控件用法合集(六) 29. DataGridView行排序(点击列表头自动排序的设置) 30. DataGridView自动行排序(新追加值也会自动排序) 31. DataGridView自动行排序禁止情况下的排序 32. DataGridView指定列指定排序 DataGridView控件用法合集(七) 33. DataGridView单元格样式设置 34. DataGridView文字表示位置的设定 35. DataGridView单元格内文字列换行 36. DataGridView单元格DBNull值表示的设定 37. DataGridView单元格样式格式化 38. DataGridView指定单元格颜色设定 39. DataGridView单元格文字字体设置 40. DataGridView根据单元格值设定单元格样式 DataGridView控件用法合集() 41. DataGridView设置单元格背景颜色 42. DataGridView行样式描画 43. DataGridView显示行号 44. DataGridView焦点所在单元格焦点框不显示的设定 DataGridView控件用法合集(九) 45. DataGridView中显示选择框CheckBox 46. DataGridView中显示下拉框ComboBox 47. DataGridView单击打开下拉框 48. DataGridView中显示按钮 49. DataGridView中显示链接 50. DataGridView中显示图像 DataGridView控件用法合集(十) 51. DataGridView编辑中单元格控件取得 52. DataGridView输入自动完成 53. DataGridView单元格编辑时键盘KEY事件取得 54. DataGridView下拉框(ComboBox)单元格编辑时事件取得 55. DataGridView下拉框(ComboBox)单元格允许文字输入设定 DataGridView控件用法合集(十一) 56. DataGridView根据值不同在另一列中显示相应图片 57. DataGridView中显示进度条(ProgressBar) 58. DataGridView中添加MaskedTextBox DataGridView控件用法合集(十二) 59. DataGridView中Enter键按下焦点移至旁边的单元格 60. DataGridView行集合化(Group)

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值