
Originally Posted by
MacShand
Hi Kevin - im coding in vb.net and am new at programming so am unable to convert ... any further suggestions? I've found some samples, eg...
Code:
Private Sub DataGridView1_CellPainting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
Dim y As Integer
For y = 0 To DataGridView1.Rows.Count - 1
If (e.ColumnIndex = 0 And e.RowIndex = y) Then
If Not IsNumeric(DataGridView1(0, y).Value) Then ' for instance i wanted to make something like band end merged with the next cell(s.t.h. like header band )
e.AdvancedBorderStyle.Right = DataGridViewAdvancedCellBorderStyle.Single
End If
e.AdvancedBorderStyle.Top = DataGridViewAdvancedCellBorderStyle.Single
End If
If (e.ColumnIndex = 1 And e.RowIndex = y) Then
If y > 0 Then
If DataGridView1(1, y).Value = DataGridView1(1, y - 1).Value Then
Else
e.AdvancedBorderStyle.Top = DataGridViewAdvancedCellBorderStyle.Single
End If
End If
End If
Next
End Sub
This is fine, but ideally, I'd like the merged cells to look like cells that are merged in excel, ie, just one line of text, centred in the merged cells, rather than the same thing repeated in all rows...(hope that makes sense)
help is greatly appreciated
Is there a reason for adding cells one by one? Have you considered creating a DataTable, adding rows to the DataTable then assign the DataTable to the DataGridView DataSource as this would make this a lot easier. Otherwise you are in for a great mess of code to contend with.
In the example below the rows added might simulate obtaining data from some source i.e. a text file etc.
Code:
Private Sub Demo()
Dim MockedData As New DataTable()
MockedData.Columns.AddRange(New DataColumn() _
{ _
New DataColumn("Identifier", GetType(System.Int32)), _
New DataColumn("ColumnA", GetType(System.String)), _
New DataColumn("ColumnB", GetType(System.String)) _
} _
)
MockedData.TableName = "Mocked"
MockedData.Rows.Add(New Object() {1, "A", "Apples"})
MockedData.Rows.Add(New Object() {1, "B", "Grapes"})
MockedData.Rows.Add(New Object() {1, "C", "Pears"})
MockedData.Rows.Add(New Object() {3, "D", "Cherries"})
MockedData.Rows.Add(New Object() {3, "E", "Oranges"})
MockedData.Rows.Add(New Object() {6, "F", "Prunes"})
MockedData.Rows.Add(New Object() {6, "G", "Grapes"})
MockedData.Rows.Add(New Object() {6, "H", "Oranges"})
MockedData.Rows.Add(New Object() {6, "I", "Oranges"})
DataGridView1.DataSource = MockedData
End Sub
I am not saying this is right for you since it sounds like you have taken a different path to load data but we need to be flexible and at least consider other options such as this.