0
votes

I'am working with c# and WPF, i have a DataGrid control to which i display data from a DataTable dynamically and also AutoGeneratedColumns property of DataGrid control is set to false.

Now the problem is that i want to add another column (DataGridTemplateColumn) in the DataGrid control which will be the last column.

I have used code like below to add Button column:

<DataGridTemplateColumn >
      <DataGridTemplateColumn.CellTemplate>
               <DataTemplate>
                      <Button Click="btnDetails">Details</Button>
                </DataTemplate>
       </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

By using above code, button column is added to DataGrid but as a FIRST column and then all columns of DataTable is added after that dynamically.

So i just want to add this DataGridTemplateColumn in the last of all columns. Please Help me out with this.

Thanks.

1
Please share full xaml code of DataGrid.ColumnsVMaleev
'<DataGrid.Columns> <DataGridTemplateColumn> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <Button Click="btnDetails">Details</Button> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid.Columns>'Saurav Anand
Do you add other columns programmatically?VMaleev

1 Answers

1
votes

You may use DisplayIndex property for the column but I recommend you to set AutogenerateColumns to false in the DataGrid declaration , and then add columns manually in the desired order:

<DataGrid AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding Path=Property1}" />
        <DataGridTextColumn Binding="{Binding Path=Property2}" />
        <DataGridTemplateColumn>
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <Button Click="btnDetails">Details</Button>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>            
</DataGrid>