CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2003
    Posts
    76

    Question Align data in datagrid to the right?

    Hi Gurus,

    I am trying to allign my numeric data in my datagrid to the right? does anyone have a clue?

    Thank you.

  2. #2
    Join Date
    Sep 2001
    Location
    Montreal Canada
    Posts
    1,080

    Re: Align data in datagrid to the right?

    You have to define a gridtablestyle and then a gridcolumnstyle then you can access the textbox if you add a text

    ReDim Preserve ColumnText(Columntextindex)
    ColumnText(ColumnText.GetUpperBound(0)) = New DataGridTextBoxColumn
    Dim temp As DataGridTextBoxColumn
    temp = ColumnText(ColumnText.GetUpperBound(0))
    temp.MappingName = "ColumnName you want"
    temp.Alignment = HorizontalAlignment.Center
    temp.HeaderText = "TEST"
    temp.TextBox.MaxLength = 26
    Temp.TextBox.Tag = 255
    temp.Width = 150
    Columntextindex = Columntextindex + 1
    Nicolas Bohemier

  3. #3
    Join Date
    Mar 2005
    Posts
    1

    Re: Align data in datagrid to the right?

    this will allign the text not the column header

    you should replace

    firend withEvents DatagridTextBoxColumn as DatagrdTextboxColumn
    with
    firend withEvents DatagridTextBoxColumn as DataGridTextBoxCurrency

    DatagridTextBoxColumn.... = new DatagrdTextboxColumn
    with
    DatagridTextBoxColumn.... = new DataGridTextBoxCurrency





    Imports System.Drawing
    Imports System.Windows.Forms




    Public Class DataGridTextBoxCurrency
    Inherits DataGridTextBoxColumn

    Private mTxtAlign As HorizontalAlignment = HorizontalAlignment.Right
    Private mDrawTxt As New StringFormat

    Public Sub New()
    MyBase.New()
    mDrawTxt.Alignment = StringAlignment.Far
    End Sub


    Protected Overloads Overrides Sub Edit(ByVal source As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal bounds As System.Drawing.Rectangle, ByVal [readOnly] As Boolean, ByVal instantText As String, ByVal cellIsVisible As Boolean)
    MyBase.Edit(source, rowNum, bounds, [readOnly], instantText, cellIsVisible)
    MyBase.TextBox.TextAlign = mTxtAlign
    End Sub

    Protected Overloads Overrides Sub Paint(ByVal g As System.Drawing.Graphics, ByVal bounds As System.Drawing.Rectangle, ByVal source As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal backBrush As System.Drawing.Brush, ByVal foreBrush As System.Drawing.Brush, ByVal alignToRight As Boolean)
    'clear the cell
    g.FillRectangle(backBrush, bounds)
    'draw the value
    Dim s As String = Me.GetColumnValueAtRow([source], rowNum).ToString()
    Dim r As Rectangle = bounds
    r.Inflate(0, -1)
    g.DrawString(s, MyBase.TextBox.Font, foreBrush, RectangleF.op_Implicit(r), mDrawTxt)
    End Sub

    Public Property DataAlignment() As HorizontalAlignment
    Get
    Return mTxtAlign
    End Get
    Set(ByVal Value As HorizontalAlignment)
    mTxtAlign = Value
    If mTxtAlign = HorizontalAlignment.Center Then
    mDrawTxt.Alignment = StringAlignment.Center
    ElseIf mTxtAlign = HorizontalAlignment.Right Then
    mDrawTxt.Alignment = StringAlignment.Far
    Else
    mDrawTxt.Alignment = StringAlignment.Near
    End If
    End Set
    End Property

    End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured