473,812 Members | 3,551 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Binding.FormatS tring rendering curly braces & trick to Bind to List<T>.Countpr operty?

First problem:
I am specifying a format string for a Binding object like so:
<code>
Binding binding = new Binding("Text",
item.EOBRemitta nce,
"AmountAllowed" ,
true,
DataSourceUpdat eMode.Never,
0,
"{0} Selected Pages");
</code>

When I run my application the Format string is working 50% ;0)

Instead of: "20 Selected Pages"
I see: "{20} Selected Pages"

So it's dropping the bound property's value in the right place, but it's
not removing the '{' and '}' characters. Does this seem like a bug or
am I doing something wrong?

Second problem:

I have a somewhat interesting situation where I would like to bind a
LinkLabel's Text property to the Count property on a List<T>. I know
List<T>.Count is readonly, but I thought you could still one-way bind to
a read only property?

Here is the code:
<code>
Binding binding = new Binding("Text",
item.Attachment Indices,
"Count",
true,
DataSourceUpdat eMode.Never,
0,
"{0} Selected Pages");

_linkLabel_Sele ctPages.DataBin dings.Clear();
_linkLabel_Sele ctPages.DataBin dings.Add(bindi ng);
</code>

In the above example item.Attachment Indices is a List<int>.
When I run my application, I get the following exception:
"Cannot bind to the property or column Count on the DataSource"

I tried the same with the Capacity property and it threw the same error,
however it is read/write. So maybe I'm barking up the wrong tree?

I look through reflector and (Couldn't find List<Tbut checked some
other classes) and didn't see any special attributes or anything.

Anyone have any ideas?
Jun 27 '08 #1
7 3450
I'm surprised the first scenario worked *at all*; I'll have a look...

Re the second - you can't bind to properties of anything that implements
IList or IListSource; it assmumes you mean "get the currency-manager for
that collection, lookup the item at the current cursor position, and
find the named property on that item". If you aren't doing anything else
with the collection, this is equivalent to looking at list[0].Count,
which is unlikely to exist.

Marc
Jun 27 '08 #2
To do the custom formatting:

Binding binding = new Binding("Text", source, "PropName",true );
binding.Format += delegate(object sender, ConvertEventArg s args)
{
args.Value = string.Format(" {0} items", args.Value);
};

Marc
Jun 27 '08 #3
Marc Gravell wrote:
To do the custom formatting:

Binding binding = new Binding("Text", source, "PropName",true );
binding.Format += delegate(object sender, ConvertEventArg s args)
{
args.Value = string.Format(" {0} items", args.Value);
};

Marc
Nice use of anonymous delegate, thanks for the snippet, Marc!
Jun 27 '08 #4
Marc Gravell wrote:
I'm surprised the first scenario worked *at all*; I'll have a look...

Re the second - you can't bind to properties of anything that implements
IList or IListSource; it assmumes you mean "get the currency-manager for
that collection, lookup the item at the current cursor position, and
find the named property on that item". If you aren't doing anything else
with the collection, this is equivalent to looking at list[0].Count,
which is unlikely to exist.

Marc
I didn't know that, I thought we could bind to *any* public property,
regardless of what interfaces it's class implements. This seems a bit
presumptuous of the framework to assume that I want one thing went I've
clearly set it up to use the "Count" property.

I'm bummed, this would have saved me several function calls and
concurrency issues with keeping my View up to date with my Model.

The only other semi-automatic solution I can come up with is to use a
collection that raises change notifications and wire those up to my View
method that updates the count control.

If you have any other suggestions I would dig hearing em' ;0)

Thanks for the explanation,
Steve
Jun 27 '08 #5
Well, depending on the setup, you could facade the count into the
containing class - i.e.

class Foo {
public List<BarBars {get {...}}

public int BarCount {get {return Bars.Count;}}
}

Now you can bind to a Foo, looking at "BarCount", and "Bars.SomeP rop"
to get the SomeProp from the selected "Bar". If you explain the setup
more, there may be other options...

