Home Forums WPF controls Xceed DataGrid for WPF Inserting Rows in Code

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #21996 |

    Hello,

    I have created a DataGrid created columns for this grid all in code. I’m now ready to add rows to the grid. The grid is unbound (i.e. I have not set the ItemsSource).

    How do I initialize the grid with new data?

    Thanks,
    Darrel

    Imported from legacy forums. Posted by Darrel (had 4023 views)

    Xceed Support
    Member
    Post count: 5658

    Hi Darrel,

    To provide unbound data, it is necessary to first create the required columns specifying values for their <b>FieldName</b> and <b>DisplayMemberBinding</b> properties. The DisplayMemberBinding property must be bound to the appropriate property in the data item and must also use TwoWay binding. In the case where the value assigned to the FieldName property is identical to the DisplayMemberBinding, then the DisplayMemberBinding property can be omitted.

    If an insertion row is to be used, then cell editors must also be specified by setting the columns’ CellEditor properties, and the grid’s AddingNewDataItem event handled to return a new data item; otherwise, it will not be possible to add new data items through an insertion row.

    If you just want to add items programmatically without using an insertion row, you can simply add them to the DataGridControl.Items.Add method.

    Here is an example:

    <code>
    <Grid xmlns:xcdg=”http://schemas.xceed.com/wpf/xaml/datagrid”&gt;
    <xcdg:DataGridControl x:Name=”UnboundGrid”
    AutoCreateColumns=”False”
    AddingNewDataItem=”AddingDataItem”>
    <xcdg:DataGridControl.Columns>
    <xcdg:Column FieldName=”FirstName”
    Title=”First Name”
    DisplayMemberBinding=”{Binding Path=[0], Mode=TwoWay}”
    CellEditor=”{x:Static xcdg:CellEditor.TextBoxEditor}”/>

    <xcdg:Column FieldName=”LastName”
    Title=”Last Name”
    DisplayMemberBinding=”{Binding Path=[1], Mode=TwoWay}”
    CellEditor=”{x:Static xcdg:CellEditor.TextBoxEditor}”/>

    <xcdg:Column FieldName=”Occupation”
    Title=”Occupation”
    DisplayMemberBinding=”{Binding Path=[2], Mode=TwoWay}”
    CellEditor=”{x:Static xcdg:CellEditor.TextBoxEditor}”/>
    </xcdg:DataGridControl.Columns>

    <xcdg:DataGridControl.Items>
    <x:Array Type=”{x:Type s:String}”>
    <s:String>Pascal</s:String><s:String>Bourque</s:String><s:String>Developer</s:String>
    </x:Array>
    <x:Array Type=”{x:Type s:String}”>
    <s:String>Marc</s:String><s:String>Laroche</s:String><s:String>Developer</s:String>
    </x:Array>
    <x:Array Type=”{x:Type s:String}”>
    <s:String>Michel</s:String><s:String>Fortin</s:String><s:String>Developer</s:String>
    </x:Array>
    <x:Array Type=”{x:Type s:String}”>
    <s:String>Francois</s:String><s:String>Carignan</s:String><s:String>Developer</s:String>
    </x:Array>
    <x:Array Type=”{x:Type s:String}”>
    <s:String>Jenny</s:String><s:String>Beland</s:String><s:String>Technical Writer</s:String>
    </x:Array>
    </xcdg:DataGridControl.Items>
    <xcdg:DataGridControl.View>
    <xcdg:TableView>
    <xcdg:TableView.FixedHeaders>
    <DataTemplate>
    <xcdg:InsertionRow/>
    </DataTemplate>
    </xcdg:TableView.FixedHeaders>
    </xcdg:TableView>
    </xcdg:DataGridControl.View>
    </xcdg:DataGridControl>
    </Grid>

    // The handler for the AddingNewDataItem event.
    private void AddingDataItem( object sender, Xceed.Wpf.DataGrid.AddingNewDataItemEventArgs e )
    {
    e.DataItem = new String[] { “”, “”, “”};
    }
    </code>

    Imported from legacy forums. Posted by Jenny [Xceed] (had 521 views)

    User (Old forums)
    Member
    Post count: 23064

    Hi Jenny,

    Thanks for the response.

    I am creating the grid 100% in C# at runtime. The code is as follows:

    DataGridControl CreateMyGrid()
    {
    // create the grid
    DataGridControl dataGridControl= new DataGridControl();
    dataGridControl.AutoCreateColumns = false;
    dataGridControl.View = new Xceed.Wpf.DataGrid.Views.TableView();
    dataGridControl.View.FixedHeaders.Add(new DataTemplate(typeof(InsertionRow));

    // create two columns
    Column firstColumn = new Column();
    firstColumn .Title = “First”;
    firstColumn .FieldName = “First”;
    firstColumn .DisplayMemberBinding = new System.Windows.Data.Binding(“First”);
    (firstColumn .DisplayMemberBinding as System.Windows.Data.Binding).Mode = System.Windows.Data.BindingMode.TwoWay;
    (firstColumn .DisplayMemberBinding as System.Windows.Data.Binding).Path = new PropertyPath(“0”);
    dataGridControl.Columns.Add(firstColumn );

    Column secondColumn= new Column();
    secondColumn.Title = “Second”;
    secondColumn.FieldName = “Second”;
    secondColumn.DisplayMemberBinding = new System.Windows.Data.Binding(“Second”);
    (secondColumn.DisplayMemberBinding as System.Windows.Data.Binding).Mode = System.Windows.Data.BindingMode.TwoWay;
    (secondColumn.DisplayMemberBinding as System.Windows.Data.Binding).Path = new PropertyPath(“1”);
    dataGridControl.Columns.Add(secondColumn);

    // fill the grid with two rows
    String[] rowData1 = new String[2];
    rowData1[0] = “A”;
    rowData1[1] = “B”;

    String[] rowData2 = new String[2];
    rowData2[0] = “A”;
    rowData2[1] = “B”;

    dataGridControl.Items.Add(rowData1);
    dataGridControl.Items.Add(rowData2);

    return myDataGrid;
    }

    Problem 1:
    When the Window.ShowDialog is called, an InvalidCastException is thrown with an inner exception message of, “Unable to cast object of type ‘System.String[]’ to type ‘Xceed.Wpf.DataGrid.Row'”.

    Problem 2:
    When I try comment out the addition of the Items, the window opens without an insertion row. How do I enable the insertion row?

    Any thoughts?

    Thanks,
    Darrel

    Imported from legacy forums. Posted by Darrel (had 541 views)

    User (Old forums)
    Member
    Post count: 23064

    Hi Darrel,

    <u>Problem 1</u>

    I cannot reproduce the problem as you describe it with the code you posted (I don’t get the InvalidCastException). However, I noticed some things that need to be modified in your code:

    When you initialize the DisplayMemberBinding for each column, you should pass “[0]” and “[1]” for the Property Paths. This will make the Binding access the Data Item by its indexer.

    As a side note: the string passed to the constructor of a Binding is the PropertyPath, so you can just provide the paths right in the constructors.

    Here’s the modified code to make this work:
    <hr>
    <code>
    Column firstColumn = new Column();
    firstColumn .Title = “First”;
    firstColumn .FieldName = “First”;
    firstColumn .DisplayMemberBinding = new System.Windows.Data.Binding(“[0]”);
    (firstColumn .DisplayMemberBinding as System.Windows.Data.Binding).Mode = System.Windows.Data.BindingMode.TwoWay;
    dataGridControl.Columns.Add(firstColumn );

    Column secondColumn= new Column();
    secondColumn.Title = “Second”;
    secondColumn.FieldName = “Second”;
    secondColumn.DisplayMemberBinding = new System.Windows.Data.Binding(“[1]”);
    (secondColumn.DisplayMemberBinding as System.Windows.Data.Binding).Mode = System.Windows.Data.BindingMode.TwoWay;
    dataGridControl.Columns.Add(secondColumn);
    </code>
    <hr>

    <u>Problem 2</u>

    To add an InsertionRow to the FixedHeaders of the grid, you need to create a DataTemplate that <i>contains</i> an InsertionRow:
    <hr>
    <code>
    DataTemplate insertionRowTemplate = new DataTemplate();
    insertionRowTemplate.VisualTree = new FrameworkElementFactory( typeof( InsertionRow ) );
    dataGridControl.View.FixedHeaders.Add( insertionRowTemplate );
    </code>
    <hr>

    Hope this helps!

    Imported from legacy forums. Posted by Pascal (had 356 views)

    User (Old forums)
    Member
    Post count: 23064

    Excellent! Thanks for your help.

    Darrel

    Imported from legacy forums. Posted by Darrel (had 5331 views)

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.