Datagrid binding in WPF

I know this has been asked already but I have done almost everything what is suggested by developers.

<DataGrid x:Name="Imported" VerticalAlignment="Top" DataContext="{Binding Source=list}"  AutoGenerateColumns="False" CanUserResizeColumns="True">
    <DataGrid.Columns>                
        <DataGridTextColumn Header="ID" Binding="{Binding Path=ID}"/>
        <DataGridTextColumn Header="Date" Binding="{Binding Path=Date}"/>
    </DataGrid.Columns>
</DataGrid>

I am trying to show this in modal dialog box and populating the license list in the constructor of the modal dialog box. But still nothing is getting populated inside the datagrid.

Constructor code:

public diagboxclass()
{
    List<object> list = new List<object>();
    list = GetObjectList();
}

public class object
{
    string id;
    DateTime date;
    public string ID
    {
        get { return id; }
        set { id = value; }
    }
    public DateTime Date
    {
        get { return date; }
        set { date = value; }
    }
}

Do you guys think something to do with the object list?

PLEASE do not use object as a class name:

public class MyObject //better to choose an appropriate name
{
    string id;
    DateTime date;
    public string ID
    {
       get { return id; }
       set { id = value; }
    }
    public DateTime Date
    {
       get { return date; }
       set { date = value; }
    }
}

You should implement INotifyPropertyChanged for this class and of course call it on the Property setter. Otherwise changes are not reflected in your ui.

Your Viewmodel class/ dialogbox class should have a Property of your MyObject list. ObservableCollection<MyObject> is the way to go:

public ObservableCollection<MyObject> MyList
{
     get...
     set...
}

In your xaml you should set the Itemssource to your collection of MyObject. (the Datacontext have to be your dialogbox class!)

<DataGrid ItemsSource="{Binding Source=MyList}"  AutoGenerateColumns="False">
   <DataGrid.Columns>                
     <DataGridTextColumn Header="ID" Binding="{Binding ID}"/>
     <DataGridTextColumn Header="Date" Binding="{Binding Date}"/>
   </DataGrid.Columns>
</DataGrid>

Bind DataGrid In WPF Using C#, We will use SQL Server 2008 for making our database and table and create our design for DataGrid in XAML format. After that bind that� The DataGrid control enables you to display and edit data from many different sources, such as from a SQL database, LINQ query, or any other bindable data source. For more information, see Binding Sources Overview. Columns can display text, controls, such as a ComboBox, or any other WPF content, such as images, buttons, or any content contained in a template.

Without seeing said object list, I believe you should be binding to the DataGrid's ItemsSource property, not its DataContext.

<DataGrid x:Name="Imported" VerticalAlignment="Top" ItemsSource="{Binding Source=list}"  AutoGenerateColumns="False" CanUserResizeColumns="True">
    <DataGrid.Columns>                
        <DataGridTextColumn Header="ID" Binding="{Binding ID}"/>
        <DataGridTextColumn Header="Date" Binding="{Binding Date}"/>
   </DataGrid.Columns>
</DataGrid>

(This assumes that the element [UserControl, etc.] that contains the DataGrid has its DataContext bound to an object that contains the list collection. The DataGrid is derived from ItemsControl, which relies on its ItemsSource property to define the collection it binds its rows to. Hence, if list isn't a property of an object bound to your control's DataContext, you might need to set both DataContext={Binding list} and ItemsSource={Binding list} on the DataGrid...)

DataGrid in WPF, The ItemSource property of DataGrid is the key to data binding. You can bind any data source that implements IEnuemerable. Each row in the DataGrid is bound to an object in the data source and each column in the DataGrid is bound to a property of the data source objects. To show a basic data grid , just drop a DataGrid control to your view and bind the ItemsSource to a collection of data objects and you're done. The DataGrid provides a feature called AutoGenerateColumns that automatically generates column according to the public properties of your data objects. It generates the following types of columns:

try to do this in the behind code

   public diagboxclass()
   {
         List<object> list = new List<object>();
         list = GetObjectList();
         Imported.ItemsSource = null;
         Imported.ItemsSource = list;
   }

Also be sure your list is effectively populated and as mentioned by Blindmeis, never use words that already are given a function in c#.

DataGrid columns, Especially the last one, the DataGridTemplateColumn, is interesting. It allows Columns> <DataGridTextColumn Header="Name" Binding="{Binding Name}"� DataGrid element represents WPF DataGrid control in XAML. <DataGrid />. When you drag and drop a DataGrid control from Toolbox to your designer, position the control, this action adds the following code to XA. The Width and Height properties represent the width and the height of a DataGrid. The Name property represents the name of the control, which is a unique identifier of a control.

DataGrid, Create each column in the DataGrid. Bind each column by setting the XPath on the binding to the query that gets the property on the item� Datagrid binding in WPF check this answer. Basically you need to add ItemSource binding so your grid knows the datacontext. Than you need to add binding for datagrid columns, so it knows what to display.

DataGridTextColumn Class (System.Windows.Controls), For more information about data binding, see Data Binding (WPF). Note DataGridTextColumn creates a TextBlock element in the non-editing mode and a TextBox� Just doing some light reading on WPF where I need to bind the selectedItems from a DataGrid but I am unable to come up with anything tangible. I just need the selected objects. DataGrid: &lt;Data

DataGrid, The data template gets the object that is bound to this row passed by the DataContext and can bind to it. <DataGrid ItemsSource="{Binding� Binding to Data To bind the DataGrid to data, set the ItemsSource property to an IEnumerable implementation. Each row in the data grid is bound to an object in the data source, and each column in the data grid is bound to a property of the data object.

Comments
  • My list object has more other properties than the two above which im not binding? would that can make any diff?
  • Probably not. Please share some more of your XAML (including how list is defined) so we can help further.
  • I have added code on the top.
  • @alice7 What type is the diagboxclass? What type is the XAML class containing the DataGrid? If diagboxclass is in the code-behind of the XAML file, you should assign list to the DataContext property of that class. You should then be able to bind to list from within the XAML.
  • Hi, If one of the Answers helped you back in 2011 please mark it as Answer, thx
  • In my case the ItemSource of the DataGrid should be: ItemsSource="{Binding MyList}"
  • @alice7 Then please post some XAML showing the rest of your Window/UserControl/Whatever contains the grid, and let us know how list is defined. :)
  • I have removed the entire content and just left the datagrid.so there is no other control. Here is the code isnide constructor: List<object> objectList = new List<object>(); objectList = GetObjectList();
  • Inside the constructor of... what? Please edit your question (click the edit link that appears beneath the tags) to include further code; it's pretty difficult to read in a comment. Thanks :)
  • @alice7: You need to expose that list as a public property and set an appropriate DataContext, you should probably read this reference.
  • The Binding in the ItemsSource Property is maybe wrong. In my case I was not successful when I declare the Binding with "{Binding Source=list}". But I was successful when I declare the Binding with "{Binding list}".