This project has moved. For the latest updates, please go here.

Dynamic Row and Button Column in Data Grid

Oct 29, 2014 at 1:12 PM
Edited Oct 29, 2014 at 1:19 PM
I want to create a datagrid with dynamic button column and row.
Each row might have maximum of 5 columns

let me tell my problem with an example
Input 7

1 2 3 4 5
6 7

if input is 2

1 2

if input is 11

1 2 3 4 5
6 7 8 9 10

data grid should be like this.
means in 1st case ,2 rows and 7 columns

in 2nd case,2 column with 1 row.

My code is for INPUT 7

for intRows as Integer 0 to 2
For IntColumn as Integer =0 to 4 --max 5 columns.
  Dim intValue as Integer
   intValue =(5*intRows  + IntColumn )+1

   Dim templatecol As New DataGridTemplateColumn()
    templatecol.Header = "Button Column"
    Dim factory As New FrameworkElementFactory(GetType(Button))
    factory.SetValue(Button.ContentProperty, intValue )
    factory.AddHandler(Button.ClickEvent, New RoutedEventHandler(Sub(s As Object, arg As              System.Windows.RoutedEventArgs)
                                                                     Dim win As New Window2()
                                                                 End Sub))
    Dim cellTemplate As New DataTemplate()
    cellTemplate.VisualTree = factory
    templatecol.CellTemplate = cellTemplate
if intValue =7 Then
GotTo FILLValue

End if

datagrid.Items.Add(New Button)

FILLValue :
     datagrid.Items.Add(New Button)

Code Image


But now my buttons are like this

How can i do that ??
Oct 30, 2014 at 8:31 PM

From what I can see you are not using the Datagrid from the Extended WPF Toolkit. So I can not really help. Usually a grid is build to display column in 1 line. So I would say the behavior looks good. Columns aren't made to be positionned in multilines.