vb教程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(nder As Object, e As EventArgs) Handles Button3.Click
Dim lCell As DataGridViewCell
lCell = dgv.CurrentCell
lblCellInfoColIndex.Text = lCell.ColumnIndex
lblCellInfoRowIndex.Text = lCell.RowIndex
txtCellInfoValue.Text = lCell.Value
cbCellInfoReadonly.Checked = lCell.ReadOnly
If lCell.HasStyle Then
应对英文>山市翻译Select Ca lCell.Style.ForeColor
Ca Color.Empty
cbCellInfoForeColor.Text = "透明"
Ca Color.Red
cbCellInfoForeColor.Text = "红⾊"
Ca Color.Green
cbCellInfoForeColor.Text = "绿⾊"
Ca Color.Blue
cbCellInfoForeColor.Text = "蓝⾊"
Ca Color.Yellow
cbCellInfoForeColor.Text = "黄⾊"
Ca Color.White
cbCellInfoForeColor.Text = "⽩⾊"
Ca Color.Black
matecbCellInfoForeColor.Text = "⿊⾊"
Ca El
cbCellInfoForeColor.Text = "其他"
End Select
Select Ca lCell.Style.BackColor
Ca Color.Empty
cbCellInfoBackColor.Text = "透明"
Ca Color.Red
cbCellInfoBackColor.Text = "红⾊"
Ca Color.Green
cbCellInfoBackColor.Text = "绿⾊"
Ca Color.Blue
cbCellInfoBackColor.Text = "蓝⾊"
Ca Color.Yellow
cbCellInfoBackColor.Text = "黄⾊"
Ca Color.White
cbCellInfoBackColor.Text = "⽩⾊"
Ca Color.Black
cbCellInfoBackColor.Text = "⿊⾊"
Ca El
cbCellInfoBackColor.Text = "其他"
End Select
Select Ca lCell.Style.Alignment
Ca DataGridViewContentAlignment.TopLeft
cbCellInfoAlign.Text = "上左"
negativesCa DataGridViewContentAlignment.TopCenter
cbCellInfoAlign.Text = "上中"sycophantic
Ca DataGridViewContentAlignment.TopRight
cbCellInfoAlign.Text = "上右"
Ca DataGridViewContentAlignment.MiddleLeft
cbCellInfoAlign.Text = "中左"
Ca DataGridViewContentAlignment.MiddleCenter cbCellInfoAlign.Text = "居中"
Ca DataGridViewContentAlignment.MiddleRight cbCellInfoAlign.Text = "中右"
Ca DataGridViewContentAlignment.BottomLeft cbCellInfoAlign.Text = "下左"
Ca DataGridViewContentAlignment.BottomCenter cbCellInfoAlign.Text = "下中"
Ca DataGridViewContentAlignment.BottomRight cbCellInfoAlign.Text = "下右"
Ca El
cbCellInfoAlign.Text = "未设定"
End Select
Select Ca lCell.Style.Font.FontFamily.Name
Ca "仿宋"
cbCellInfoFontName.Text = "仿宋"
Ca "楷体"
cbCellInfoFontName.Text = "楷体"
Ca "⾪书"
cbCellInfoFontName.Text = "⾪书"
Ca "⿊体"
cbCellInfoFontName.Text = "⿊体"
Ca El
cbCellInfoFontName.Text = "仿宋"
End Select
cbCellInfoFontSize.Text = lCell.Style.Font.SizeInPoints
Select Ca lCell.Style.Font.Style
Ca FontStyle.Regular
cbCellInfoFontStyle.Text = "正常"
Ca FontStyle.Bold
cbCellInfoFontStyle.Text = "加粗"
Ca FontStyle.Italic
cbCellInfoFontStyle.Text = "倾斜"
Ca FontStyle.Underline
cbCellInfoFontStyle.Text = "下划线"
Ca FontStyle.Strikeout
cbCellInfoFontStyle.Text = "删除线"
End Select
membraneEl
cbCellInfoForeColor.Text = "透明"
cbCellInfoBackColor.Text = "透明"
fanciercbCellInfoAlign.Text = "未设定"
cbCellInfoFontName.Text = ""
cbCellInfoFontStyle.Text = ""
cbCellInfoFontSize.Text = ""
End If
End Sub
运⾏如下图:
那么相应的修改单元格信息的代码如下:
'修改选中的单元格信息
Private Sub btnCellInfoEdit_Click(nder As Object, e As EventArgs) Handles btnCellInfoEdit.Click Dim ColumnIndex, RowIndex As Integer
ColumnIndex = lblCellInfoColIndex.Text
RowIndex = lblCellInfoRowIndex.Text
Dim lCell As DataGridViewCell
lCell = dgv(ColumnIndex, RowIndex)
lCell.Value = txtCellInfoValue.Text
lCell.ReadOnly = cbCellInfoReadonly.Checked
Select Ca cbCellInfoForeColor.Text
Ca "透明"
lCell.Style.ForeColor = Color.Empty
Ca "红⾊"
lCell.Style.ForeColor = Color.Red
lCell.Style.ForeColor = Color.Green
Ca "蓝⾊"
lCell.Style.ForeColor = Color.Blue
Ca "黄⾊"
lCell.Style.ForeColor = Color.Yellow
Ca "⽩⾊"
lCell.Style.ForeColor = Color.White
Ca "⿊⾊"
lCell.Style.ForeColor = Color.Black
Ca El
lCell.Style.ForeColor = Color.Empty
End Select
Select Ca cbCellInfoBackColor.Text
Ca "透明"
lCell.Style.BackColor = Color.Empty
Ca "红⾊"
lCell.Style.BackColor = Color.Red
Ca "绿⾊"
lCell.Style.BackColor = Color.Green
Ca "蓝⾊"
lCell.Style.BackColor = Color.Blue
Ca "黄⾊"
lCell.Style.BackColor = Color.Yellow
Ca "⽩⾊"
lCell.Style.BackColor = Color.White
Ca "⿊⾊"
lCell.Style.BackColor = Color.Black
Ca El
lCell.Style.BackColor = Color.Empty
End Select
Select Ca cbCellInfoAlign.Text
Ca "上左"
lCell.Style.Alignment = DataGridViewContentAlignment.TopLeft
Ca "上中"
lCell.Style.Alignment = DataGridViewContentAlignment.TopCenter
Ca "上右"
lCell.Style.Alignment = DataGridViewContentAlignment.TopRight
Ca "中左"
lCell.Style.Alignment = DataGridViewContentAlignment.MiddleLeft
Ca "居中"
lCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter Ca "中右"
lCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight Ca "下左"
lCell.Style.Alignment = DataGridViewContentAlignment.BottomLeft Ca "下中"
lCell.Style.Alignment = DataGridViewContentAlignment.BottomCenter Ca "下右"
lCell.Style.Alignment = DataGridViewContentAlignment.BottomRight End Select
Dim fName As String = cbCellInfoFontName.Text
杭外英特Dim fFamilly As New FontFamily(fName)
Dim fStyle As FontStyle
Select Ca cbCellInfoFontStyle.Text
Ca "正常"
fStyle = FontStyle.Regular
Ca "加粗"
fStyle = FontStyle.Bold
Ca "倾斜"
fStyle = FontStyle.Italic
fStyle = FontStyle.Underline
Ca "删除线"
fStyle = FontStyle.Strikeout
End Select
Dim newFont As New Font(fFamilly, CType(cbCellInfoFontSize.Text, Single), fStyle) lCell.Style.Font = newFont
End Sub
运⾏如下图:
由于平台下C#和vb很相似,本⽂也可以为C#爱好者提供参考。
学习更多vb知识,请参看英文新年祝福