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