p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   C# (http://p2p.wrox.com/forumdisplay.php?f=22)
-   -   Binding to Filtered Data (or Data subsets) (http://p2p.wrox.com/showthread.php?t=17426)

gdbjohnson August 25th, 2004 04:12 PM

Binding to Filtered Data (or Data subsets)
 
I'm new to C#, coming from VB6.

I have a Dataset set up to hold data from a table.
I read somewhere about DataView's, and have bound a textbox successfully to a DataViewManager.

When I apply a row filter to the DataView, the Databound control does not get updated.

What is the best way to get databound controls to work with data subsets? Before, what I would do is just requery the database. But with datasets, the idea is to minimize resending queries, and just filter the data you have already brought over, right?

g


gdbjohnson August 26th, 2004 11:06 PM

I guess people are too scared to answer my question?

-g


mehdi62b August 27th, 2004 01:49 AM

oh,not at all...
when do you bind the Databound control?are you sure to do this?

--------------------------------------------
Mehdi.:)

gdbjohnson August 27th, 2004 08:35 AM

I believe I have set up the bindings correctly. I have been able to increment through my result set fine. The problem lies in the following:

when the contact changes the following code executes:

Code:

dsContact.Tables["Address"].DefaultView.RowFilter = "AddressID = '" + dr["AddressID"].ToString() + "'";

this.BindingContext[dvContact, "Address"].Position = 0;

I find that the rowfilter works. When I check the Current record visible from the dataview, the city is "Toronto", but the TextField will continue to display "New York" from the previous Address record. The binding value does not update after I filter the dataset.

How can I get this to update properly?

This is how I set up the databinding:
Code:

txtCity.DataBindings.Add("Text", dvContact, "Address.City");
where dvContact is a DataViewManager built from dsContact which is my DataSet object.

Thanks in advance for your help.

-g


mehdi62b August 27th, 2004 09:16 AM

are you sure you bind your grid in proper places?
Code:

private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
   //here you shoud give datasource to your grid and bind your grid for the first Page_Load
}
//dont bind your grid here
}
private void UpdateButton_Click(object sender, System.EventArgs e)
{
//1-change your datasource
//2-set DataSource property of your grid with new datasource
//3-bind your datagrid
}

your algorithm should be something like above to assure your grid updates properly,
HtH.


--------------------------------------------
Mehdi.:)

gdbjohnson August 27th, 2004 09:59 AM

The IsPostBack looks to be an ASP specific property?

I'm pretty sure that my binding's are set up correctly.
in the Page_Load event:
Code:

adpContact = new SqlDataAdapter(strSQL, sqlConn);           
adpAddress = new SqlDataAdapter(strSQL, sqlConn);

//Configure DataSet
dsContact = new DataSet("dsContact");
adpAddress.Fill(dsContact, "Address");
adpContact.Fill(dsContact, "Contact");
dsContact.Relations.Add( "FK_Address_Contact", dsContact.Tables["Address"].Columns["AddressID"], dsContact.Tables["Contact"].Columns["AddressID"]);

//Configure DataViewManager over DataSet dsContact
dvContact = new DataViewManager(dsContact);
dvContact.DataViewSettings["Contact"].Sort = "LastName";
//dvContact.DataViewSettings["Contact"].RowFilter = "ContactID >= 2";

dvContact.DataViewSettings["Address"].Sort = "AddressID";
//dvContact.DataViewSettings["Address"].RowFilter = "AddressID


txtCity.DataBindings.Add("Text", dvContact, "Address.City");

Binding bn = new Binding("Text", this.BindingContext[dvContact, "Contact"], "Position");
bn.Format += new ConvertEventHandler(this.FormatRecPosition);
txtRecPosition.DataBindings.Add(bn);

BindingManagerBase b;
b = this.BindingContext[dvContact, "Contact"];
b.CurrentChanged += new System.EventHandler(this.ContactBinding_CurrentChanged);

b = this.BindingContext[dvContact, "Address"];
b.CurrentChanged += new System.EventHandler(this.AddressBinding_CurrentChanged);

I was thinking that maybe after the filter gets applied, although the data changes, the position number stays the same (at 0) which is why the databound controls do not update... maybe the position value is the trigger?

-g


Hal Levy August 27th, 2004 11:56 AM

You must rebind a data control when the underlying data changes externally to the control.. such as your doing.

Clear the binding and then re-bind it.


Hal Levy
Web Developer, PDI Inc.

NOT a Wiley/Wrox Employee

gdbjohnson August 27th, 2004 12:46 PM

I can't see how that can be correct, because this defeats the purpose of binding. I might as well just set the values manually.

Data changes externally to the control every time you change the CurrentBindingContext's position, but of course when that happens, the bound control's are supposed to update accordingly.

I am not doing anything different: By applying a filter to the data, all I am doing is restricting the set of rows available to browse/edit, but the structure of the data is not changing.

As I've been working, I'm now thinking that maybe I should be binding text boxes to a DataRowView object, rather than the DataViewManager... but I haven't seen this documented anywhere.

As a general comment, I'm rather surprised at how little documentation there is regarding binding to subsets of a Dataset, since this must be a common requirement. I've seen hints of it using the DataViewManager, but this isn't working (for me) for binding to TextBoxes.


Hal Levy August 27th, 2004 01:59 PM

According to what I read in _expert one-on-one VB.NET Business Objects_ (Now an APress book) (if I am reading it correctly) some of these functions will not actually change the existing object (in this case, the dataset) but return a new object. Since that is the case, the binding needs to be re-done to the new object that was returned.

I am no where near as smart as Rocky Lhotka- so I might have misunderstood what he wrote- but that's how I read it.

Hal Levy
Web Developer, PDI Inc.

NOT a Wiley/Wrox Employee


All times are GMT -4. The time now is 12:00 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.