通过 DataGridView.CellParsing 事件可以设定用户输入的值。下面的示例:当输入英文文本内容的时候,立即被改变为大写。
[VB.NET]
'CellParsing 事件处理方法
Private Sub DataGridView1_CellParsing(ByVal sender As Object, _
ByVal e As DataGridViewCellParsingEventArgs) _
Handles DataGridView1.CellParsing
Dim dgv As DataGridView = CType(sender, DataGridView)
' 单元格列为“Column1”时
If dgv.Columns(e.ColumnIndex).Name = "Column1" AndAlso _
e.DesiredType Is GetType(String) Then
' 将单元格值设为大写
e.Value = e.Value.ToString().ToUpper()
' 解析完毕
e.ParsingApplied = True
End If
End Sub
'CellParsing 事件处理方法
Private Sub DataGridView1_CellParsing(ByVal sender As Object, _
ByVal e As DataGridViewCellParsingEventArgs) _
Handles DataGridView1.CellParsing
Dim dgv As DataGridView = CType(sender, DataGridView)
' 单元格列为“Column1”时
If dgv.Columns(e.ColumnIndex).Name = "Column1" AndAlso _
e.DesiredType Is GetType(String) Then
' 将单元格值设为大写
e.Value = e.Value.ToString().ToUpper()
' 解析完毕
e.ParsingApplied = True
End If
End Sub
[C#]
//CellParsing 事件处理方法
private void DataGridView1_CellParsing(object sender,
DataGridViewCellParsingEventArgs e)
{
DataGridView dgv = (DataGridView)sender;
//单元格列为“Column1”时
if (dgv.Columns[e.ColumnIndex].Name == "Column1" &&
e.DesiredType == typeof(string))
{
//将单元格值设为大写
e.Value = e.Value.ToString().ToUpper();
//解析完毕
e.ParsingApplied = true;
}
}
//CellParsing 事件处理方法
private void DataGridView1_CellParsing(object sender,
DataGridViewCellParsingEventArgs e)
{
DataGridView dgv = (DataGridView)sender;
//单元格列为“Column1”时
if (dgv.Columns[e.ColumnIndex].Name == "Column1" &&
e.DesiredType == typeof(string))
{
//将单元格值设为大写
e.Value = e.Value.ToString().ToUpper();
//解析完毕
e.ParsingApplied = true;
}
}
本文转自 qianshao 51CTO博客,原文链接:http://blog.51cto.com/qianshao/201796,如需转载请自行联系原作者