insert new row in datagrid

Dec 1, 2008 at 4:28 PM
Hi,
First of all, sorry for my english.

I have a problem.

System.Collections.ObjectModel.ObservableCollection<myItem> MyItem = new .....
MyDataGrid.DataContext = MyItem

<dg:DataGrid Name="MyDataGrid" CanUserAllRows="true" ..... />

but when I start my test Application, no rows are displayed on the datagrid, and I can't insert nothing.

How could I do it ?

thanks
Dec 1, 2008 at 6:17 PM
You need set the ItemsSource for your datagrid.

MyDataGrid.ItemsSource = MyItem;

Or you can keep your code the way it is and do something like this:

System.Collections.ObjectModel.ObservableCollection<myItem> MyItem = new .....
MyDataGrid.DataContext = MyItem

<dg:DataGrid Name="MyDataGrid" ItemsSource="{Binding}" CanUserAllRows="true" ..... />
Dec 2, 2008 at 7:42 AM
thanks for the reply.

I did as you said, but mygrid is empty with no rows and I can't insert new row. I think it is because the collection is empty...
Dec 2, 2008 at 7:42 AM
thanks for the reply.

I did as you said, but mygrid is empty with no rows and I can't insert new row. I think it is because the collection is empty...
Dec 3, 2008 at 4:35 PM
please help me....
Coordinator
Dec 3, 2008 at 5:03 PM
See this post, http://blogs.msdn.com/vinsibal/archive/2008/10/01/overview-of-the-editing-features-in-the-wpf-datagrid.aspx.  It talks about some of the things that have to be enabled for being able to add new rows.
Dec 3, 2008 at 5:42 PM
I think I see what you mean. I am able to reproduce this when I set AutoGenerateColumns=true and my collection is empty. It looks like the DataGrid defers auto-generating the columns when your collection is empty. The problem is that if there are no columns in your datagrid, then there's nowhere to click to add a new row.

I'm not sure if this is the intended behavior or a bug but you can work around it by setting AutoGenerateColumns=false and explicity defining columns in the XAML.

<dg:DataGrid Name="MyDataGrid" ItemsSource="{Binding}" CanUserAllRows="true" AutoGenerateColumns="False">
  <dg:DataGrid.Columns>
    <dg:DataGridTextColumn Header="Column 1" Binding="{Binding Column1}" />

    ...

  </dg:DataGrid.Columns>
</dg:DataGrid>
Dec 4, 2008 at 8:17 AM
thank you very much Peterburrell, I did as you said and all works fine.
I find the problem.
I have a collection like this.

public class MyCollection : ObservableCollection<MyItem>
{
    // nothing here
}

if I do

MyCollection items = new MyCollection();
MyDataGrid.DataContext =  items;

Don't Work and the grid display me only columns but no rows for add, I items collection is empty;

if I do
ObservableCollection<MyItem> items = new ObservableCollection<MyItem>();
MyDataGrid.DataContext =  items;

All Works also collection is empty.

do you konw why ?

thanks.

ps: sorry for my english.



Dec 4, 2008 at 8:18 AM
thank you very much Peterburrell, I did as you said and all works fine.
I find the problem.
I have a collection like this.

public class MyCollection : ObservableCollection<MyItem>
{
    // nothing here
}

if I do

MyCollection items = new MyCollection();
MyDataGrid.DataContext =  items;

Don't Work and the grid display me only columns but no rows for add, I items collection is empty;

if I do
ObservableCollection<MyItem> items = new ObservableCollection<MyItem>();
MyDataGrid.DataContext =  items;

All Works also collection is empty.

do you konw why ?

thanks.

ps: sorry for my english.



Jan 27, 2009 at 11:31 AM
Hi,

I am having the same problem and I'd like to know if this is a bug or the intended behavior.
Why doesn't WPF DataGrid show an empty row when one uses a class deriving from an ObservableCollection (or Collection) as a binding source and the collection has no elements?

thanks.
Jan 27, 2009 at 1:08 PM
...so the answer to my question is that my custom made collection has to have a generic argument, even if it's not used.

What I had before was this:

public class MyCollection : Collection<MyClass>
{
 ...
}

What I have now is:
public class MyCollection <T> : Collection<MyClass> where T : MyClass
{
 ...
}
Adding the generic argument {T} in the second case enabled the DataGrid to show an empty row for adding new values as I wanted.

However this generic argument is not used anywhere in MyCollection, and it seems redundant. In the first case, the type of elements in the collection would still be MyClass, but it seems that DataGrid (or data binding FW) fails to "see" that...

Am I missing something obvious here or is it a bug?
Jan 28, 2009 at 1:48 PM
Is this the same issue as http://www.codeplex.com/wpf/Thread/View.aspx?ThreadId=42062 ?
Jan 28, 2009 at 2:31 PM
It looks like it's the same issue, yes.
Jun 20, 2011 at 12:10 PM

the problem is that  DataGrid try to create instant of new MyItem()  for new empty row.

So DataType of your ItemSource "must have" a default constructor that accept no params

public class MyItem
{

       // the Constructor with NO params must declared
        public MyItem()
        {
            //do something or leave nothing.
        }
}

DataGrid CanUserAddRows only work if.. it can create new MyItem()