Harshal
Harshal

Reputation: 63

Duplicate columns in DataGrid when using DataGridTemplateColumn.CellTemplate and binding to a datatable

I am facing problem in DataGrid. I need to make result clickable in datagrid. for this i need to show some result from binding result and some result as clickable using <DataGridTemplateColumn>

<DataGrid Name="Result" IsReadOnly="True" ItemsSource="{Binding Result}" AutoGenerateColumns="True" Height="200">      

 <DataGridTemplateColumn Header="Image">
      <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
          <StackPanel>
            <Button  Content="{Binding Image}" Name="Image" Click="Button_Click" />
          </StackPanel>
        </DataTemplate>
      </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>

but in the result i am getting duplicate column name, because of one from binding result and one from <datagridtemplatecolumn>. Can some one please help me to remove duplicate column name from binding result.

Upvotes: 2

Views: 3695

Answers (1)

mm8
mm8

Reputation: 169218

Can some one please help me to remove duplicate column name from binding result.

Just set the AutoGenerateColumns property to False:

<DataGrid Name="Result" IsReadOnly="True" ItemsSource="{Binding Result}" AutoGenerateColumns="False" Height="200">
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="Image">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <StackPanel>
                        <Button  Content="{Binding Image}" Name="Image" Click="Button_Click" />
                    </StackPanel>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

Then the DataGrid won't generate any columns and you will only see the column(s) that you define explicitly in your XAML markup, i.e. the "Image" column in this case.

Upvotes: 10

Related Questions