0

I created a datagrid(drag and drop) in a wpf application. I then went through the properties window to add the columns manually.

I want to now add rows to this datagrid through my code behind. I would have thought you would make a

DataGridRow row = new DataGridRow();

and then through maybe a Items.Add() or something you would add your values(one for each column).

I am not seeing this so I am wondering how do I do this.

I know I should be like databinding and stuff but I new to wpf and I am making a quick and dirty application and I rather just go with a forloop and make the rows manually.

I rather come back and refactor the area if I ever feel the desire too. I really just want what I am making up and running so I can use it asp.

1 Answer 1

1

One row is one object, the values are the properties on said object. You should not create the container (the DataGridRow) yourself, the DataGrid can do that for you. Just add the data object directly to the Items (or a collection set as ItemsSource, it should implement INotifyCollectionChanged (e.g. ObservableCollection<T>)). The columns should bind the properties on the data object, by default they are created automatically from the data.


In response to a comment: Using the DisplayNameAttribute you can get the spaces out of the headers easily but you need to add the attribute to all the offending properties:

[DisplayName("Full Name")]
public string FullName { get; }

Then subscribe to DataGrid.AutoGeneratingColumn (- oh, there's a hacky solution for this problem in the docs -):

private void DataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    var descriptor = (MemberDescriptor)e.PropertyDescriptor;
    //Takes the value from the attribute if exists & non-default-value, else property name.
    e.Column.Header = descriptor.DisplayName;
}

The hard way would be an algorithm which just splits the existing header strings correctly (would need to consider pascal casing, numbers and abbreviations, probably not so easy to get 100% accuracy).

Sign up to request clarification or add additional context in comments.

7 Comments

Can you give an example. I don't understand why I can't manually make a row. I have an object with some properties and I did dg.Items.Add(test); and loaded up the row but nothing is in there. So not sure if it can't figure out the column names and bind since they are a bit difference or what).
I am trying to also use my list collection with ItemSource but all I get empty rows in my datagrid
@chobo2: The properties need to be public as well, also you do have properties (as opposed to fields), right? If you get columns generated depends on AutoGenerateColumns (true by default). Read the documentation of the column class for examples of how to set them up manually. Read the docs for the DataGrid and anything you can find as well, it helps.
Ok that was what missing(autoGenerateColumns set to true). However how can I override the columns so I can put spaces in the column names. Also I just used a plain List but how I understood what you wrote I needed to use like ObserableCollection.
@chobo2: You only need an observable collection if you add items after assigning it as ItemsSource. Also i do not know about the column names, maybe if you add DisplayName attributes for the properties, no idea if the DataGrid supports any of that. If you don't like the auto columns make your own (see examples in DataGrid doc).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.