locked
adding button dynamically to a grid RRS feed

  • Question

  • How can I dynamically add a button to a grid and specify the row and column position. Right now when I use the following code, it always adds the button to the first row and column.

                Button btn2 = new Button();
                btn2.Content = "Welcome 600";
                SecondGrid.Children.Add(btn2);

    In XAML , we do have the option to specify like : <Button Grid.Column="2">Welcome</Button>. How can do the same at runtime.

    Thanks
    Vishal
    Thursday, May 28, 2009 10:13 AM

Answers

  • How can I dynamically add a button to a grid and specify the row and column position. Right now when I use the following code, it always adds the button to the first row and column.

                Button btn2 = new Button();
                btn2.Content = "Welcome 600";
                SecondGrid.Children.Add(btn2);

    In XAML , we do have the option to specify like : <Button Grid.Column="2">Welcome</Button>. How can do the same at runtime.

    Thanks
    Vishal

    Hi Vishal,

    You are missing the following two statements ....

    Grid

     

    .SetColumn(btn2, 2);

     

    Grid.SetRow(btn2,5);

    the second parameter is the respective column or row

    after this you can add it to the grid like you have already done.



    Cheers,
    Tarun


    Developer
    • Proposed as answer by Tarun Jindal Thursday, May 28, 2009 10:23 AM
    • Edited by Tarun Jindal Thursday, May 28, 2009 10:24 AM formatting
    • Marked as answer by Vishal Kalia Thursday, May 28, 2009 10:29 AM
    Thursday, May 28, 2009 10:23 AM

All replies

  • How can I dynamically add a button to a grid and specify the row and column position. Right now when I use the following code, it always adds the button to the first row and column.

                Button btn2 = new Button();
                btn2.Content = "Welcome 600";
                SecondGrid.Children.Add(btn2);

    In XAML , we do have the option to specify like : <Button Grid.Column="2">Welcome</Button>. How can do the same at runtime.

    Thanks
    Vishal

    Hi Vishal,

    You are missing the following two statements ....

    Grid

     

    .SetColumn(btn2, 2);

     

    Grid.SetRow(btn2,5);

    the second parameter is the respective column or row

    after this you can add it to the grid like you have already done.



    Cheers,
    Tarun


    Developer
    • Proposed as answer by Tarun Jindal Thursday, May 28, 2009 10:23 AM
    • Edited by Tarun Jindal Thursday, May 28, 2009 10:24 AM formatting
    • Marked as answer by Vishal Kalia Thursday, May 28, 2009 10:29 AM
    Thursday, May 28, 2009 10:23 AM
  • Thanks a lot Tarun for the prompt reply
    Thursday, May 28, 2009 10:30 AM