首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > VB Dotnet >

datagridview 绑定数据表后 设置数字右对齐,文本左对齐,该如何处理

2012-03-18 
datagridview 绑定数据表后 设置数字右对齐,文本左对齐在绑定数据后,datagridview默认是所有单元格左对齐

datagridview 绑定数据表后 设置数字右对齐,文本左对齐
在绑定数据后,datagridview默认是所有单元格左对齐的。很多专业的软件都是数字友对齐,文本左对齐,如excel等 
这看起来很不专业的样子?。。。呵呵 其实自己本来就是小菜。
通过百度,有网友提供下面这个方法

VB.NET code
    Protected Overrides Sub OnCellPainting(ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs)        If IsNumeric(e.Value) Then            e.CellStyle.Alignment = DataGridViewContentAlignment.MiddleRight        Else            e.CellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft        End If        MyBase.OnCellPainting(e)    End Sub

在绘制单元格的时候,判断数据类型。然后按需要对齐方式绘制。
自个儿感觉这样的效率不高。因为每个单元都做了判断检查
我在另外一个绑定数据表的时候马上设置对齐方式代码如下 
VB.NET code
    ''' <summary>    ''' 重载,控制文本对齐    ''' </summary>    ''' <param name="e"></param>    ''' <remarks></remarks>    Protected Overrides Sub OnDataBindingComplete(ByVal e As System.Windows.Forms.DataGridViewBindingCompleteEventArgs)        For Each col As DataGridViewColumn In Columns            Select Case col.ValueType.ToString                Case GetType(Integer).ToString(), GetType(Double).ToString(), GetType(Decimal).ToString()                    col.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight            End Select        Next    End Sub

自己对type 不熟悉。不知道改怎么 数字 的type是什么?只好在Select Case 枚举 int32,Double,Decimal这三个类型,当然,数字类型还有其他的。 

不知道各位达人有啥更好的高招?


[解决办法]
通过遍历吧

热点排行