vb.net 教程 3-10 窗体编程 datagridview控件 7 修改单元格

版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。

这里要学习的修改单元格,包括两个方面的内容:一是修改单元格内容;二是修改单元格的样式。

1、获得某个单元格:
方法一:获得当前活动状态的单元格:
DataGridView.CurrentCell  获取或设置当前处于活动状态的单元格,当多个单元格被选中时,值是最后一个被选中的单元格。
方法二:获得选中所有单元格:
DataGridView.SelectedCells
可以用 DataGridView.SelectedCells(0) 获得选中单元格中最左上角的那个单元格
可以用 DataGridView.SelectedCells(dgv.SelectedCells.Count - 1) 获得选中单元格中最右下角的那个单元格
方法三:获得指定行号列号的单元格
DataGridView(列号-1,行号-1)

2、通过以上方法获得单元格后,再通过单元格的Value属性获得其内容:
DataGridViewCell.Value

3、通过以上方法获得单元格,获得该单元格在DataGridView.中的位置:
列索引:DataGridViewCell.ColumnIndex
行索引:DataGridViewCell.RowIndex

4、判断单元格是否可以手动修改:
DataGridViewCell.ReadOnly

5、调整单元格的属性
调整前景色:DataGridViewCell.Style.ForeColor
调整背景色:DataGridViewCell.Style.BackColor
调整文本对齐方式:DataGridViewCell.Style.Alignment
调整文本字体:DataGridViewCell.Style.Font.FontFamily.Name
调整文本大小:DataGridViewCell.Style.Font.SizeInPoints
调整文本样式:DataGridViewCell.Style.Font.Style

6、当你直接用第5条的内容去获得前景色、背景色的话,你会发现获得的颜色是Empty,而获得字体之类,会产生错误:未将对象引用设置到对象的实例。所以,在使用以上属性前,必须要判断单元格是否具有 Style 属性:
DataGridViewCell.HasStyle

具体获得单元格信息的代码如下:

   '获得选中的单元格信息
    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Dim selCell As DataGridViewCell
        selCell = dgv.CurrentCell
        lblCellInfoColIndex.Text = selCell.ColumnIndex
        lblCellInfoRowIndex.Text = selCell.RowIndex


        txtCellInfoValue.Text = selCell.Value
        cbCellInfoReadonly.Checked = selCell.ReadOnly

        If selCell.HasStyle Then
            Select Case selCell.Style.ForeColor
                Case Color.Empty
                    cbCellInfoForeColor.Text = "透明"
                Case Color.Red
                    cbCellInfoForeColor.Text = "红色"
                Case Color.Green
                    cbCellInfoForeColor.Text = "绿色"
                Case Color.Blue
                    cbCellInfoForeColor.Text = "蓝色"
                Case Color.Yellow
                    cbCellInfoForeColor.Text = "黄色"
                Case Color.White
                    cbCellInfoForeColor.Text = "白色"
                Case Color.Black
                    cbCellInfoForeColor.Text = "黑色"
                Case Else
                    cbCellInfoForeColor.Text = "其他"
            End Select

            Select Case selCell.Style.BackColor
                Case Color.Empty
                    cbCellInfoBackColor.Text = "透明"
                Case Color.Red
                    cbCellInfoBackColor.Text = "红色"
                Case Color.Green
                    cbCellInfoBackColor.Text = "绿色"
                Case Color.Blue
                    cbCellInfoBackColor.Text = "蓝色"
                Case Color.Yellow
                    cbCellInfoBackColor.Text = "黄色"
                Case Color.White
                    cbCellInfoBackColor.Text = "白色"
                Case Color.Black
                    cbCellInfoBackColor.Text = "黑色"
                Case Else
                    cbCellInfoBackColor.Text = "其他"
            End Select

            Select Case selCell.Style.Alignment
                Case DataGridViewContentAlignment.TopLeft
                    cbCellInfoAlign.Text = "上左"
                Case DataGridViewContentAlignment.TopCenter
                    cbCellInfoAlign.Text = "上中"
                Case DataGridViewContentAlignment.TopRight
                    cbCellInfoAlign.Text = "上右"
                Case DataGridViewContentAlignment.MiddleLeft
                    cbCellInfoAlign.Text = "中左"
                Case DataGridViewContentAlignment.MiddleCenter
                    cbCellInfoAlign.Text = "居中"
                Case DataGridViewContentAlignment.MiddleRight
                    cbCellInfoAlign.Text = "中右"
                Case DataGridViewContentAlignment.BottomLeft
                    cbCellInfoAlign.Text = "下左"
                Case DataGridViewContentAlignment.BottomCenter
                    cbCellInfoAlign.Text = "下中"
                Case DataGridViewContentAlignment.BottomRight
                    cbCellInfoAlign.Text = "下右"
                Case Else
                    cbCellInfoAlign.Text = "未设定"
            End Select

            Select Case selCell.Style.Font.FontFamily.Name
                Case "仿宋"
                    cbCellInfoFontName.Text = "仿宋"
                Case "楷体"
                    cbCellInfoFontName.Text = "楷体"
                Case "隶书"
                    cbCellInfoFontName.Text = "隶书"
                Case "黑体"
                    cbCellInfoFontName.Text = "黑体"
                Case Else
                    cbCellInfoFontName.Text = "仿宋"
            End Select

            cbCellInfoFontSize.Text = selCell.Style.Font.SizeInPoints
            Select Case selCell.Style.Font.Style
                Case FontStyle.Regular
                    cbCellInfoFontStyle.Text = "正常"
                Case FontStyle.Bold
                    cbCellInfoFontStyle.Text = "加粗"
                Case FontStyle.Italic
                    cbCellInfoFontStyle.Text = "倾斜"
                Case FontStyle.Underline
                    cbCellInfoFontStyle.Text = "下划线"
                Case FontStyle.Strikeout
                    cbCellInfoFontStyle.Text = "删除线"
            End Select
        Else
            cbCellInfoForeColor.Text = "透明"
            cbCellInfoBackColor.Text = "透明"
            cbCellInfoAlign.Text = "未设定"
            cbCellInfoFontName.Text = ""
            cbCellInfoFontStyle.Text = ""
            cbCellInfoFontSize.Text = ""
        End If

    End Sub

