Asp.Net教程,WinForm教程,Asp.Net MVC,vs2008教程,vs2010教程,Silverlight技术,源码下载,Asp.Net视频教程
全站热门标签
vs2010 Silverlight 存储过程 水晶报表 ADO.NET JavaScript LINQ AjaxPro DataGridView 面向对象 Extjs GridView XML DevExpress HTML教程 Oracle jQuery 分页 GDI+ Visual C++2010 MySQL Office2010 WPF MVC Dojo WCF4.0 VB.NET Sql2005 textbox cookie WCF WinForm Discuz!NT SQL经典语句 T-SQL checkbox ASPxGridView F# asp.net SQL VS2008新特性 DropDownList Access TreeView Ajax VS2008 页面执行时间 Flex 字符串 回调 VB2005 DataSet C#时间 ASP.NET性能优化 用户在线检测 动画
FrameworkC#技术 VB.NET VC.NET WCF WPF
当前位置: 主页 > WinForm教程 > C#技术 >

DataGridView编程36计(7)

时间:2010-06-27 21:37来源:未知 作者:admin 点击:

DataGridView 单元格表示值的自定义
通过CellFormatting事件,可以自定义单元格的表示值。(比如:值为Error的时候,单元格被设定为红色)
下面的示例:将“Colmn1”列的值改为大写。
[VB.NET]
'CellFormatting 事件处理方法
Private Sub DataGridView1_CellFormatting(ByVal sender As Object, _
        ByVal e As DataGridViewCellFormattingEventArgs) _
        Handles DataGridView1.CellFormatting
    Dim dgv As DataGridView = CType(sender, DataGridView)
    ' 如果单元格是“Column1”列的单元格
    If dgv.Columns(e.ColumnIndex).Name = "Column1" AndAlso _
            TypeOf e.Value Is String Then
        ' 将单元格值改为大写
        Dim str As String = e.Value.ToString()
        e.Value = str.ToUpper()
        ' 应用该Format,Format完毕。
        e.FormattingApplied = True
    End If
End Sub
[C#]
//CellFormatting 事件处理方法
private void DataGridView1_CellFormatting(object sender,
    DataGridViewCellFormattingEventArgs e)
{
    DataGridView dgv = (DataGridView)sender;
// 如果单元格是“Column1”列的单元格
    if (dgv.Columns[e.ColumnIndex].Name == "Column1" && e.Value is string)
    {
        // 将单元格值改为大写
        string str = e.Value.ToString();
        e.Value = str.ToUpper();
        // 应用该Format,Format完毕。
        e.FormattingApplied = true;
    }
}
CellFormatting事件的DataGridViewCellFormattingEventArgs对象的Value属性一开始保存着未被格式化的值。当Value属性被设定表示用的文本之后,把FormattingApplied属性做为True,告知DataGridView文本已经格式化完毕。如果不这样做的话,DataGridView会根据已经设定的Format,NullValue,DataSourceNullValue,FormatProvider属性会将Value属性会被重新格式化一遍。


DataGridView 用户输入时,单元格输入值的设定
通过 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
[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;
    }
}


DataGridView 新加行的默认值的设定
需要指定新加行的默认值的时候,可以在DataGridView.DefaultValuesNeeded事件里处理。在该事件中处理除了可以设定默认值以外,还可以指定某些特定的单元格的ReadOnly属性等。
[VB.NET]
' DefaultValuesNeeded 事件处理方法
Private Sub DataGridView1_DefaultValuesNeeded(ByVal sender As Object, _
        ByVal e As DataGridViewRowEventArgs) _
        Handles DataGridView1.DefaultValuesNeeded
    ' 设定单元格默认值
    e.Row.Cells("Column1").Value = 0
    e.Row.Cells("Column2").Value = "-"
End Sub
[C#]
// DefaultValuesNeeded 事件处理方法
private void DataGridView1_DefaultValuesNeeded(object sender,
    DataGridViewRowEventArgs e)
{
    // 设定单元格的默认值
    e.Row.Cells["Column1"].Value = 0;
    e.Row.Cells["Column2"].Value = "-";
}

(责任编辑:admin)
Tags:DataGridView
责任编辑:admin
返回顶部
------分隔线----------------------------
推荐内容
骆驼户外男 真皮磨砂日常休闲鞋 低帮 2011秋冬新款 专柜正品特价 骆驼户外男 真皮磨砂日常休闲鞋 低帮 2011秋冬新款 专柜正品特价