?
Solved

"Requery" a datagrid based on changes in underlying dataset

Posted on 2006-03-28
10
Medium Priority
?
374 Views
Last Modified: 2008-01-09
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
Comment
Question by:kmoloney
  • 6
  • 3
9 Comments
 
LVL 5

Accepted Solution

by:
razorback041 earned 1500 total points
ID: 16315097
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
 
LVL 2

Author Comment

by:kmoloney
ID: 16315569
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
 
LVL 2

Author Comment

by:kmoloney
ID: 16315574
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 2

Author Comment

by:kmoloney
ID: 16315601
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
 
LVL 5

Expert Comment

by:razorback041
ID: 16317432
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
 
LVL 2

Author Comment

by:kmoloney
ID: 16318062
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
 
LVL 5

Expert Comment

by:razorback041
ID: 16326928
you could always call the page_load to force it if you wont be losing anything off the page
0
 
LVL 2

Author Comment

by:kmoloney
ID: 16355128
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
 
LVL 2

Author Comment

by:kmoloney
ID: 16555219
Never did find out how to force a refresh...
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question