Results 1 to 3 of 3

Thread: [RESOLVED] Datagridview Issue After Clear Columns

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2017
    Posts
    75

    Resolved [RESOLVED] Datagridview Issue After Clear Columns

    i have one datagridview call it dgv and two button
    in btn1 i want insert columns header this my code --->
    Code:
     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            dgv.Columns.Clear()
            If dgv.Columns.Count = 0 Then
                With dgv
                    .Columns.Add(0, "NID_S")
                    .Columns.Add(1, "NAMA_SPAREPART")
                    .Columns.Add(2, "MERK")
                    .Columns.Add(3, "HARGA")
                    .Columns.Add(4, "QTY")
                    .Columns.Add(5, "SUBTOTAL")
                    .AllowUserToAddRows = False
                End With
            Else
                Exit Sub
            End If
        End Sub
    and in btn2 i want load data from database access this my code --->
    Code:
      Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            dgv.Columns.Clear()
            Try
                Call koneksi()
                sql = "select nid_sp,nama_sparepart,merk,harga,qty,subtotal from detailpenjualan where no_faktur ='" & sernofaktur.Text & "' order by nid_sp asc"
                da = New OleDbDataAdapter(sql, conn)
                ds = New DataSet
                da.Fill(ds, "detailpenjualan")
                conn.Close()
            Catch ex As Exception
                MsgBox("Koneksi Load Data Sparepart Error", MsgBoxStyle.Critical, "Warning !")
                Exit Sub
            End Try
            With dgv
                .DataSource = ds.Tables("detailpenjualan")
                .AllowUserToAddRows = False
            End With
        End Sub
    i try to running this ..first i try click btn1 and so dgv correct next
    i click btn2 and showing data from access correctly . next i back click btn1 and going to issue
    issue is (showing headercolumns but show one row under header)...
    how to fix this issue..
    if my explanation unclear u can see my screenshot here
    Name:  Untitled-1.jpg
Views: 186
Size:  14.9 KB

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,347

    Re: Datagridview Issue After Clear Columns

    Why is it incorrect? You removed the columns from the grid but not the rows, so why would you expect adding new columns again to affect the number of rows? Your DataTable is still bound to the grid and it still contains one DataRow so the grid should contain one row, so what you're seeing is exactly correct. If you don;t want that DataTable bound to the grid any more then you need to set the DataSource property to Nothing. Of course, you could always just do that anyway and not bother with the columns. What's the point of removing all the columns and then adding new ones that are ostensibly exactly the same? In short, what you're doing doesn't really make sense so you should probably explain what you're trying to achieve, rather than how you're trying to achieve it. We can then explain how to do it properly.

    By the way, what is the point of that If...Else block in the first code snippet? You just cleared the Columns collection so how can Count be anything other than zero? Even if it could, what's the point of the Exit Sub when there's no more code after that anyway so the method would complete immediately after that anyway?

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2017
    Posts
    75

    Re: Datagridview Issue After Clear Columns

    Quote Originally Posted by jmcilhinney View Post
    Why is it incorrect? You removed the columns from the grid but not the rows, so why would you expect adding new columns again to affect the number of rows? Your DataTable is still bound to the grid and it still contains one DataRow so the grid should contain one row, so what you're seeing is exactly correct. If you don;t want that DataTable bound to the grid any more then you need to set the DataSource property to Nothing. Of course, you could always just do that anyway and not bother with the columns. What's the point of removing all the columns and then adding new ones that are ostensibly exactly the same? In short, what you're doing doesn't really make sense so you should probably explain what you're trying to achieve, rather than how you're trying to achieve it. We can then explain how to do it properly.

    By the way, what is the point of that If...Else block in the first code snippet? You just cleared the Columns collection so how can Count be anything other than zero? Even if it could, what's the point of the Exit Sub when there's no more code after that anyway so the method would complete immediately after that anyway?
    thanks dude i forget to clear it ...

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width