10

How can I add data Items to a DataGrid programmatically in WPF which do not have bindings? The DataGrid has 4 columns.

3
  • 3
    DataBinding and adding items programmatically are not mutally exclusive.
    – brunnerh
    Commented Aug 14, 2012 at 10:27
  • DataGrid.Items.Add(new DataItem()); I tried this but no idea to create DataItem()
    – katu
    Commented Aug 14, 2012 at 10:27
  • Then how to bind the columns that are not from a dataset
    – katu
    Commented Aug 14, 2012 at 10:29

3 Answers 3

18

It is not very clear, what You like to do. I guess, You have defined some place where You want to put the DataGrid. For illustration purposes, I created a new WPF project and use the code provided by chridram, who posted the first answer.

In the following MainWindow.xaml I name the Grid MainGrid to access it in the code behind:

<Window x:Class="WpfExperiments.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid Name="MainGrid"/>
</Window>

The DataItem class is not a WPF class, but a custom class created by Yourself:

public class DataItem
{
    public string Column1 { get; set; }
    public string Column2 { get; set; }
    public string Column3 { get; set; }
    public string Column4 { get; set; }
}

To let the DataGrid display data stored in DataItem objects programmatically, You may do the following:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        // Your programmatically created DataGrid is attached to MainGrid here
        var dg = new DataGrid();
        this.MainGrid.Children.Add(dg);

        // create four columns here with same names as the DataItem's properties
        for (int i = 1; i <= 4; ++i)
        {
            var column = new DataGridTextColumn();
            column.Header = "Column" + i;
            column.Binding = new Binding("Column" + i);
            dg.Columns.Add(column);
        }

        // create and add two lines of fake data to be displayed, here
        dg.Items.Add(new DataItem { Column1 = "a.1", Column2 = "a.2", Column3 = "a.3", Column4 = "a.4" });
        dg.Items.Add(new DataItem { Column1 = "b.1", Column2 = "b.2", Column3 = "b.3", Column4 = "b.4" });
    }
}

I hope this helps.

Greetings Jörg

2
  • This is working well.But i want to do this for a DataGrid that is already created in XAML.I tried this on it,i can see columns getting added but nothing displays in the columns.I think the problem is with DataBinding. i tried <DataGridTextColumn Header="Code" Width="80" x:Name="colCode" Binding="{Binding}"> and <DataGridTextColumn Header="Code" Width="80" x:Name="colCode" Binding="{Binding ElementName=colCode}">
    – katu
    Commented Aug 16, 2012 at 5:30
  • Its alrite.I got it done.Problem was different column names and DataItem names.
    – katu
    Commented Aug 16, 2012 at 5:44
2

this a function which i use for retrive data from database string query = "Select * from VWpatientinfo"; DataTable dataTableObject = new DataTable("Table Name");

obj.DataRetrive(query,dataTableObject);
DataGridName.ItemsSource = dataTableObject.DefaultView;
1

enter image description here

.xaml:

<DataGrid x:Name="dataGrid" Margin="10">
    <DataGrid.Columns>
        <DataGridCheckBoxColumn Binding="{Binding Path=Column1}"/>
        <DataGridTextColumn Binding="{Binding Path=Column2}"/>
    </DataGrid.Columns>
</DataGrid>

.cs:

public class DataItem
{
    public bool Column1 { get; set; }
    public string Column2 { get; set; }
}

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        DataItem item = new DataItem();
        item.Column1 = true;
        item.Column2 = "test";
        dataGrid.Items.Add(item);
    }
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.