Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I want to add column manually in datagrid in WPF.

So, How to add and bind column in WPF DataGrid like simple windows application?

Please help me.

Thanks in Advance.

Ankit Agarwal
Software Engineer
Posted

This can be done either by defining a object datasource to the datagrid and manipulating the datasource value towill be reflected in data grid automatically

Refer to this article for example
binding object data source




Else if you prefer codewise binding of a collection to datagrid then the example mentioned in below link will provide you the details
binding collection
 
Share this answer
 
C#
private void AddColumn_Click(object sender, RoutedEventArgs e)
    {
      dt.Columns.Add(new DataColumn("New Column" + newColumnIndex++));
      for (int i = 0; i < dt.Rows.Count; i++)
      {
        dt.Rows[i][dt.Columns.Count - 1] = i.ToString() + " - New Column";
      }

      // Refresh the DataGrid
      dataGrid.ItemsSource = null;
      dataGrid.ItemsSource = dt.DefaultView;
    }
 
Share this answer
 
Comments
[no name] 19-Mar-15 3:23am    
No,No, I want to add Column through collection property of DataGrid in wpf, and bind also.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900