all groups > dotnet windows forms databinding > june 2005 >
You're in the

dotnet windows forms databinding

group:

Display a matrix in a datagrid?



Display a matrix in a datagrid? Bill Cohagan
6/5/2005 9:35:34 PM
dotnet windows forms databinding: I've got an n x n matrix (of integers) that I'd like to display in a grid.
The matrix is actually wrapped in a class. What's the best approach for
binding the matrix to the grid? Currently I've implemented the IList
interface on my object, exposing the list of n^2 elements as a list, but
this just results in a grid with n^2 rows and a few columns displaying the
attributes of the cells in the grid. (Yes, I said integers, but in fact the
cells are objects that contain other information as well; e.g., row, col
numbers.)

So, perhaps what I want is to redo the IList to return a list of rows, but
then how do I handle the columns as far as binding to the grid columns? Any
suggestions/guidance would be appreciated.

Thanks in advance,
Bill

RE: Display a matrix in a datagrid? v-jetan NO[at]SPAM online.microsoft.com (
6/6/2005 8:47:07 AM
Hi Bill,

Thanks for your post.

For your requirement, we have several options to get it done.

1. We can dynamically create a n-Column int type DataTable, then add the
n^2 elements in the DataTable, then we can bind the DataTable to the
DataGrid. I think this is the simplest way.

2. What you wanted is similiar with the funciton DataTable and DataSet
class provided. Actually, DataTable and DataSet both implemented
IListSource interface, if you also want to enable internal Column
databinding to certain field, your class should implement IListSource
interface. You may view IListSource in MSDN:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfSystemComponentModelIListSourceClassTopic.asp

However, this requires more work to be done. For more information, please
refer to:
"HOWTO: implement IListSource in Winforms v2"
http://noiseehc.freeweb.hu/IListSource.html

Hope this helps
===============================================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
Re: Display a matrix in a datagrid? Bill Cohagan
6/7/2005 1:33:45 PM
Jeffrey

Thanks for the response. I didn't really find the msdn documentation on
IListSource very helpful. It read like it would be helpful if I already
understood how thing worked! In any case I found a simpler way. Since in my
case there are a fixed number of cells in the matrix I ended up just using a
panel with 81 TextBox controls -- generated and placed at form load time. I
used the Observer Pattern to link up my cell objects with the corresponding
TextBox so that any change to the value of a cell would be reflected
immediately in the UI.

Interestingly I found that letting my app run full speed resulted in no
update of the UI, presumably the messages were being genereated so quickly
that each message caused the previous to be ignored. I vaguely remember
something about this message loop handling from some long ago reading about
Windows architecture. What I wanted to achieve was a visual reflection of a
game board tree search for the game of Sudoku. I finally got it to work by
inserting a call to Sleep() in my cell update code.

One problem remains however. When the form is running you can watch the
tree search progress (and regress), but if I bring another application's
window in front of the form, then switch focus back to the form I find that
the visual update has stopped. Any idea what might cause that?

Thanks,
Bill

[quoted text, click to view]

Re: Display a matrix in a datagrid? v-jetan NO[at]SPAM online.microsoft.com (
6/8/2005 12:00:00 AM
Hi Bill,

Thanks for your feedback.

First, I think there are too much details unclear in your implementation,
such as:
1. How you do the updating with the 81 textboxes from the matix data?
2. How you implement the "tree search progress", did you use
multi-threading in your App?

I suggest you provide some description of your application context, some
key code snippet will be helpful on the understanding of you scenario.

Additionally, does my first suggestion of copying the 9^2 matrix elements
into a DataTable, then bind this DataTable to the DataGrid make sense to
you? Because you have fixed number of elements, I think it is easy to
implement, then we can leverage Winform complex databinding for the 2 way
updating.

At last, for your last issue, I suggest you create a little sample project
to reproduce out the problem, then I can help you better on it. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
RE: Display a matrix in a datagrid? MichaelGeier
6/29/2005 1:30:03 AM
Hello Bill,
I have exact the same problem.
I want a 2d-Array as a DataSource for a DataGrid.
In the meantime, maybe you have solved this problem??

Michael

[quoted text, click to view]
AddThis Social Bookmark Button