all groups > dotnet windows forms databinding > september 2005 >
You're in the

dotnet windows forms databinding

group:

Refreshing a dataview source to a datagrid



Refreshing a dataview source to a datagrid Michael Kellogg
9/22/2005 3:29:50 PM
dotnet windows forms databinding: I have a table that I've built by saying something like
"SELECT * FROM Manifest_Details WHERE [manifest_id] = @manifest_id". I can
pull up the information into a dataset/datatable nicely, and then I build a
DataView off of the table and set it as the DataSource for my DataGrid.

Everything related to that works fine; but I just ran into an issue wherein
I am changing the manifest_id for a given record, to move it out of that
manifest. The Update works fine (the underlying data is changed as
expected), however the record never disappears from my DataGrid. I can't
figure out why this is. Examination of the DataView indicates the record
IS gone. I've tried reassigning the DataSource and calling
DataGrid.REFRESH, as well as doing a FILL on the dataset again. Nothing
seems to fix it.

Any ideas?

--
Re: Refreshing a dataview source to a datagrid Bart Mermuys
9/23/2005 12:00:00 AM
Hi,

[quoted text, click to view]

I'm not sure why it wouldn't work if eg. you used a DataView with a
RowFilter set to show only one foreign key.

But there isn't any reason why the DataRow should disappear, which is
probely more what you want. There are two things you can do:

1) After the Update, enumerate all rows and remove (dataTable.Rows.Remove)
the rows whose foreign key has changed (since the last fill)

2) After the Update, clear the DataTable/DataSet and re-fill, but before
re-assigning it, try to set the DataSource to null first:
dataGrid.DataSource = null;
dataGrid.DataSource = someDataSource;

HTH,
Greetings

[quoted text, click to view]

Re: Refreshing a dataview source to a datagrid Bart Mermuys
9/23/2005 12:00:00 AM
Hi,

[quoted text, click to view]

No, it won't.

- someDataRow.Delete(); will mark the row for deletion, once you call
DataAdapter.Update it will be deleted in the db. Since DataAdapter.Update
implicitly calls AcceptChanges, all rows that were marked for deletion will
now be removed from the rows collection.

- someDataTable.Rows.Remove( someDataRow ) will straightaway remove the
DataRow from the row collection and since the row is gone it can't have a
delete flag and it will not be deleted in db on update.


[quoted text, click to view]

All i can say is, it should work. If you clear the relevant table
(DataTable.Clear()) and then re-fill it, the DataGrid should see this. You
don't even need to re-assign it, like i first said.

HTH,
greetings

[quoted text, click to view]

Re: Refreshing a dataview source to a datagrid Michael Kellogg
9/23/2005 9:01:01 AM
[quoted text, click to view]

Well it turns out that had been part of the problem. I was NOT
explicitly setting a rowfilter for the manifest_id criterion because the
SELECT query I used to originally generate the datatable had a WHERE
clause for this already, so early-on it never occurred to me to set a
dataview RowFilter for this condition, too.

I was using a RowFilter, but for other conditions. This turned out to
ding me, as well, because when I clued-in that I needed such a filter for
the manifest_id foreign key, I just stuck it in there, forgetting about
my subroutine that constantly is resetting that filter based on those
other conditions. So my foreign key RowFilter would just get wiped out
by my standard filter-setting routine that I'd forgotten about and I'd go
on thinking this RowFilter idea wasn't working. This is getting
embarassing...

The long and the short of it is that I've incorporated the additional
foreign key criterion into the standard filter-setting sub, now, and it
works great.

[quoted text, click to view]

The problem with this would have been that I don't want to actually
DELETE the rows. I just want to zero-out their foreign key. If I do a
Rows().Remove, assuming I had a functioning DeleteCommand in place,
wouldn't this kill off the row?

[quoted text, click to view]

I tried this, too, and it didn't work. Strange, really, I always thought
that re-doing the FILL command would re-fill the underlying DataTable and
my view would reflect this new table, and in fact it seemed that the
records WERE disappearing from the view. However, the grid was still
showing them there. The only way to really get it to refresh seemed to
be to re-run the queries entirely. That meant doing a CLEAR on each of
the datatables in the dataset, then re-querying and re-attaching to the
grid.

Thanks, Bart, for your help with this.
--
Re: Refreshing a dataview source to a datagrid Michael Kellogg
9/23/2005 11:18:03 AM
[quoted text, click to view]

Without having tried it again, I can already see that the problem is I was
not CLEARing the table. I have used this method before, maybe in VB6,
successfully, just doing the NULL and the reassignment. I neglected to do
a table.clear, which I'm sure would make it work as you described. My bad.

Thanks again.

--
AddThis Social Bookmark Button