Winforms. ObjectDataSource add calculated column - winforms

I use ObjectDataSource and bind IEnumerable[Person] to my DevExpress GridView
class Person
{
private int Id {get; set;}
public string Name {get; set;}
}
At grid there are two columns Id,Name. I want new column "Sum", which is calculated by person object(aggragate by person). At asp.net I create new column and calculate its value at RowDataBound event. At winforms I don't see this event and other appr. So what should I do in this case. Where can I handle binding moment? Thanks

You should make an unbound column in your GridView, then calculate its value using the CustomUnboundColumnData event.

Related

Bind datagrid to empty itemsource?

hey iam doin silvelright 4 app.
I have datagrid with 3 coloums say Name,Age,EmpId,
The datagrid is not binded to itemsource.(Will have no rows intially)
The user can add new rows and finally save the grid on a button click.
At the moment of button click i need to get all data from the datatgrid and pass to WCF..
Is it possible to assign datagrid empty itemsource?if so how?
how do i store these data from datagrid.. collections or datatable??
Could someone help me out ..i am new to this..
please provide example if possible.
(sorry for my bad english)
You have to bind your datagrid to the collection using ItemsSource. Trust me, it will be easier.
Create a class like this:
public class Person
{
public string Name {get;set;}
public int Age {get;set;}
public int EmpId {get;set;}
}
Then, create a ObservableCollection<Person> and bind it to your dataGrid ItemsSource. On a button click, add the code:
myObservableCollection.Add(new Person() { });
this will add a empty row on your grid so you can fill it with some data.
This way you will have a observableCollection with all your data displayed on a dataGrid. You could process/serialize it and send to the server. If you have a specific question about that I suggest you create another question. RIA Services could do this kind of thing easily.

Binding combobox in WPF datagrid with parameters

I have a DataGrid with a combobox inside a template column. Elsewhere on this screen, the user makes a 'customer' selection from a separate control altogether. In order to populate the comboboxes in my datagrid, I need to pass in that selected customer as a parameter, in addition to other information from each row in the grid.
Basically... the grid contains part information, and the combobox items are based on a combination of the following: selected customer, part number, and manufacturer. Each row's combobox can potentially have a different source list. Is there a way I can bind the ItemsSource for that combobox in XAML?
I may not understand correctly, but you could possibly have an object that contains all that information together, and bind that to the combo box.
ie
public class ContextualInfo
{
public Customer Customer { get; set; }
public int PartNumber { get; set; }
public Manufacturer Manufacturer { get; set; }
}
In reply to comment.
How about having the rows returned from the query to also be in the ContextualInfo mentioned above? You can then bind the itemsource to that. You could potentially run the query in the constructor for the ContextualInfo class.

WPF - DataGrid Custom Binding - List of Custom Objects with dynamic number of properties - C#

Using Linq pad I created a view on data in the database which I now hope to replicate in a WPF Application.
I took advantage of the Linq Dump() method. By implementing ICustomMemberProvider I could provide the column headers, types and values which I wanted to be output. The three methods I needed to implement were;
public IEnumerable<string> GetNames()
public IEnumerable<Type> GetTypes()
public IEnumerable<object> GetValues()
This was a simple, quick and clean way of describing what the Dump()' for single or multiple rows should be.
For the life of me I cannot find anything as straight forward in WPF. I have a dynamic (per run not per row) number of Columns and so I can't hard code Column titles and binding paths, there may be 5 columns and there may be 20.
I was pointed towards ICustomTypeDescriptor but I need a concrete example of how that would work as there are so many methods in that interface.
I'm really hoping there's something simpler that I've missed which will allow me to implement dynamically what the rows and columns should contain given an IEnumerable of my custom class.
Any links to a tutorial or overview of how this is meant to work would be greatly appreciated. I have been surprised by the lack of documentation I've found so I must be using the wrong terms.
For clarity the source of a single row is an instance of a class like this;
public class CustomDatum
{
public string ID {get; private set;}
public string Location {get; private set;}
public IEnumerable<Attributes> attributes {get; private set;}
public class Attribute
{
public string Name {get; private set;}
public string Value {get; private set;}
public override ToString()
{
....
}
}
}
I want to display the ID, Location and all attributes in a single Row, I have an IEnumerable<CustomDatum> to bind to. The actual class is a lot more complex than this example naturally.
Thanks!
I'm pretty sure you can use a DataGridView and set its AutoGenerateColumns to true.
example of ICustomTypeDescriptor

Bind a WPF data grid to multiple data sources

I have created a data grid in WPF and have 2 lists. I want to bind one column to one list and rest of the columns to another list.
Can anyone please tell me how to do this?
Thanks
Basically, You cant. The datagrid is an ItemsControl which has one ItemsSource property.
What i would do is build a view model which is a composite object that contains one of each of the items (from the two lists). then you could bind to a collection of these.
public class CompositeItem{
public Object ItemFromListOne { get; set; }
public Object ItemFromListTwo { get; set; }
}

WPF Datagrid binding to DataTable with complex type

I have a class that contains data from some model. This class has metadata along with the actual value.
class ServerValue {
public int SomeId {get;}
public int SomeOtherId {get;}
public DateTime LastChanged {get;}
public object Value {get;set;}
// this lets me show the value, but how do i update it from the grid?
public override string ToString(){
return Value.ToString();
}
}
Now I also have a class MyDataTable that derives from DataTable that has all kind of logic. It calls the server, gets a bunch of ServerValues and puts them into Rows and Columns.
Finally I have a WPF DataGrid that I bind to the MyDataTable and the data are displayed, because the DataGrid calls ToString on each ServerValue and gets back the value for display. Hurray so far.
Now, I want to have two way databinding, so input on the grid is written back to the ServerValue. So I want to bind the grid cells to the Value property of the ServerValue instead of the ServerValue itself.
Right now the ServerValue of the DataGrid cell is just replaced with a string. I could work around this and all but I'd to try the elegant route first.
So I have a datatable with a complex type in cells and i want two-way databinding to a specific property of that type.
Is this possible? I've been googling on this and i can't anything on this.
Thanks in advance,
John
What you want is a way to convert back and forth from your object to their text reprenstations.
Define a Converter for your Binding
http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter.aspx

Resources