title: Center Align Cell Text in Vb.Net DataGridView
body: <p>I am working with DataGridView and there is a scenario that I have to show centered align text in selected columns, I have managed to set Header Center Align Text but with Row Cell and Conditions, I am unable to figure out how?</p>
  
  <p>Suppose I have 4 Rows with 3 Columns,<code>ID ,Name,Type</code>, On the bases of <code>Type</code> column I want to show my data as given below picture,
  <a href="https://i.stack.imgur.com/IzYN3.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IzYN3.jpg" alt=""></a></p>
  
  <p>In CellFormattingEvent I have managed to set different color scheme.</p>
  
  <pre><code>Private Sub grdDetailsNew_CellFormatting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles grdFruitDetailsNew.CellFormatting
      Try
          If e.RowIndex &gt; -1 Then
              If grdDetailsNew.Rows.Count &gt; 0 Then
                  If grdDetailsNew.Rows(e.RowIndex).Cells("Type").Value = 1 Then
                      e.CellStyle.BackColor = Color.FromArgb(253, 192, 97)
                      e.CellStyle.Font = New Font(e.CellStyle.Font.FontFamily, 17, FontStyle.Regular)
                  ElseIf grdDetailsNew.Rows(e.RowIndex).Cells("Type").Value = 2 Then
                      e.CellStyle.BackColor = Color.FromArgb(255, 249, 237)
                      e.CellStyle.Font = New Font(e.CellStyle.Font.FontFamily, 16, FontStyle.Regular)
                  Else
                      e.CellStyle.BackColor = Color.FromArgb(255, 255, 255)
                      e.CellStyle.Font = New Font(e.CellStyle.Font.FontFamily, 15, FontStyle.Regular)
                  End If
              End If
          End If
  
      Catch ex As Exception
          WriteToLog(ex)
      End Try
  End Sub
  Private Sub grdDetailsNew_CellPainting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles grdFruitDetailsNew.CellPainting
      Try
  
          If e.RowIndex &gt; -1 AndAlso e.ColumnIndex &gt; -1 Then
              If e.ColumnIndex = 2 AndAlso grdDetailsNew.Rows(e.RowIndex).Cells("Type").Value = 1 Then
                  e.CellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
              End If
          End If
  
      Catch ex As Exception
  
      End Try
  End Sub
  </code></pre>