Hi

Can you change the alignment of text ion a datagridview column programmatically ?

i.e. Columns(0).?????????????????.MiddleCenter

as I dont want to centre align all of the columns in the datagrid.

Thanks

Andrew

Recommended Answers

All 4 Replies

Thanks.

That's done the trick.

it can also be done this way

Dim col0 As New DataGridViewTextBoxColumn
                col0.DataPropertyName = "your data field here"
                col0.HeaderText = "your Header Text here"
                col0.Width = 75
                col0.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                DataGridView1.Columns.Add(col0)

Hi,

how could i export the data from datagridview wherein in first column the datatype is number and it is in middleright alignment then when i run my code the alignment comes in left not in right where my datagridview is showing?

when i run the code it will look like this and it is wrong
176 2011-10-01 07:47:12 1 1 1 0
40 2011-10-01 07:55:15 1 0 1 0
25 2011-10-01 09:08:18 1 1 1 0
28 2011-10-01 10:09:01 1 0 1 0
28 2011-10-01 18:01:05 1 1 1 0

this is my code


Dim sTab As Char = Convert.ToChar(Keys.Tab)
Dim numCols As Integer = DataGridView1.ColumnCount
Dim numRows As Integer = DataGridView1.RowCount - 1
Dim strDestinationFile As String = "C:\ITKS\1_attlog.dat"
Dim tw As TextWriter = New StreamWriter(strDestinationFile)
Try

For count As Integer = 0 To numRows - 0
tw.Write(" ")
For count2 As Integer = 0 To numCols - 1
tw.Write(DataGridView1.Rows(count).Cells(count2).Value)
If (count2 <> numCols) Then

If count2 = 1 Then
tw.Write(" ")
End If
If count2 = 0 Or count2 = 2 Or count2 = 3 Or count2 = 4 Or count2 = 5 Then
tw.Write(sTab)
End If

End If


Next
tw.WriteLine()
Next
tw.Close()

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.