Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Data Binding / January 2007

Tip: Looking for answers? Try searching our database.

Data Grid Behavior with Normal Controls

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Brian Pelton - 22 Jan 2007 16:36 GMT
Is there a way to get the data grid behavior of working with multiple
rows, but use conventional controls like text boxes and combo boxes?

Let's say I have form that is used for orders.  Each order can have
multiple lines, but I don't want to use a datagrid for order lines.

I would rather use a series of combo boxes and text boxes.  I would
create three sets of controls and I would like to bind those sets to a
`System.Windows.Forms.BindingSource`.  The BindingSource's DataSource is
an IList<>.

Because an order could have more than 3 order lines, I would need to be
able to "scroll" through the lines.

Does this make sense?  Is this possible?

Thanks for any advice...

Brian
Brian Pelton - 22 Jan 2007 23:39 GMT
> Is there a way to get the data grid behavior of working with multiple
> rows, but use conventional controls like text boxes and combo boxes?

I put together something that works, but it feels like there should be a
better way to do it.

For 3 rows, I created 3 BindingSource objects.
For each one, I sent the data source to my IList<> object.
I used MoveNext() on dbs2 and dbs3 to push them to the second and third
items in the list.

I then created three sets of text boxes.  So like txtName1, txtName2,
txtName3, etc.

I databound txtName1's Text property to dbs1 and txtName2's Text
property to dbs2 and so on.

This gives the the basic behavior I am looking for.

But it feels like the wrong way to do it.  Perhaps if I make some kine
of "OcurrsManager" that would take an int and build the second and third
sets of controls and perform the databinding at runtime, it would feel
cleaner.

I'm still open to any ideas / suggestions anyone has..

Thanks,
Brian
Linda Liu [MSFT] - 23 Jan 2007 04:27 GMT
Hi Brian,

Based on my understanding, you'd like a better way to display multiline
data with conventional control such as TextBox and ComboBox. If I am off
base, please feel free to let me know.

The behavior of displaying multiline data within a control is called
complex data binding. TextBox can only be used for simple data binding,
i.e. bind a property of a TextBox control, usually its Text property to a
field of the data source.

As for ComboBox, although it supports complex data binding, the common
usage is to set the DataSource property of the ComboBox to a lookup data
source(set the DisplayMember and ValueMember properties in the meanwhile)
and then bind its SelectedValue property to a field of another data source.

I'm sorry to say that I don't think it's a good idea to display multiline
data with TextBox and ComboBox. As for your workaround, I think there's a
problem in it. Suppose that there're 1000 rows in the data source, then we
should create 1000 sets of controls 1000 myIList<> objects and 1000
BindingSource objects to display all the data, shouldn't we?

DataGridView is very good for complex data binding, and is easy to extend.
Could you tell me why you don't like to use DataGridView?

Alternatively, ListBox is a good candidate for complex data binding . Set
the DataSource property of the ListBox to the data source and then set its
ValueMember and DisplayMember properties properly.

Hope this helps.
If you have any concerns, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
Brian Pelton - 23 Jan 2007 16:16 GMT
> Based on my understanding, you'd like a better way to display multiline
> data with conventional control such as TextBox and ComboBox. If I am off
> base, please feel free to let me know.

Sort of...

> I'm sorry to say that I don't think it's a good idea to display multiline
> data with TextBox and ComboBox. As for your workaround, I think there's a
> problem in it. Suppose that there're 1000 rows in the data source, then we
> should create 1000 sets of controls 1000 myIList<> objects and 1000
> BindingSource objects to display all the data, shouldn't we?

There will only be one IList<> object for the form.  The IList<> will
contain say 1000 objects.  But I wouldn't create 1000 BindingSource objects.

I would still create 3 (or some other fixed number) of sets of controls,
and 3 BindingSource objects.  If the IList<> has more than 3 objects,
then the user would need to scroll through the IList<> using a scroll
bar or arrow buttons.  The scroll bar could trigger the BindingSource's
MoveNext() or MovePrevious() methods.

> DataGridView is very good for complex data binding, and is easy to extend.
> Could you tell me why you don't like to use DataGridView?

I use the DataGridView for "quick" data binding.  But I am building data
input forms for things like orders and invoices.  I want the look and
feel of text boxes and combo boxes for my detail lines.  I don't like
the appearance of the Grid.  I want to provide a logical layout for my
controls rather than the strict excel columns and rows of the DataGrid.
Linda Liu [MSFT] - 25 Jan 2007 11:08 GMT
Hi Brian,

Thank you for your prompt response. I understand what you really want now.

I think your solution works but there may be two problems in it.

One problem is that you have to create 3 or some other fixed number sets of
controls on your form, and I don't think it necessary.

Generally speaking, if we need to use the same set of controls in our
project for several times,  we create a UserControl for the set of controls
and then use the UserControl everywhere we'd like.

The other problem is that it may be not easy to maintain the correct
position for each BindingSource object.

I suggest that you create only 1 BindingSource object. You may pass the
current object in the BindingSource object to one UserControl instance on
the form and call the MoveNext method and then pass the current object to
the next UserControl instance. You could add a public property in the
UserControl to accept the current object in the data source. After the
public property is set, you may display the values of the passed object in
the UserControl immediately.

Hope this helps.
If you have anything unclear, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support
Brian Pelton - 25 Jan 2007 16:39 GMT
> Generally speaking, if we need to use the same set of controls in our
> project for several times,  we create a UserControl for the set of controls
> and then use the UserControl everywhere we'd like.

That's a good idea.  It would make it easier to modify the form later if
I need to go from 3 to 5 sets.

> The other problem is that it may be not easy to maintain the correct
> position for each BindingSource object.

Yes, I was thinking that too.  I would almost need some kind of
BindingSource manager to keep all of the BindingSource objects in sync.

> I suggest that you create only 1 BindingSource object. You may pass the
> current object in the BindingSource object to one UserControl instance on
[quoted text clipped - 3 lines]
> public property is set, you may display the values of the passed object in
> the UserControl immediately.

Thanks for the ideas.  I think I see how to do this now.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.