none
Add empty row to datagridview RRS feed

  • Question

  • Hi All

    I would like to add (with a button click) empty rows between to datagridview rows using Rectangle  (with let's say color.red) with the same width of any other row

    This is not the real application but it will be usefull for DGV grouping I  wish to build

    And of course I have also to delete this empty row with a button click

    Can anybody help me ?

    Monday, July 9, 2018 7:07 PM

All replies

  • If the DataGridView is bound to a data source then I don't think you'll be able to do this.  With unbound data you could insert empty rows, but with bound data you would need empty records and that could make a mess of the underlying data source (if it was even allowed e.g. you couldn't have any required fields in the bound data source)

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Monday, July 9, 2018 7:35 PM
    Moderator
  • Hi

    Here is a couple of thoughts that may help, but be aware of the reply above from Reed.

      Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        ' ADDS NEW ROW ABOVE CURRENT ROW
        ' BASED ON DATASOURCE DATATABLE
        dt.Rows.InsertAt(dt.NewRow, DataGridView1.CurrentRow.Index)
      End Sub
    
      Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        ' ADDS 'DIVIDER' BELOW CURRENT ROW
        ' CHANGE THE '22' (pixels) AS NEEDED
        DataGridView1.CurrentRow.DividerHeight = 22
        DataGridView1.AutoResizeRows()
      End Sub


    Regards Les, Livingston, Scotland

    • Proposed as answer by Alex Li-MSFT Tuesday, July 10, 2018 3:45 AM
    Monday, July 9, 2018 8:22 PM
  • Hi  Claudio

      'Add rows and set colors
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            dt.Rows.InsertAt(dt.NewRow, DataGridView1.CurrentRow.Index)
    
            For i = 0 To dt.Columns.Count - 1
                DataGridView1.Rows(DataGridView1.CurrentRow.Index - 1).Cells(i).Style.BackColor = Color.Red
            Next
        End Sub
        'Delete selected line
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            If DataGridView1.SelectedRows.Count > 0 Then
                Dim drv As DataRowView = TryCast(DataGridView1.SelectedRows(0).DataBoundItem, DataRowView)
                drv .Delete ()
            End If
        End Sub

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, July 10, 2018 3:47 AM