运行如下图:

那么相应的修改单元格信息的代码如下:

    '修改选中的单元格信息
    Private Sub btnCellInfoEdit_Click(sender As Object, e As EventArgs) Handles btnCellInfoEdit.Click
        Dim ColumnIndex, RowIndex As Integer

        ColumnIndex = lblCellInfoColIndex.Text
        RowIndex = lblCellInfoRowIndex.Text

        Dim selCell As DataGridViewCell
        selCell = dgv(ColumnIndex, RowIndex)

        selCell.Value = txtCellInfoValue.Text
        selCell.ReadOnly = cbCellInfoReadonly.Checked

        Select Case cbCellInfoForeColor.Text
            Case "透明"
                selCell.Style.ForeColor = Color.Empty
            Case "红色"
                selCell.Style.ForeColor = Color.Red
            Case "绿色"
                selCell.Style.ForeColor = Color.Green
            Case "蓝色"
                selCell.Style.ForeColor = Color.Blue
            Case "黄色"
                selCell.Style.ForeColor = Color.Yellow
            Case "白色"
                selCell.Style.ForeColor = Color.White
            Case "黑色"
                selCell.Style.ForeColor = Color.Black
            Case Else
                selCell.Style.ForeColor = Color.Empty
        End Select

        Select Case cbCellInfoBackColor.Text
            Case "透明"
                selCell.Style.BackColor = Color.Empty
            Case "红色"
                selCell.Style.BackColor = Color.Red
            Case "绿色"
                selCell.Style.BackColor = Color.Green
            Case "蓝色"
                selCell.Style.BackColor = Color.Blue
            Case "黄色"
                selCell.Style.BackColor = Color.Yellow
            Case "白色"
                selCell.Style.BackColor = Color.White
            Case "黑色"
                selCell.Style.BackColor = Color.Black
            Case Else
                selCell.Style.BackColor = Color.Empty
        End Select

        Select Case cbCellInfoAlign.Text
            Case "上左"
                selCell.Style.Alignment = DataGridViewContentAlignment.TopLeft
            Case "上中"
                selCell.Style.Alignment = DataGridViewContentAlignment.TopCenter
            Case "上右"
                selCell.Style.Alignment = DataGridViewContentAlignment.TopRight
            Case "中左"
                selCell.Style.Alignment = DataGridViewContentAlignment.MiddleLeft
            Case "居中"
                selCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
            Case "中右"
                selCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
            Case "下左"
                selCell.Style.Alignment = DataGridViewContentAlignment.BottomLeft
            Case "下中"
                selCell.Style.Alignment = DataGridViewContentAlignment.BottomCenter
            Case "下右"
                selCell.Style.Alignment = DataGridViewContentAlignment.BottomRight
        End Select

        Dim fName As String = cbCellInfoFontName.Text
        Dim fFamilly As New FontFamily(fName)

        Dim fStyle As FontStyle
        Select Case cbCellInfoFontStyle.Text
            Case "正常"
                fStyle = FontStyle.Regular
            Case "加粗"
                fStyle = FontStyle.Bold
            Case "倾斜"
                fStyle = FontStyle.Italic
            Case "下划线"
                fStyle = FontStyle.Underline
            Case "删除线"
                fStyle = FontStyle.Strikeout
        End Select

        Dim newFont As New Font(fFamilly, CType(cbCellInfoFontSize.Text, Single), fStyle)
        selCell.Style.Font = newFont

    End Sub

运行如下图:

由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供参考。

学习更多vb.net知识,请参看vb.net 教程 目录

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值