Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 25th, 2004, 04:12 PM
Registered User
 
Join Date: Aug 2004
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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

Reply With Quote
  #2 (permalink)  
Old August 26th, 2004, 11:06 PM
Registered User
 
Join Date: Aug 2004
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I guess people are too scared to answer my question?

-g

Reply With Quote
  #3 (permalink)  
Old August 27th, 2004, 01:49 AM
Friend of Wrox
 
Join Date: Jul 2004
Location: Tehran, , Iran.
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

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

--------------------------------------------
Mehdi.:)
Reply With Quote
  #4 (permalink)  
Old August 27th, 2004, 08:35 AM
Registered User
 
Join Date: Aug 2004
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

Reply With Quote
  #5 (permalink)  
Old August 27th, 2004, 09:16 AM
Friend of Wrox
 
Join Date: Jul 2004
Location: Tehran, , Iran.
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

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.:)
Reply With Quote
  #6 (permalink)  
Old August 27th, 2004, 09:59 AM
Registered User
 
Join Date: Aug 2004
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

Reply With Quote
  #7 (permalink)  
Old August 27th, 2004, 11:56 AM
Friend of Wrox
Points: 3,489, Level: 24
Points: 3,489, Level: 24 Points: 3,489, Level: 24 Points: 3,489, Level: 24
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Central, NJ, USA.
Posts: 1,102
Thanks: 0
Thanked 2 Times in 2 Posts
Default

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
Reply With Quote
  #8 (permalink)  
Old August 27th, 2004, 12:46 PM
Registered User
 
Join Date: Aug 2004
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.

Reply With Quote
  #9 (permalink)  
Old August 27th, 2004, 01:59 PM
Friend of Wrox
Points: 3,489, Level: 24
Points: 3,489, Level: 24 Points: 3,489, Level: 24 Points: 3,489, Level: 24
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Central, NJ, USA.
Posts: 1,102
Thanks: 0
Thanked 2 Times in 2 Posts
Default

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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Export to Excel only the filtered data Suresh62 Access VBA 0 May 23rd, 2008 10:26 AM
Data Binding - Editing GridView Row Data desk_star BOOK: Professional ASP.NET 2.0 and Special Edition; ISBN: 978-0-7645-7610-2; ISBN: 978-0-470-04178-9 7 December 30th, 2007 11:07 AM
data binding g2000 ASP.NET 2.0 Basics 0 January 13th, 2006 03:36 PM
using data filtered from asp.net page to generate ashutoshpandey31 Crystal Reports 0 March 12th, 2005 09:40 AM
Data binding. mmwaikar ADO.NET 0 July 28th, 2003 12:51 PM



All times are GMT -4. The time now is 09:14 AM.


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