How to set the DataSource of a DataGrid in WPF?
Asked Answered
W

5

13

I need to set a table from a database to be the DataSource of a GridGrid in WPF. In Windows Forms the property is called DataSource but in WPF no such property exists, so how can i do it?

Whitleywhitlock answered 22/3, 2011 at 22:5 Comment(3)
You mean "in Windows Forms", not "in C#"... C# is a language, not a UI frameworkAmimia
I assumed as much as well and went ahead to edit it since C# does not make any sense.Annabelannabela
Yes i mean Windows Forms. Thanks for the correctionWhitleywhitlock
A
16

You can use the ItemsSource property :

<ListView ItemsSource="{Binding YourData}">
    <ListView.View>
        <GridView>
            <!-- The columns here -->
        </GridView>
    </ListView.View>
</ListView>

If you prefer to use code-behind rather than a binding, just give a name to the ListView and set the ItemsSource property in code:

listView1.ItemsSource = YourData;

You can also use the ItemsSource property with other list controls (DataGrid, ListBox, ComboBox, etc), since it is defined in the ItemsControl base class.


EDIT: if the data source is a DataTable, you can't assign it directly to ItemsSource because it doesn't implement IEnumerable, but you can do it through a binding:

listView1.SetBinding(ItemsControl.ItemsSourceProperty, new Binding { Source = YourData });
Amimia answered 22/3, 2011 at 22:7 Comment(5)
But "YourData" how you say is an have to be an object? Because if i do it like in Windows Forms, with a Datatable, it doesn't work.Whitleywhitlock
Ah, yes, DataTable is a special case... see the edit in my answerAmimia
But i have another problem, if i want to bind data to the DataGrid, but if i already have columns defined in the DataGrid, how can i put the binding Data in the existing columns?Whitleywhitlock
I need for example to set something as a DataSource of a DataGrid (and i already have achieved that), but imagine that i already have columns (as TextBox or/and ComboBox) in the DataGrid. I can put the Data successfully in the cells of the columns as TextBox by defining the 'Binding' Property, but to put the data in the columns as ComboBox i don't understand how i can do it.Whitleywhitlock
You can use a DataGridComboBoxColumn. I can't really give you a detailed explanation in the comments, have a look at the example in the documentationAmimia
M
11

This is simple an example:

XAML part :

<DataGrid Name="dataGrid1" Width="866" Height="auto" HorizontalAlignment="Left" VerticalAlignment="Top" />

C# part :

... [code to read and fill your table ] ...

da.Fill(myDataTable);
dataGrid1.ItemsSource = myDataTable.DefaultView;

And now your DataGrid will be filled with your DataTable

Murage answered 3/4, 2013 at 15:54 Comment(0)
A
2

The GridView is a view and not a standalone control as far as i know, you would normally use it as the view of a ListView. In WPF the property for data population is called ItemsSource, you probably want to either use a ListView or DataGrid to display your data that way.

Annabelannabela answered 22/3, 2011 at 22:9 Comment(0)
K
0

You can use below both ways to bind the datatable to datagrid in WPF.

 datagrid.ItemSource = mydt.DefaultView();

 datagrid.DataContext = mydt.DefaultView();
Kreis answered 2/11, 2017 at 4:23 Comment(0)
H
0

I have this code, its work load from csv in .xaml

<DataGrid x:Name="dataGridView1"
          ItemsSource="{Binding Source=ClusterList}"  
          AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="ClusterNumber" Binding="{Binding ClusterNumber}"/>
        <DataGridTextColumn Header="ServerName" Binding="{Binding ServerName}"/>
/>
    </DataGrid.Columns>
</DataGrid>

then I have c# code of view

public DiscoveryView()
{
    InitializeComponent();
    ClusterList = new ObservableCollection<ClusterData>();
    LoadDataFromCsv();
    dataGridView1.ItemsSource = ClusterList;}

public class ClusterData
{
    public string ClusterNumber { get; set; }
    public string ServerName { get; set; }
}

public ObservableCollection<ClusterData> ClusterList { get; set; }

private void LoadDataFromCsv()
{
    using (var reader = new StreamReader("./Configs/clusterlist.csv"))
    {
        while (!reader.EndOfStream)
        {
            var line = reader.ReadLine();
            var values = line.Split(',');
            ClusterList.Add(new ClusterData
            {
                ClusterNumber = values[0],
                ServerName = values[1]
            });
        }
    }
}

maybe this code is for beginners, but it works for me

Hammett answered 22/12, 2023 at 22:31 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.