2

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.

Harshal
  • 63
  • 1
  • 10
  • Possible duplicate of [DataGrid shows path of image instead of image itself](https://stackoverflow.com/questions/40358111/datagrid-shows-path-of-image-instead-of-image-itself) – ASh Feb 27 '18 at 09:34
  • @ASh sorry but i am adding column and row like this ` private void AddColumns(DataTable resultDataTable_, BsonDocument row) { var rowAttributes = row.GetElement("attributes").Value.AsBsonDocument; rowAttributes.Elements.ToList().ForEach(column =>resultDataTable_.Columns.Add(column.Name)); }` – Harshal Feb 27 '18 at 11:33
  • code related to datatable isn't relevant, it can add column by any method, it is fine. copy xaml part from answer, modify with your own CellTemplate and add `ChannelDataGrid_OnAutoGeneratingColumn` in code-behind – ASh Feb 27 '18 at 11:36

1 Answers1

10

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.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • I want some result from DataGrid bind result and some result using . – Harshal Mar 01 '18 at 03:07
  • Then you nedd define only the columns that you want in the XAML. Else you get all columns plus the one(s) that you define in XAML. It's one way or the other but not both. – mm8 Mar 01 '18 at 09:19