Marc
Jun 27 '08 #6
Marc Gravell wrote:
Well, depending on the setup, you could facade the count into the
containing class - i.e.

class Foo {
public List<BarBars {get {...}}

public int BarCount {get {return Bars.Count;}}
}
The facade solution is so simple and in my case a perfect solution. I
overlooked it somehow, thanks for pointing it out.

Have a great weekend,
Steve
Jun 27 '08 #7
No problem; cheers,

Marc
Jun 27 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
5638
by: Dave | last post by:
Hello all, After perusing the Standard, I believe it is true to say that once you insert an element into a std::list<>, its location in memory never changes. This makes a std::list<> ideal for storing vertices of an arbitrary n-ary tree where a vertex contain pointers to its parent / children. These parent / child vertices need to stay put if we've got pointers to them somewhere! Am I correct in my assertion?
1
5461
by: RJN | last post by:
Hi I'm using XMLTextReader to parse the contents of XML. I have issues when the xml content itself has some special characters like & ,> etc. <CompanyName>Johnson & Jhonson</CompanyName> <EmployeeStrength>> 1000</EmployeeStrength> When I do a Xmltextreader.read() and then check the contents of the xml node by XmltextReader.ReadString(), I get an exception when I have
4
53020
by: matty.hall | last post by:
I have two classes: a base class (BaseClass) and a class deriving from it (DerivedClass). I have a List<DerivedClass> that for various reasons needs to be of that type, and not a List<BaseClass>. However, I need to cast that list to a List<BaseClass> and it is not working. The code is below. I get the following exception: "Unable to cast object of type 'System.Collections.Generic.List`1' to type 'System.Collections.Generic.List`1'." ...
3
2627
by: Eric | last post by:
I have a string representation of an object. I create an object of that type through reflection. I would like to create a List<> of those objects. I obviously can't do List<myObject.GetType()> or Type t = object.GetType(); List<t>;
1
2714
by: RJN | last post by:
Hi I'm using XMLTextReader to parse the contents of XML. I have issues when the xml content itself has some special characters like & ,> etc. <CompanyName>Johnson & Jhonson</CompanyName> <EmployeeStrength>> 1000</EmployeeStrength> When I do a Xmltextreader.read() and then check the contents of the xml node by XmltextReader.ReadString(), I get an exception when I have
2
2684
by: Brian Pelton | last post by:
I am not sure how to fix this problem I've stumbled into... I have a list<> of an interface type. I need to pass that list to a method that adds more objects to the list. But, eventually, I need to get back to the original list<> object. --- In other words; let's say I start with an List<> of 3 objects. I call a
1
6039
by: Diego | last post by:
I need to display a list of ojects (containing few fields like name, surname, adress) in a Grid, is it possible to directly bind the list of objects with the DataGridView? Thanks, Diego
0
1754
by: Iron Moped | last post by:
I'm airing frustration here, but why does LinkedList<not support the same sort and search methods as List<>? I want a container that does not support random access, allows forward and reverse traversal and natively supports sorting, i.e., STL's list<T>. There isn't even a set of algorithms that would allow me to easily sort a generic collection. System.Array has a robust set of static algorithms, why not extend this to ICollection?
7
57561
by: Andrew Robinson | last post by:
I have a method that needs to return either a Dictionary<k,vor a List<v> depending on input parameters and options to the method. 1. Is there any way to convert from a dictionary to a list without itterating through the entire collection and building up a list? 2. is there a common base class, collection or interface that can contain either/both of these collection types and then how do you convert or cast from the base to either a...
0
1355
by: SC | last post by:
How do I create at runtime a list of string (List<stringstrs = new List<string>(); fill it) and then bind it to a DataGridViewColumnBox? Setting the columns DataSource to the list doesn't display any data, i.e., OnNewRow Event: myDataViewGrid.Row.Cell.DataSource = strs;
0
10404
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10417
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10139
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7678
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6897
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5568
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4357
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3881
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3029
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.