locked
Grid Layout RRS feed

  • Question

  • Hi,

    How can we create grid dynamically..

    my requirement is to display static data, so i thought of displaying this data inside a grid layout containing textblock.. wat say?

    a code snippet or link would be of great help

    Wednesday, March 18, 2009 4:25 AM

Answers

  • Hi,

    Here is some code that will create a Grid with two rows.  Two textblocks are created and added to the two rows.

    public void CreateGrid()
            {
                Grid myGrid = new Grid();
                TextBlock textblock1 = new TextBlock() { Text = "Some text 1" };
                TextBlock textblock2 = new TextBlock() { Text = "Some text 2" };
    
                myGrid.RowDefinitions.Add(new RowDefinition());
                myGrid.RowDefinitions.Add(new RowDefinition());
    
                textblock1.SetValue(Grid.RowProperty, 0);
                textblock2.SetValue(Grid.RowProperty, 1);
    
                myGrid.Children.Add(textblock1);
                myGrid.Children.Add(textblock2);
            }

     Hope this helps!

     

    Wednesday, March 18, 2009 4:51 AM