• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 377
  • Last Modified:

"Requery" a datagrid based on changes in underlying dataset

Hi,

I have a datagrid (DataGrid1) on a webform.  The datagrid is "bound" to the dataset dsJobs1.  DsJobs1 is derived from OleDBAdapter1.  The datasource of OleDBAdapter1 is:

SELECT JobID, JobTitle, JobDesc
FROM Jobs
WHERE (JobFilled=0);

The connection is to a single table in a Microsoft Access Database on the server.

In the property builder of the datagrid, I am displaying the "Delete" column, and the control appears as a pushbutton.  When the "Delete" button is pushed, I don't really want it to delete the row from the database; I want it to change the value of JobFilled to -1, and redisplay the datagrid (This is an oversimplification; I'm doing other stuff, too, but for these purposes, I'll just use this as the example).  Since JobFilled is no longer 0, the row that I just "deleted" shouldn't be there.

So far, I have the following code:

    Private Sub DataGrid1_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.DeleteCommand

        Dim intIndex = e.Item.ItemIndex
        Dim ds As DataSet
        Dim tbl As DataTable
        ds = Me.DsJobs1
        tbl = ds.Tables("Jobs")

        With tbl.Rows(intIndex)
            .Item("JobFilled") = -1
        End With

        Me.OleDbDataAdapter1.Update(ds)
        Me.DataGrid1.DataBind()
        Me.DataGrid1.EditItemIndex = -1
        Me.DataGrid1.DataBind()

    End Sub

Besides some possibly redundant coding (do I really need to databind the datagrid twice at the end of the sub?)...this is not giving me the results I'm hoping for.  The datagrid still shows the "Deleted" row.  If I exit the form, and open it back up again, it's gone, but I want it to update immediately.

Any help?

Thanks,

Kevin
0
kmoloney
Asked:
kmoloney
  • 6
  • 3
1 Solution
 
razorback041Commented:
try refilling your dataadapter after your update
that should give you the results of a new query(what you get when you open up the form again)
0
 
kmoloneyAuthor Commented:
That kind of works.

The first time the button is clicked, the display doesn't change.  The next time, the "deleted" row is gone.

However, if I keep on doing this a couple times, I'll get an error message "There is no row at position X" where x is the row I'm deleting.

So it seems like it's changing the data every time I do this, but only updating the display every other time.
0
 
kmoloneyAuthor Commented:
CURRENT CODE:

 Private Sub DataGrid1_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.DeleteCommand
        Dim intIndex = e.Item.ItemIndex
        Dim ds As DataSet
        Dim tbl As DataTable
        ds = Me.DsJobs2
        tbl = ds.Tables("Jobs")

        With tbl.Rows(intIndex)
            .Item("JobFilled") = -1
            .Item("JobFilledBy") = Session("UserID")
            .Item("JobDateFilled") = Now()
        End With

        Me.OleDbDataAdapter1.Update(ds)
        Me.OleDbDataAdapter1.Fill(ds)
        Me.DataGrid1.DataBind()
        Me.DataGrid1.EditItemIndex = -1
        Me.DataGrid1.DataBind()
    End Sub
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
kmoloneyAuthor Commented:
The extra fields in the code listed should be superflous...also the fact that my dataset is named dsJobs2 instead of dsJobs1.  This is the actual code...I was trying to simplify it earlier, and then just copied and pasted the code on this one...cause I'm in a hurry! :)
0
 
razorback041Commented:
That kind of works.
The first time the button is clicked, the display doesn't change.  The next time, the "deleted" row is gone.
However, if I keep on doing this a couple times, I'll get an error message "There is no row at position X" where x is the row I'm deleting.
So it seems like it's changing the data every time I do this, but only updating the display every other time

im realy not sure of why...try taking out the first DataBind() and clearing the dataset completely ...so you have

        Me.OleDbDataAdapter1.Update(ds)
        Me.DataGrid1.DataSource = Nothing
        Me.OleDbDataAdapter1.Fill(ds)
        Me.DataGrid1.DataBind()
        Me.DataGrid1.EditItemIndex = -1
    End Sub

if not...try setting a break point and making sure everything is cleared and reset each time
0
 
kmoloneyAuthor Commented:
Yeah, its all running - display doesn't change, though.  If I hit the refresh key, it works, but I have to "resubmit" the data (probably because of the session variable?)  No solution yet.
0
 
razorback041Commented:
you could always call the page_load to force it if you wont be losing anything off the page
0
 
kmoloneyAuthor Commented:
I thought about that - that's probably what I would want.  But somewhat of a newbie.  Don't I have to pass the "Sender as System.Object" and "e as System.EventArgs" parameters?  How do I do that?
0
 
kmoloneyAuthor Commented:
Never did find out how to force a refresh...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now