Erika
Erika

Reputation: 289

Blank Row is not visible in data-bound DataGrid

I have an ObservableCollection bound to a DataGrid, and I want to be able to add rows in it at run time, using the actual rows. However, the blank row is not visible, therefore the user is not able to add rows. I have researched this already, I have added CanUserAddRows=true, and IsReadOnly=false. I have also added a blank constructor for the CostCenter object, and it is still not working. The code below is what I have so far:

In XAML:

<Window.Resources>
    <CollectionViewSource x:Key="jobItemViewSource1" d:DesignSource="{d:DesignInstance my:JobItem, CreateList=True}" />
    <CollectionViewSource x:Key="CostCenterListViewSource" Source="{Binding Path=CostCenterList, Source={StaticResource jobItemViewSource1}}" />
</Window.Resources>


<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Source={StaticResource CostCenterListViewSource}}" Name="costCenterListDataGrid" CanUserResizeRows="False" CanUserAddRows="True" CanUserDeleteRows="True" Background="LightGray" IsReadOnly="False">
    <DataGrid.Columns>
        <DataGridTextColumn x:Name="nameColumn" Binding="{Binding Path=Name, Mode=TwoWay}" Header="Cost Center" />
        <DataGridTextColumn x:Name="glCodeColumn" Binding="{Binding Path=GlCode, Mode=TwoWay}" Header="Gl Code"/>
        <DataGridTextColumn x:Name="costCenterPercentageColumn" Binding="{Binding Path=CostCenterPercentage, Mode=TwoWay}" Header="%"/>
    </DataGrid.Columns>
</DataGrid>

In the code behind I have set the datacontext:

costCenterListDataGrid.DataContext = item.CostCenterList;

In the JobItem Class:

private ObservableCollection<CostCenter> costCenterList;
public JobItem()
{
    costCenterList = new ObservableCollection<CostCenter>();         
}

public ObservableCollection<CostCenter> CostCenterList
{
    get { return costCenterList; }
    set
    {
        costCenterList = value;
    }

}

and in the CostCenter class I have:

public CostCenter()
{
    afes = new ObservableCollection<Afe>();
}

public CostCenter(ObservableCollection<Afe> afes, string glCode)
{
    this.afes = afes;
    this.glCode = glCode;
}

public string Name
{
    get { return name; }
    set
    {
        if (String.IsNullOrEmpty(Name) || !Name.Equals(value))
        {
            name = value;
            OnPropertyChanged("Name");
        }
    }
}

public ObservableCollection<Afe> Afes
{
    get { return afes; }
    set { afes = value; }
}

public string GlCode
{
    get { return glCode; }
    set 
    {
        if (String.IsNullOrEmpty(GlCode) || !GlCode.Equals(value))
        {
            glCode = value;
            OnPropertyChanged("GlCode");
        }
    }
}
public double TotalPercentage
{
    get { return totalPercentage; }
    set
    {
       if (totalPercentage + value <= 100)
       {
           totalPercentage += value;
       }

    }
}

    public double CostCenterPercentage
    {
        get { return cPercentage; }
        set
        {
            if (CostCenterPercentage != value)
            {
                cPercentage = value;
                OnPropertyChanged("CostCenterPercentage");
            }
        }

    }

I have tried to omit irrelevant code, the OnPropertyChanged method is working as I have used it in other code, otherwise, hopefully this is enough code for you to help me figure things out. Also the datacontext for JobItem is working, which I have not added in this snippet either.

Upvotes: 1

Views: 356

Answers (2)

Vinit Sankhe
Vinit Sankhe

Reputation: 19895

Your CollectionViewSource is not creating a IEditableCollectionView type collection view which is required if you want your DataGrid to show Add, Delete and Editable Rows.

http://blogs.msdn.com/b/vinsibal/archive/2008/10/01/overview-of-the-editing-features-in-the-wpf-datagrid.aspx

Upvotes: 0

Eric Robinson
Eric Robinson

Reputation: 2095

This just might have been overlooked but do you databind the dataGrid after you've added a blank object?

costCenterListDataGrid.DataContext = item.CostCenterList;
costCenterListDataGrid.DataBind()

Upvotes: 0

Related Questions