I’m quite new to C# (coming from a Java background) and I’m working on a Form to quickly manage different data (e.g. Users, Domains, Computers) along with providing utilities that use the data.
To avoid confusion the following is a summary of the example data structure;
<User>
<Name>joe</Name>
<Domain>europa</Domain>
</User>
<Domain>
<Name>europa<Name>
<Domain>
<Domain>
<Name>group</Name>
</Domain>
<Computer>
<Name>localhost</Name>
</Computer>
In my Form I have tab pages for each data, each containing a DataGridView (containing a column for each member) to allow for simple management. I’m using a single DataSet which reads the data from an XML file (using a schema) when the Form Load event is fired. After reading the data I am setting the tables as the DataSource of their respective DataGridView using the code;
userDataGridView.DataSource = dataSet.Tables["User"]; domainDataGridView.DataSource = dataSet.Tables["Domain"]; computerDataGridView.DataSource = dataSet.Tables["Computer"];
This is working properly for the Domain and Computer DataGridViews as they only have single text columns which are mapped to the Name properties. However, the Domain column of for the User DataGridView is a combo box and I need that to contain all the possible Domains (e.g. europa, group) as well as the selected Domain being bound to User.Domain (also, the User.Domain being initially selected).
My main question is how I achieve the above but I also have some additional questions hopefully someone can answer;
- I’m assuming that changes made to a
DataGridVieware immediately persisted in the underlying XML file as I am reading it from the build’s output directory. Is this correct or is additional configuration/process required? - During my attempts at getting this to work I tried to use a
DataRelationas follows;DataRelation dataRelation = new DataRelation("Domain users", dataSet.Tables["Domain"].Columns["Domain"], dataSet.Tables["User"].Columns["Domain"]); dataSet.Relations.Add(dataRelation);How exactly do
DataRelationswork and what effect do/can they have?
In case it helps you understand/explain I am using the SharpDevelop IDE for working on this application.
Thanks in advance.
As you have already found out, the
DataGridViewis very powerful and does most of the work in an automagically way. Unfortunately you run into problems if these defaults doesn’t match your preferences (like using a ComboBox for a text property).To get the
DataGridViewComboBoxColumninto the proper place you can do this programmatically within your code or (if possible) do it with the designer (in Visual Studio don’t know if SharpDevelop supports it).Using the (Visual Studio) Designer
For this scenario it is necessary that the structure of the data is known at design time by providing a class holding all (or more) informations as simple properties (like a person class with properties name, age, etc.). If this is available you can add a
BindingSourceto your control and click in the properties window at the button next to the DataSource property. Select Add Project Data Source and select Object and select your desired object.Now you have a binding source configured with a specific type of DataSource. Next select your DataGridView and change the DataSource property to the recently created binding source.
After this the DataGridView will automatically be populated with a column for each property. Now you can easily step into the Columns property and change the behaviour and type of each column as you like.
To connect your concrete data with the DataGridView just apply your table to the binding source.
Doing the same at runtime
If you have Visual Studio and followed the above steps you can afterwards easily take a look into the Designer.cs file to find how Visual Studio did all the stuff. There is nothing you can’t do also manually. So if you need to just do it.
Simply create a
DataGridViewComboBoxColumn, set theDataPropertyNameandHeaderTextand you’ve got a good starting point. Get theIndexOf()the column you want to replace, remove it andInsert()your freshly created column at the position you want.Before the Grid can show some data (in the ComboBoxColumn) you need to provide a list with the possible values. For this purpose the comboBoxColumn has a DataSource property on itself where you can provide the desired values. With the simplest scenario you just provide an
ICollection<string>, but you can also give something more complex likeList<KeyValuePair<Enum, string>>. If that’s the case you should also set the propertiesValueMemberandDisplayMemberto tell the ComboBox which properties of the objects should be used to populate the list.Maybe with these informations given you should take a look at the MSDN article about the DataGridViewComboBoxColumn and study the example. This should give you some additional hints on how to set it up properly.