版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。
这里要学习的修改单元格,包括两个方面的内容:一是修改单元格内容;二是修改单元格的样式。
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 教程 目录