Home > database >  how to create a datagridview dynamically in c# wpf
how to create a datagridview dynamically in c# wpf

Time:08-17

How do I dynamically create a DataGridView in C#? Could you please provide an example?

I have List of Suffix and List of Prefix The suffixes are : BA1, BA2, BA3, BA4,BA5, BA6, the préfixes are : Parameter1, parameter2, parameter3, parameter4.

I have Also List parameters

public class Parameter
{
public string Prefix{get;set;}
public string Suffix{get;set;}
public string Value {get;set;}
}

public class Mother 
{
    public string List<Parameter> parameters{get;set;} = new List<Parameter>();
}

the list of parameters contains :

"Parameter1" "BA1" "55"
"Parameter1" "BA2" "58"
"Parameter1" "BA3" "1"
"Parameter1" "BA4" "77"
"Parameter1" "BA5" "7889"
"Parameter1" "BA6" "100"
"Parameter2" "BA1" "7"
"Parameter2" "BA2" "44"
..
..
..
..

I want to display a DataGrid like

Name            BA1  BA2  BA3   BA4   BA5   BA6

Parameter1      55   58    1     77   7889    100
Parameter2      7    44 
Parameter3
Parameter4

My XAML is :

DataTemplate DataType="{x:Type local:Mother}"><DataGrid Name="Grid" AutoGenerateColumns ="True" ItemsSource="{Binding Parameters}"></DataGrid>
 </DataTemplate>

CodePudding user response:

You could build a DataTable like this

    public MainWindow()
    {
        InitializeComponent();

        var mother = new Mother();
        mother.Parameters.Add(new Parameter("Parameter1", "BA1", "55"));
        mother.Parameters.Add(new Parameter("Parameter1", "BA2", "58"));
        mother.Parameters.Add(new Parameter("Parameter1", "BA3", "1"));
        mother.Parameters.Add(new Parameter("Parameter1", "BA4", "77"));
        mother.Parameters.Add(new Parameter("Parameter1", "BA5", "7889"));
        mother.Parameters.Add(new Parameter("Parameter1", "BA6", "100"));
        mother.Parameters.Add(new Parameter("Parameter2", "BA1", "7"));
        mother.Parameters.Add(new Parameter("Parameter2", "BA2", "44"));

        DataGrid.ItemsSource = ConvertToDataTable(mother).DefaultView;
    }

    private static DataTable ConvertToDataTable(Mother mother)
    {
        var dataTable = new DataTable();
        dataTable.Columns.Add("Name");

        foreach (var column in mother.Parameters
                     .OrderBy(x => x.Suffix)
                     .GroupBy(x => x.Suffix))
        {
            dataTable.Columns.Add(column.First().Suffix);
        }

        foreach (var rowGroup in mother.Parameters
                     .OrderBy(x => x.Prefix)
                     .GroupBy(x => x.Prefix))
        {
            var dataRow = dataTable.NewRow();
            dataRow["Name"] = rowGroup.First().Prefix;

            foreach (var item in rowGroup)
            {
                dataRow[item.Suffix] = item.Value;
            }

            dataTable.Rows.Add(dataRow);
        }

        return dataTable;
    }
}

public class Parameter
{
    public Parameter(string prefix, string suffix, string value)
    {
        Prefix = prefix;
        Suffix = suffix;
        Value = value;
    }

    public string Prefix { get; set; }
    public string Suffix { get; set; }
    public string Value { get; set; }
}

public class Mother
{
    public Mother()
    {
        Parameters = new List<Parameter>();
    }
    public List<Parameter> Parameters { get; set; }
}

And the xaml:

<DataGrid x:Name="DataGrid" AutoGenerateColumns="True" CanUserAddRows="False" CanUserDeleteRows="False" />
  • Related