0
votes

There are some problem with fill datagrid with dynamic source. For example, I want to fill datagrid with objects that has various parameters. This parameters display dynamic data from database. Columns count may be changed and parameters count may be changed but counts equals. Fill headers:

private void DataSourseChanged(SourceList sourceList)
{
    Columns.Clear();
    Columns.Add(new DataGridTextColumn());
    if (sourceList != null)
    {
        foreach (var item in sourceList.ColumnsHeaders)
            Columns.Add(new DataGridTextColumn { Header = item });
    }
}

public class SourceList
{
    private readonly IList _columnsHeaders;
    private readonly IList _rowsHeaders;
    private readonly IList _dataRows;

    public IList ColumnsHeaders
    {
        get { return _columnsHeaders; }
    }

    public IList RowsHeaders
    {
        get { return _rowsHeaders; }
    }

    public IList DataRows
    {
        get { return _dataRows; }
    }

    public SourceList(IList rowsHeaders, IList columnsHeaders, IList dataRows)
    {
        _rowsHeaders = rowsHeaders;
        _columnsHeaders = columnsHeaders;
        _dataRows = dataRows;
    }
} 

I want to fill headers and rows (rows as merge of _rowsHeaders[i] in first column and other columns with _datarows[i]) but rows fills only with objects that has properties. May I fill datagrid with dynamic length?

1
I did`n find another solution but use dynamic stackoverflow.com/questions/15819720/…Andre
And it solution did`n help meAndre

1 Answers

0
votes

There are not solutions but create custom grid that derives from System.Windows.Controls.Grid and create custom logic for fill grid

public static readonly DependencyProperty CustomSourceProperty
            = DependencyProperty.Register(
                      "CustomSource",
                      typeof(SourceList),
                      typeof(InteractiveGrid),
                      new FrameworkPropertyMetadata(null,
                          FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
                          CustomSourceChanged
                      )
                    );

[Category("Group properties")]
public SourceList CustomSource
{
 get { return (SourceList)GetValue(CustomSourceProperty); }
 set { SetValue(CustomSourceProperty, value); }
}

private void CustomSourceChanged(SourceList sourceList)
{
//...
}

private static void CustomSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
 ((InteractiveGrid)d).CustomSourceChanged((SourceList)e.NewValue);
}