Results 1 to 3 of 3

Thread: [RESOLVED] How do I edit a DataGridView?

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2014
    Posts
    55

    Resolved [RESOLVED] How do I edit a DataGridView?

    I want a user to be able to click on the data grid and edit certain aspects of the cells (not the primary key). They then click an update button which updates the database with the new content. I've got the 'Enable Editing' check box ticked but that doesn't allow me to change the contents. Can someone help?
    This is what I've got so far:
    Code:
    Private Sub frmEditUsers_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ds = New DataSet
            da = New OleDbDataAdapter("SELECT * FROM tblUsers", con)
            Dim cmdBuilder As New OleDbCommandBuilder(da)
    
            da.Fill(ds, "tblUsers")
    
            Me.datagridUsers.DataSource = ds.Tables("tblUsers")
    
        End Sub
    
        Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
            Me.datagridUsers.Update()
    
            Try
                da.Update(ds.Tables("tblUsers"))
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End Sub

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,467

    Re: How do I edit a DataGridView?

    Firstly, this line is useless:
    Code:
        Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
            Me.datagridUsers.Update()
    
            Try
                da.Update(ds.Tables("tblUsers"))
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End Sub
    That Update method has nothing at all to do with the data in the grid. All it does is redraw the grid onscreen, which does nothing useful.

    Secondly, while it's a small thing, if all you want is a DataTable then why create a DataSet? Why not just create a DataTable?

    As for your issue, what exactly are the symptoms? Are you saying that you can't select a cell and modify its contents or that those modifications are not reflected in your database, because they are very different issues and your post is not exactly clear on that?

  3. #3

    Thread Starter
    Member
    Join Date
    Dec 2014
    Posts
    55

    Re: How do I edit a DataGridView?

    The problem I'm having is selecting the cell and modifying the data. I can select a cell, but that's all. I'm assuming that I should be able to select the cell, type something on the keyboard and the cell would change, but I'm not 100% sure that's how the feature works.

    The reason I'm using a DataSet is because I used it in a 'Delete' function I have, so I assumed that with a bit of editing, I could get it to work to update the database.

    EDIT: Nevermind, I've managed to get it working. I had to make each columns Read-Only property false. Hitting the update button updates the database too so it's all good.
    Last edited by magic_andrew; Jan 6th, 2015 at 07:51 AM. Reason: Found a fix

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