Here's the code i'm using for the same purpose as you...
Note that i have 2 custom classes called DataGridColoredTextBoxColumn and DataGridCustomTextBoxColumn, you might want to ignore that.
vb Code:
Private Sub formataGrelhaEntidades() Try Dim aGridTableStyle2 As New DataGridTableStyle Dim locationCol1 As New DataGridColoredTextBoxColumn Dim locationCol2 As New DataGridCustomTextBoxColumn Dim locationCol3 As New DataGridTextBoxColumn Dim locationCol4 As New DataGridTextBoxColumn Dim locationCol5 As New DataGridTextBoxColumn aGridTableStyle2.MappingName = myDataset.ToString Me.DataGridEntidades.RowHeadersVisible = False Me.DataGridEntidades.ColumnHeadersVisible = True With locationCol1 .backgroundColor = Color.LightSteelBlue .ForeColor = Color.Black .MappingName = "Cod" .HeaderText = "Cod" .Width = 44 .NullText = "" End With With locationCol2 .Owner = Me.DataGridEntidades .Alignment = HorizontalAlignment.Left .AlternatingBackColor = Color.LightSteelBlue .MappingName = "Nome" .HeaderText = "Empresa" .ReadOnly = True .Width = Screen.PrimaryScreen.WorkingArea.Width - locationCol1.Width - 16 .NullText = "" End With With locationCol3 .MappingName = "Morada" .HeaderText = "Morada" .Width = -1 .NullText = "" End With With locationCol4 .MappingName = "CodPostal" .HeaderText = "CodPostal" .Width = -1 .NullText = "" End With With locationCol5 .MappingName = "Contacto" .HeaderText = "Contacto" .Width = -1 .NullText = "" End With With aGridTableStyle2.GridColumnStyles .Add(locationCol1) .Add(locationCol2) .Add(locationCol3) .Add(locationCol4) .Add(locationCol5) End With With DataGridEntidades .BackColor = Color.AliceBlue .BackgroundColor = Color.AliceBlue .GridLineColor = Color.RoyalBlue .HeaderBackColor = Color.SteelBlue .SelectionBackColor = Color.LightCoral .SelectionForeColor = Color.DarkBlue End With DataGridEntidades.TableStyles.Clear() DataGridEntidades.TableStyles.Add(aGridTableStyle2) Catch ex As Exception ' End Try End Sub




and rating it.
Reply With Quote