I came across Marc Gravell's elegant and witty solution to the problem posted by Matt ( title "C#/winforms: how to开发者_StackOverflow中文版 best bind a propertygrid and a System.Data.DataRow" ), and have used same solution in one of my applications.
Using some excerpts from Marc's code :
DataTable table = new DataTable();
table.Columns.Add("ID", typeof(int));
table.Columns.Add("Foo", typeof(int));
table.Columns.Add("Bar", typeof(string));
table.Columns.Add("Audit", typeof(DateTime));
table.Rows.Add(1, 14, "abc", DateTime.MinValue);
DataRow row = table.Rows.Add(2,13,"def", DateTime.MinValue);
table.Rows.Add(3, 24, "ghi", DateTime.MinValue);
RowWrapper wrapper = new RowWrapper(row);
wrapper.Exclude.Add("ID");
wrapper.Exclude.Add("Bar");
I was able to somehow make this work with a DataGridView; DataGridView uses the table as it's source, while every click on a row on DataGridView would be set as the SelectedObject for the propertyGrid1 ( using Marc's RowWrapper ), and it works perfectly. You might ask why I am doing this, meaning use both DataGridView and PropertyGrid; fact is, business requirement and narrow-minded management policies.
Anyway, I have now another dilemna; one of the columns in the table, say "Foo" for instance, ( which shows up in the PropertyGrid as one of the fields, by the way ), should be some sort of a drop-down list in the PropertyGrid. What I mean is, "Foo" should be a combobox containing a list of items derived from another DataTable. Also, is there a way to include some sort of description for each field item?
Could this be done? Marc Gravell mentioned something like " Note that you could do other things in this area to make some of the properties non-editable (IsReadOnly), or have a different caption (DisplayName), or category (Category) - by overriding other members in RowWrapperDescriptor. " as the end note to his post, and I was wondering is there is a way to do this.
Any theoretical advice is greatly appreciated. Thanks much.
Thanks for the response Marc ...
It's like this; assuming that our business requirement has the following DataTable setup :
DataTable table = new DataTable();
table.Columns.Add("ID", typeof(int));
table.Columns.Add("MedSequence", typeof(int));
table.Columns.Add("Prognosis", typeof(string));
table.Columns.Add("RBC", typeof(string));
table.Columns.Add("WBC", typeof(string));
table.Columns.Add("BP Normal", typeof(bool);
table.Columns.Add("Histology", typeof(string));
DataRow row = table.Rows.Add(559,6845,"Progressive decline","54–62pcnt","6.1 million/uL",true,"Haematoxylin");
I set dataGridView1's datasource to "table", while on every cellcontentclick for the datagridview, propertyGrid1's SelectedObject is set to the "current" selected row in dataGridView. In a sense, the requirement was that dataGridView1 should show only 2 columns : MedSequence and Prognosis, while the rest of the columns in the DataTable table are invisible to the user.
However, propertyGrid1 should show ALL columns in the table as properties. Hence, the intended effect was for the DataGridView simply as a means for display ( of columns MedSequence and Prognosis), while the editing of the other table values is handled by propertyGrid1.
Like I said in my post, as of the moment, using your RowWrapper class, it works perfectly. However, for one of the table columns, "Histology", the propertyGrid1 should display an editable drop-down list that contains the following list : { "Haematoxylin","Eosin","Toluidine blue","Periodic acid-Schiff stain" }. Essentially, management was thinking of having this drop-down list populated by values queried from a database ( this is probably what we'll do ), but basically, my problem is getting the "Histology" field in propertyGrid1 to be become a drop-down list. Since propertyGrid1 is using a wrapper for it's fields derived from columns of a datatable, I am at a loss on what to do.
Even if I changed the DataGridView column for Histology to a drop-down, it wouldn't have worked anyway since like I said, only 2 columns of the table will be shown ( MedSequence and Prognosis ), while all others are editable only through the propertyGrid1, with the Histology being a special case since it has to appear as a drop-down value list in the propertyGrid1.
精彩评论