473,396 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Binding.FormatString rendering curly braces & trick to Bind to List<T>.Countproperty?

First problem:
I am specifying a format string for a Binding object like so:
<code>
Binding binding = new Binding("Text",
item.EOBRemittance,
"AmountAllowed",
true,
DataSourceUpdateMode.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.AttachmentIndices,
"Count",
true,
DataSourceUpdateMode.Never,
0,
"{0} Selected Pages");

_linkLabel_SelectPages.DataBindings.Clear();
_linkLabel_SelectPages.DataBindings.Add(binding);
</code>

In the above example item.AttachmentIndices 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 3394
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, ConvertEventArgs 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, ConvertEventArgs 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.SomeProp"
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
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...
1
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>...
4
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>....
3
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()>...
1
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>...
2
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...
1
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
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...
7
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...
0
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.