I'm using a Telerik for WinForms RadGridView, I set the datasource to a custom list of objects and I enable the columnreordering. It works fine on the frontend, but if I go to select a row and read a value, it returns the vaule of the initial ordering not the actual one I see on the screen.
Nearly all 3rd party grid controls (not just WinForms) don't manipulate the underlying data source when sorts, column reordering, filter etc. are done on them. These are just display only features.
You should just read the column value via the key not index so that it will never matter what order the user puts a column in.
Related
I want to display multiple columns (2) and multiple rows. Data consists of Image and label.
Can anyone suggest a way todo it or provide a sample code. I find it challenging as to know, how to bind the data with multiple rows.
I do not have any code to show as I am not able to come up with anything close to what I want to acheive.
You could consider the RepeaterView control (https://github.com/XLabs/Xamarin-Forms-Labs/wiki/RepeaterView) that allows you to bind a control template (e.g. consisting of an Image and Label) to a set of objects as bound or set to the ItemsSource property - it will then create an instance of the control template for every item in the collection, and any binding references within the control template will relate to the individual items in the collection. This is easy to include in your project in isolation.
Alternatively if your requirements are more involved I would recommend the free DevExpress grid component (https://components.xamarin.com/view/devexpress-grid).
I have set up a DataGridView in part of my application as shown in the following screenshot:
I'm attempting to databind this to a specified business object, and use it as an alternative PropertyGrid (there are a number of issues that make PropertyGrid inappropriate for my planned use).
The below view is built on-the-fly from a specified set of the business object's properties.
There are three area's I'm struggling with:
DataBinding - I want to bind this grid to a specified business object, for two-way databinding.
CellTemplate - I will need to have different cell templates on different rows, depending on the datatype of the property. TextBoxes, ComboBoxes, CheckBoxes, Date/Time Pickers.
Validation - OnCellLeave, I need to validate the value entered, and format the cell red = failed validation, blue = passed validation. Then when the user leaves the overall control, if there are blue cells, persist the object to DB, but if there are red cells, restore the object from DB. I already have this functionality implemented on other (traditional) DataGridView's, and this description is just for info / context.
I have implemented all of the above in the traditional form (properties in column headers), but am having problems getting it working with the properties in the row headers, which is what I'm really after.
If anyone has implemented something like this, or can help, it would be much appreciated. Thank you.
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 DataGridView are 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 DataRelation as follows;
DataRelation dataRelation = new DataRelation("Domain users",
dataSet.Tables["Domain"].Columns["Domain"],
dataSet.Tables["User"].Columns["Domain"]);
dataSet.Relations.Add(dataRelation);
How exactly do DataRelations work 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 DataGridView is 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 DataGridViewComboBoxColumn into 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 BindingSource to 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 the DataPropertyName and HeaderText and you've got a good starting point. Get the IndexOf() the column you want to replace, remove it and Insert() 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 like List<KeyValuePair<Enum, string>>. If that's the case you should also set the properties ValueMember and DisplayMember to 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.
I have this program in which I am trying to store a collection of values into a list and session
Example:
valueCollection = (List<Values>)Session["Value"];
I want to do the front end in a way which will show a table with each row showing a label and textbox. This would be too simple to do obviously but I want it to show 4 rows of the table by default and then the user can select "add another" this will then add another row onto the table with a label and textbox exactly similar to the 4 default. Everytime the user selects "add another" the table increments by 1.
How do you do something like that, or is there an easy way?
Here is a screenshot to explain it better:
http://img828.imageshack.us/img828/9986/idead.png
The basic idea is that you bind a control to your data and provide GUI controls to allow users to add/edit/delete records. Once the records are modified you re-bind the control to see the changes.
Take a look at the Databound controls in the .Net Framework. For a quick first pass you could use a ListView - a control which provides built-in functionality to automatically generate and mange the add/edit/delete GUI elements.
In 'real world' usage, storing big datasets isn't a great idea as more users = more sessions and each session uses server resources. You should really be getting/storing your data in a database or perhaps an XML file.
HTH
I have a winform (C# VS2008 .net 3.5) with a Tab Control. Two of the pages on the tab control have Data Grid Views bound to the same Binding Source. This works fine.
However, there is a bug with Tab Pages in that Data Grid Views held in Tab Pages always show the first column of the bound table, whether or not it is set to visible at design time.
To work around this, you just set the column.visible property to false manually. This worked fine when I only had a single DGV bound to the BindingSource. With two bound, some strange things happen. I manually set both DGV's first column to visible = false, but sometimes only one actually takes, and the column still shows in the other. Further, when clicking a record in the DGV that is showing the first column, the following exception is raised:
InvalidOperationException - Operation
is not valid because it results in a
reentrant call to the
SetCurrentCellAddressCore function.
Has anyone else experienced this or have a better fix for the column[0] problem with Tab Pages?