0

I am attaching a DataTable to a DataGrid. My datatable has three columns. The first and last are populated and the middle one is blank. However, when I attach the datatable to my datagrid, the blank column is populated with "System.Data.DataRowView" and there is a final blank column added to the end (please see attached picture). I want my middle data column to remain blank and I don't want an added blank end column. When I debug my code, I use the DataSet Visualizer to look at my datatable, and it looks just the way that I want it to. What am I doing wrong?

    DataTable visualDataTable = new DataTable();
    visualDataTable.Columns.Add("EMS", typeof(String));
    visualDataTable.Columns.Add("   ", typeof(String));
    visualDataTable.Columns.Add("Algo", typeof(String));

    <DataGrid x:Name="Sfs1clf6_Table" IsReadOnly="True" Grid.Row="0" Margin="0,22,0,0" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="EMS" Binding="{Binding EMS}" Width="50" FontSize="14"/>
            <DataGridTextColumn Header="" Binding="{Binding}" Width="50" FontSize="14"/>
            <DataGridTextColumn Header="Algo" Binding="{Binding Algo}" Width="50" FontSize="14"/>
        </DataGrid.Columns>
    </DataGrid>

enter image description here

Sean
  • 336
  • 1
  • 3
  • 15
  • How are you assigning the `ItemsSource` of your DataGrid, and what does the data model look like? It looks like you are using `AutoGenerateColumns`, and populating the grid with a collection of objects which contain two string properties (EMS and Algo) and a DataRowView property (Not sure why no header here..). In regards to the extra column, that's just empty left over space, as described [here](http://stackoverflow.com/q/8494333/302677). – Rachel Mar 31 '16 at 16:05
  • I just assign the DataTable to the DataGrid like so: – Sean Mar 31 '16 at 16:09
  • dataGrid.ItemsSource = visualDataTable.DefaultView; – Sean Mar 31 '16 at 16:09
  • Is `visualDataTable` just a standard .Net data table? Because if so, you should not get the image you have in your question... AutoGenerateColumns will automatically generate a column for each property in the DataTable, and assign the caption to whatever the column name is, however your column2 has no caption and is of type DataRowView, which is pretty uncommon unless your `visualDataTable` isn't created correctly. – Rachel Mar 31 '16 at 16:16
  • But regardless, you'll probably want to turn off AutoGenerateColumns and manually define them if you want to remove the extra space left over, and in doing that you might solve your other issue too. The alternative to that though is to subscribe to the AutoGeneratingColumn event, and manually set each column width to "*" to fill remaining space : `e.Column.Width = new DataGridLength(1, DataGridLengthUnitType.Star);` – Rachel Mar 31 '16 at 16:17
  • It is a standard .Net DataTable. I just have a column with " " as the title. Why shouldn't I be able to do that? I will try out turning off AutoGenerateColumns and see if that solves my added extra column problem. – Sean Mar 31 '16 at 16:21
  • 1
    Then that means your 2nd column is of datatype `System.Data.DataRowView`. WPF's DataGrid doesn't know how to render that data type, so uses the default which is a label containing the `.ToString()` of the object, which is usually the full class name. – Rachel Mar 31 '16 at 16:25
  • For the problem of the added column. I have tried adding the declaration of the columns, and that works beautifully. Thanks! However, Please see the added code to my question. The blank column is declared as a string. When I change the name from " " to "test" the column appears blank. – Sean Mar 31 '16 at 16:37
  • Give your second column a name so you can bind it properly. If you just write `"{Binding }"`, it binds to the DataRowView itself, which is why you are seeing the full class name like your image. Give it a proper name so you bind the column to the correct property in your DataRowView, and use the `Header` property to ensure you display a blank header if that's what you want. – Rachel Mar 31 '16 at 19:19

0 Answers0