Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding rows to DataGrid through XAML

Tags:

wpf

xaml

datagrid

Is it possible to add one or more rows to WPF DataGrid through XAML, without binding it to a collection. What I'm looking for would essentially be something like:

<DataGrid AutoGenerateColumns="False">
    <DataGrid.Columns>
    ...
    </DataGrid.Columns>

    <DataGrid.Items>
        <DataGridRow>
        ...
        </DataGridRow>
    </DataGrid.Items>
</DataGrid>

I'm going to use it at design-time to see how my DataGrid columns would look like without actually running the code.

like image 787
dotNET Avatar asked Aug 28 '13 18:08

dotNET


People also ask

Can user add rows DataGrid WPF?

WPF DataGrid (SfDataGrid) provides built-in row called AddNewRow. It allows user to add a new row to underlying collection. You can enable or disable by setting SfDataGrid.

How add rows and columns to WPF DataGrid programmatically?

To programatically add a column: DataGridTextColumn textColumn = new DataGridTextColumn(); textColumn. Header = "First Name"; textColumn. Binding = new Binding("FirstName"); dataGrid.

What is the difference between DataGrid and DataGridView?

The DataGrid control is limited to displaying data from an external data source. The DataGridView control, however, can display unbound data stored in the control, data from a bound data source, or bound and unbound data together.

What is the difference between grid and DataGrid in WPF?

A Grid is a control for laying out other controls on the form (or page). A DataGrid is a control for displaying tabular data as read from a database for example.


1 Answers

Feeling lucky. Found it myself. Here's the simplest way.

Create a dummy class with the same public properties (important that you define members as properties and not fields). For example:

public class Dummy
{
    public string Subject { get; set; }
    public string Body { get; set; }
    public DateTime DueDateStart { get; set; }       
}

Import your project namespace into XAML by adding the following import at the top:

xmlns:local="clr-namespace:YourProjectNamespace"

Now you can add items (rows) to your DataGrid at design-time like (make sure your columns have proper bindings):

<DataGrid AutoGenerateColumns="False">
    <DataGrid.Columns>
      <DataGridTextColumn Header="Subject" Binding="{Binding Path=Subject}"/>
      <DataGridTextColumn Header="Body" Binding="{Binding Path=Body}"/>
      <DataGridTextColumn Header="Due Date" Binding="{Binding Path=DueDateStart}"/>
    </DataGrid.Columns>

    <local:Dummy Subject="Subject 1" Body="Body 1" ... />
    <local:Dummy Subject="Subject 2" Body="Body 2" ... />
</DataGrid>

Hope this helps someone!

Update

Since this is now a popular post, I thought I should update this with the standard way of doing things.

WPF supports a concept known as Design-Time Data that serves this exact purpose. A few key advantages of using Design-Time Data over the approach I mentioned above include:

  1. Design-Time Data remains separate from application functionality.
  2. You do not need to change anything at all to switch your controls between design and run modes.
  3. Data lives in XML files that are easily editable.

Here are the steps to create design-time data files:

  1. Open your project in Blend (comes free with VS2015 and VS2017).
  2. Open your View (Window or Control that you're working with).
  3. From Data tool window (docked with Solution Explorer by default), choose Create Sample Data From Class.
  4. Select your VM class. You should choose the same class that your control will actually use at runtime as its DataContext.
  5. Blend will create an XML file with sample data automatically filled in for you. The file will look something like this:

    <local:TestDataList xmlns:local="clr-namespace:YourNamespaceHere" Capacity="46" ID="33" Name="Maecenas curabitur cras">
      <local:TestData ID="66" Name="Aenean vestibulum class"/>
      <local:TestData ID="34" Name="Duis adipiscing nunc praesent"/>
      <local:TestData ID="91" Name="Accumsan bibendum nam"/>
    </local:TestDataList>
    

Important to note that you do not need Blend to generate this file. You can do this by hand too.

  1. Now in your DataGrid (or whatever control you're working with), add the following property (change file path according to your project):

    d:DataContext="{d:DesignData Source=SampleData/TestDataListSampleData.xaml}"
    
  2. Given that your control has its properties properly set (e.g. ItemsSource, Columns etc.), sample data will start showing in the designer immediately.

Just for completion, note that Blend cannot generate automatic data for generic classes. For example, if your VM class contains a property of type List<string> (or if the VM class itself is a generic class), you'll not see that property getting generated in the sample data file. In that case, you must create your own dummy class inheriting from the generic class and then use it as the type of your property. For example:

public class MyListOfStrings : List<string>
{ }
like image 138
dotNET Avatar answered Nov 16 '22 00:11

dotNET