0

I have DataGrid as below

<DataGrid x:Name="Setvalue" Grid.Row="4" Grid.Column="6" AutoGenerateColumns="True">
</DataGrid>

I am binding the data to DataGrid as below

Setvalue.ItemsSource = dataTable.DefaultView;

I want to generate buttton for each row dynamically.I have tried like this

DataGridViewButtonColumn btn = new
DataGridViewButtonColumn();
btn.HeaderText = "Copy";
btn.Text = "Copy";
btn.UseColumnTextForButtonValue = true;
Imported.Columns.Add(btn);

I am getting error like "cannot convert from DataGridViewButtonColumn to DataGridColumn" . Please let me know any approach .

1
  • Create a DataGridTemplateColumn with a CellTemplate. But why are you doing this programmatically? Commented Aug 10, 2018 at 12:19

1 Answer 1

1

Create a DataGridTemplateColumn with a CellTemplate in your XAML. If you want it to be the last column, you could set the AutoGenerateColumns property to false and define all columns in the order you want them to appear:

<DataGrid x:Name="Setvalue" Grid.Row="4" Grid.Column="6" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding Name}" Header="Name" />
        <DataGridTextColumn Binding="{Binding Type}" Header="Type" />
        <DataGridTextColumn Binding="{Binding Place}" Header="Place" />
        <DataGridTemplateColumn>
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <Button Content="Copy" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>
Sign up to request clarification or add additional context in comments.

14 Comments

Thanks mm8 . How can i set column index here . like i want copy column to be appeared at the end and not at the beginning . and also how can i bind each button to respective row . Please let me know
I am not sure what you mean by "column index"? Do you want the button column to appear in the middle of the other columns? And what do you want to bind it to?
I want button column to appear at the end , l mean that should be the last column .and if i click that button , i should be able to get the content of that row only .
I am not able to attach the screenshot . i have tried your solution . The copy column is coming in the begining itself . not at the end
How many other columns are there? Can't you specify them all explicitly in the XAML markup in the order you want them to appear?
|

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.