Sandy
Sandy

Reputation: 11687

Datagrid column header values are missing

Beginner question. I have following XAML in my WPF form.

    <DataGrid x:Name="GridTable" 
              ItemsSource="{Binding GridDataSource}" 
              HorizontalGridLinesBrush="#FFE2E2E2" 
              VerticalGridLinesBrush="#FFE2E2E2" 
              CanUserAddRows="False" 
              CanUserResizeColumns="True" 
              Margin="10,140,214,10" 
              SelectionChanged="GridTableSelectionChanged" >

        <DataGrid.Columns>
            <DataGridCheckBoxColumn Header="{Binding Selection}" ></DataGridCheckBoxColumn>
            <DataGridTextColumn Header="{Binding XmlFile}"></DataGridTextColumn>
            <DataGridTextColumn Header="{Binding Result}"></DataGridTextColumn>
        </DataGrid.Columns>

        <DataGrid.Resources>
            <Style TargetType="DataGridCell">
                <Setter Property="BorderThickness" Value="0"/>
                <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
            </Style>
        </DataGrid.Resources>

    </DataGrid>

ViewModel Code

    public string Selection
    {
        get { return "Selection"; }
    }

    public string XmlFile
    {
        get { return "Xml File"; }
    }

    public string Result
    {
        get { return "Result"; }
    }

    private DataTable CreateDataSource()
    {
        var dt = new DataTable();

        var dc = new DataColumn(Selection, typeof(bool));
        dt.Columns.Add(dc);

        dc = new DataColumn(XmlFile, typeof(string));
        dt.Columns.Add(dc);

        dc = new DataColumn(Result, typeof(string));
        dt.Columns.Add(dc);

        return dt;
    }

I am using this datatable as source for my grid after populating data in it.

I expect when I run my application I should be able to see a datagrid with 3 columns with headers Selection, XmlFile and Result as the per the binding values. But I see is only grid with no columns. Can anyone help me to understand whats wrong in this?

Also I want to attach a datasource GridDataSource to the grid. This datatable has 3 columns with same names as of grid columns and some related data. When I load some data in datatable and notify it to view, then I see already present 3 empty columns and then more 3 columns of my datatable. Can anyone help me to understand what is wrong with my code. Let me know if you need more info.

Edit

Also I noticed, when my AutoGenerateColumns tag is set to true then I see additional 3 columns of my datatable in DataGrid along with 3 empty columns. And when AutoGenerateColumns` tag is set to false, then I don't see any data and only see 3 empty columns.

Upvotes: 2

Views: 2372

Answers (1)

dkozl
dkozl

Reputation: 33364

Currently you're binding DataGridColumn.Header which means that your GridDataSource should expose 3 properties (Selection, XmlFile and Result) to be displayed in column headers, not values. If I understand your problem and data model, I think what what you want to bind is Binding, not Header:

<DataGrid.Columns>
   <DataGridCheckBoxColumn Header="Selection" Binding="{Binding Selection}"/>
   <DataGridTextColumn Header="XmlFile" Binding="{Binding XmlFile}"/>
   <DataGridTextColumn Header="Result" Binding="{Binding Result}"/>
</DataGrid.Columns>

also DataGrid has AutoGenerateColumns option , which might be of a use here, that will create columns automatically for you based on the attached data source so you don't have to specify DataGrid.Columns manually

<DataGrid AutoGenerateColumns="True" ..../>

Upvotes: 0

Related Questions