none
make group rows in datagrid dynamically

    Question

  • Hi,

    I wanna make group rows in datagrid , but i wanna make it dynamically through c# code not Xaml one,

    please Help ASAP

    Thursday, April 12, 2012 1:43 AM

Answers

All replies

  • Hi Pouyas,

       I think you want to add multiple columns into a datagrid through code.   I am having a list of items, Vals, which has 2 properties n1,n2 .  Se my code below .  I am binding that list to the datagrid through code, only after setting the AutoGenerateColumns  property to false.

      public class Vals
        {
           
            public int n1 {get;set;}
            public int n2 { get; set; }
            public Vals()
            { }
        }

     

     List<Vals> mylist1 = new List<Vals>();
                
                for (int i = 0; i < 10; i++)
                {
                    Vals v1 = new Vals();
                    v1.n1 = i * 3;
                    v1.n2 = i * 5;
                    mylist1.Add(v1);
                }
                dataGrid1.AutoGenerateColumns = false;
                dataGrid1.ItemsSource = mylist1;
             
                DataGridTextColumn dgc = new DataGridTextColumn();
                dgc.Header = "Column1";
                dgc.Binding = new Binding("n1");
                dataGrid1.Columns.Add(dgc);
                
                DataGridTextColumn dgc1 = new DataGridTextColumn();
                dgc.Header = "Column2";
                dgc.Binding = new Binding("n2");
                dataGrid1.Columns.Add(dgc1);
             

    Hope It Helps Laughing

    Thursday, April 12, 2012 4:20 AM
  • I wrote your code but i get exception and when i trace it i go to:

     private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
            {
                // If the app is running outside of the debugger then report the exception using
                // the browser's exception mechanism. On IE this will display it a yellow alert
                // icon in the status bar and Firefox will display a script error.
                if (!System.Diagnostics.Debugger.IsAttached)
                {

                    // NOTE: This will allow the application to continue running after an exception has been thrown
                    // but not handled.
                    // For production applications this error handling should be replaced with something that will
                    // report the error to the website and stop the application.
                    e.Handled = true;
                    Deployment.Current.Dispatcher.BeginInvoke(delegate { ReportErrorToDOM(e); });
                }
            }

    Friday, April 13, 2012 12:56 AM
  • Hi Pouyas,

      Just change the  code  to dgc1  instead of dcg here     (Only for the second datagridtextcolumn )

             Change                    
             

                DataGridTextColumn dgc1 = new DataGridTextColumn(); 
                dgc
    .Header = "Column2"; 
                dgc
    .Binding = new Binding("n2"); 
                dataGrid1
    .Columns.Add(dgc1); 
         

         TO

             DataGridTextColumn dgc1 = new DataGridTextColumn(); 
                dgc1
    .Header = "Column2"; 
                dgc1
    .Binding = new Binding("n2"); 
                dataGrid1
    .Columns.Add(dgc1); 

      Hope it Helps  Laughing

    Friday, April 13, 2012 1:49 AM
  • It works but actually i want to have sub headers lfor example i have address header and in sub headers i have : city,street,postal code

    Sunday, April 15, 2012 2:04 AM
  • Hi Pouyas,

    Please check if below grouping DataGrid is what you want.

    To change the group item in code, you can set PropertyGroupDescription of PagedCollectionView in code like below

    PagedCollectionViewName.GroupDescription.Clear();

    PagedCollectionViewName.GroupDescriptions.Add(new PropertyGroupDescription("Gender"));

    http://timheuer.com/blog/archive/2009/04/08/grouping-in-silverlight-datagrid.aspx

     

    Best Regards,

    Sunday, April 15, 2012 10:29 PM
  • Hi Shi,

    thank you for your answer, but it can't help me. Actually i want to design two rows of column headers, first row is super headers, like what i said before (Address) and in second row i want Street, postal code below the Address cell

    Monday, April 16, 2012 12:51 AM
  • Hi Pouyas,

    Please have a look at below article. Hope it helps

    http://www.codeproject.com/Articles/31779/Silverlight-DataGrid-with-Dynamic-Multiple-Row-Col

     

    Best Regards,

    Monday, April 16, 2012 5:07 AM