locked
WPF Adding Columns to DataGrid Dynamically RRS feed

  • Question

  • Hi All,

    I Have a Button to Add Datagrid Dynamically ,It can add any number of Datagrid controls

    But i dont no how to add columns to those Datagrids....

    Any Help????

     

    Thanks in Advance.

     

    Monday, June 21, 2010 9:40 AM

Answers

  •  

    DataGridTextColumn txtClmn = new DataGridTextColumn();

    txtClmn.Header =

    "Test Column";

    dataGrid1.Columns.Add(txtClmn);


    Please mark the post as answer if it is helpfull to you because it boosts the members to answer more and more.
    • Marked as answer by Krishna.13579 Monday, June 21, 2010 12:00 PM
    Monday, June 21, 2010 10:16 AM

All replies

  •  

    DataGridTextColumn txtClmn = new DataGridTextColumn();

    txtClmn.Header =

    "Test Column";

    dataGrid1.Columns.Add(txtClmn);


    Please mark the post as answer if it is helpfull to you because it boosts the members to answer more and more.
    • Marked as answer by Krishna.13579 Monday, June 21, 2010 12:00 PM
    Monday, June 21, 2010 10:16 AM
  • You can use below code to add column dynamically.

                DataGridTextColumn textColumn1= new DataGridTextColumn();
                textColumn1.Header="Your header";
                textColumn1.Binding=new Binding("YourBindingField");
                dg.Columns.Add(textColumn1);


    Regards
    Nayan Paregi (MCTS)
    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Proposed as answer by FishaMeena Tuesday, June 7, 2011 5:45 AM
    Monday, June 21, 2010 10:34 AM
  • Hi, You can do like this.

    public void GenerateColumns(DataGrid yourGrid, IEnumerable<ColumnSchema> columns) 
    { 
        yourGrid
    .Columns.Clear(); 
     
       
    int index = 0; 
       
    foreach (var column in columns) 
       
    { 
            yourGrid
    .Columns.Add(new DataGridTextColumn 
           
    { 
               
    Header = column.Name, 
               
    Binding = new Binding(string.Format("[{0}]", index++)) 
           
    }); 
       
    } 
    }


    Syed Mustehsan Ikram
    Monday, June 21, 2010 10:47 AM
  • Thank U All
    Monday, June 21, 2010 1:12 PM
  • hi Krishana

      how to do that one can u give example on that

    Thanks in Advance

    Tuesday, September 13, 2011 8:08 AM