Hi all
I have a DataGridView with 2 Columns an Order column (text) and a price Column (decimal)
The Datagrid contains all the orders and price for a certain customer.
I'm currently adding all the prices and adding a new row to the datatable containing the total.In this row on the orders column it has the word "Total" and the Price column the calculated total. It all seams too be working fine.
Since the order column is wide and left align when the word "Total" is added it is to far from the calculated total.
What I need help is how to make the last row of the orders column right align so the word total will be closer to the calculated total While maintaining the remaining rows Left align..

Thanks in advance.

Dani AI

Generated

Nice work, . To make the alignment stick through repaints, rebinds, and sorts, apply it at render time instead of only once when you add the row. The simplest way is to key off the cell value and set the style inside CellFormatting for the Orders column. This targets only the "Total" label and leaves all other rows left-aligned, as you want. See the event’s behavior notes for why this is reliable. (learn.microsoft.com)

' Align only the "Total" text in the Orders column
Private Const OrdersColumnIndex As Integer = 0

Private Sub DataGridView1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) _
    Handles DataGridView1.CellFormatting

    If e.ColumnIndex = OrdersColumnIndex Then
        Dim text = TryCast(e.Value, String)
        If text IsNot Nothing AndAlso String.Equals(text.Trim(), "Total", StringComparison.OrdinalIgnoreCase) Then
            Dim cell = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex)
            cell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
            cell.Style.Font = New Font(DataGridView1.Font, FontStyle.Bold)
        End If
    End If
End Sub

While you are at it, format the Price column so all numbers are right-aligned and display as currency. This keeps the total visually lined up with the rest of the prices:

With DataGridView1.Columns("Price").DefaultCellStyle
    .Alignment = DataGridViewContentAlignment.MiddleRight
    .Format = "C2"
End With

DefaultCellStyle.Alignment and the DataGridViewContentAlignment enum govern the horizontal/vertical positioning here. (learn.microsoft.com)

One more tip: if your Total row lives in the bound DataTable, sorting can move it away from the bottom. If that matters, handle DataGridView.Sorted (or reapply the style in DataBindingComplete) to recheck the cell and ensure it is still right-aligned after a sort operation. (learn.microsoft.com)

Recommended Answers

All 3 Replies

Thanks for your help.
here is the code that works.

Me.DataGridView1.Rows(Me.DataGridView1.Rows.Count - 1).Cells(0).Style.Alignment = DataGridViewContentAlignment.BottomRight

Remember to mark as solved....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.