0

I am using WPF Application. I need to Create Button Dynamically with some properties and i add the buttons in dataGridView Rows. But Datatable is not Contains The Button DataType Then how to do this Please Someone Help me thanks in advance.

This is My Code:

DataTable NewDataTable = new DataTable();
for (int i = 0; i < Row * level; i++)
{
    Button[] buttonArray = new Button[position];
    string Col1 = "Rows";
    string Col2 = "Levels";
    for (int j = 0; j < position; j++) 
    {                    
        if (j == 1) 
        {
            Col1 = "Rows";
        }
        else if (j == 2)
        {
            Col2 = "Levels";
        }
        else 
        {
            buttonArray[j] = new Button();
            buttonArray[j].Content = "Postion " + j;
            buttonArray[j].ToolTip = "Postion " + j;

        }                    
    }
    NewDataTable.Rows.Add(Col1, Col2, buttonArray[j]);
}

Code Behind

Dgrid.ItemsSource = NewDataGrid.DefaultView();
1
  • Well, if 'Datatable is not Contains' something then create a collection of custom objects which have required data and assign it to ItemsSource. There's rarely a place for DataTable in WPF app anyways. For further info and examples check out MSDN-DataGrid. Commented Sep 1, 2014 at 10:19

2 Answers 2

3

It sounds like you haven't yet met the DataGridTemplateColumn Class. Let me introduce to you the column that can contain any controls in it... from the linked page:

Represents a DataGrid column that hosts template-specified content in its cells.

An example, again from the linked page:

<Grid>
    <Grid.Resources>
        <!--DataTemplate for Published Date column defined in Grid.Resources.  PublishDate is a property on the ItemsSource of type DateTime -->
        <DataTemplate x:Key="DateTemplate" >
            <StackPanel Width="20" Height="30">
                <Border Background="LightBlue" BorderBrush="Black" BorderThickness="1">
                    <TextBlock Text="{Binding PublishDate, StringFormat={}{0:MMM}}" FontSize="8" HorizontalAlignment="Center" />
                </Border>
                <Border Background="White" BorderBrush="Black" BorderThickness="1">
                    <TextBlock Text="{Binding PublishDate, StringFormat={}{0:yyyy}}" FontSize="8" FontWeight="Bold" HorizontalAlignment="Center" />
                </Border>
            </StackPanel>
        </DataTemplate>
        <!--DataTemplate for the Published Date column when in edit mode. -->
        <DataTemplate x:Key="EditingDateTemplate">
            <DatePicker SelectedDate="{Binding PublishDate}"  />
        </DataTemplate>
    </Grid.Resources>
    <DataGrid Name="DG1" ItemsSource="{Binding}" AutoGenerateColumns="False" >
        <DataGrid.Columns>
            <!--Custom column that shows the published date-->
            <DataGridTemplateColumn Header="Publish Date" CellTemplate="{StaticResource DateTemplate}" CellEditingTemplate="{StaticResource EditingDateTemplate}" />
        </DataGrid.Columns>
    </DataGrid>
</Grid>
Sign up to request clarification or add additional context in comments.

Comments

1

I created the following working example. Below is the code

<DataGrid x:Name="dtGrid" AutoGenerateColumns="False" IsReadOnly="True">
  <DataGrid.Columns>
    <DataGridTextColumn Header="Col1" Binding="{Binding Col1}" />
    <DataGridTextColumn Header="Col2" Binding="{Binding Col2}" />
    <DataGridTemplateColumn Header="ButtonsList">
      <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
          <Button Content="{Binding ButtonsList.Content}" ToolTip="{Binding ButtonsList.ToolTip}"/>
        </DataTemplate>
      </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
  </DataGrid.Columns>
</DataGrid>

That is the Xaml part of the datagrid.

The Code behind file

public MainWindow()
    {
        InitializeComponent();

        DataTable dt = new DataTable();
        dt.Columns.Add(new DataColumn("Col1", Type.GetType("System.String")));
        dt.Columns.Add(new DataColumn("Col2", Type.GetType("System.String")));
        dt.Columns.Add(new DataColumn("ButtonsList", Type.GetType("WpfApplication.ButtonsList")));
        dt.Rows.Add("Test1", "Test1", new ButtonsList { Content = "Test1", ToolTip = "Test1" });
        dt.Rows.Add("Test2", "Test2", new ButtonsList { Content = "Test2", ToolTip = "Test2" });
        dtGrid.ItemsSource = dt.DefaultView;

    }

As you can see that I created a new class for your button. You will run into problems if you directly add buttons to DataGrid. The extra class is very simple

public class ButtonsList
{
    public String Content { get; set; }
    public string ToolTip { get; set; }
}

Please check if it works.

You have to take care of property changes since I have not done any implementation

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.