Click here to Skip to main content
15,913,941 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a problem that I am not able to solve. Would appreciate any help.

I have the following situation:

1) CollectionViewSource that is mapped to a DataGrid that has DataGridTextColumns and DataGridTemplateColumns.
2) My DataGrid loads up fine with the data and updates as and when the data model item values change.
3) I want to add dynamic rows to the Grid such that I can directly enter some text in one of the row/columns and populate the rest of the data in the relavant row.

For example, I have columns A, B, C and the following data:

A          B          C
------------------------
12133    Henry      89.4
14345    John       78.2


Now, I want to enter a new ID in column A (15345). I want to populate column B and C for this new row with the data (Carl and 84.3).

The problem I am having is that the new row is added after the DataGrid is refreshed, but I have now 4 rows as follows:

A          B          C
------------------------
12133    Henry      89.4
14345    John       78.2
15345    0          0
15345    Carl       84.3


How can I fix this problem?

Thanks,

Manish
Posted
Comments
wizardzz 11-Jul-11 12:24pm    
Not sure what your doing, could you post the code you use?
Manish V Mahajan 12-Jul-11 8:56am    
Code posted below as the solution. Cheers.

1 solution

Hi,

After thinking about the problem, the solution was quite simple and works great.

What I was doing was to create a new instance of the object and insert it in the dataModel. What I should have done was to get the dataModel from the dataViewSource and then find the row that was updated and update the data items withing the object in th dataModel.

To explain, let's say I have an object TestData that implements INotifyPropertyChanged.

C#
namespace DataGridUpdate
{
    // Encapsulates pricing information for the same data file - Need to move this to a another file.
    [Serializable]
    public class TestData : INotifyPropertyChanged
    {
        // Get; Set; 
        public string _a;
        public string A
        {
            get { return _a; }
            set
            {
                Trace.WriteLine("TestData: A= " + value);
                _a = value;
                OnPropertyChanged("A");
            }
        }

        // Get; Set;
        public string _b;
        public string B
        {
            get { return _b; }
            set
            {
                _b = value;
                OnPropertyChanged("B");
            }
        }

        Get; Set;
        public double _b;
        public double B
        {
            get { return _b; }
            set
            {
               b = value;
               OnPropertyChanged("B");
            }
        }


My viewModel is a List<testdata> set of objects that is bound to the DataGrid as the follows:

C#
// MainView.xaml.cs
// TestData Structure in the XAML
private static CollectionViewSource dataViewSource;

// Get a handle on the Data Source in the WPF
dataViewSource = (CollectionViewSource)FindResource("TestDataSource");


MIDL
//Populate the WPF Data - where dataList = List<testdata>
dataViewSource.Source = dataList;</testdata>


XML
<!-- MainView.xaml.  The Main DataStructure that is used to populate the Data in the Table -->
<CollectionViewSource x:Key="TestDataSource"/>


CSS
<!-- MainView.xaml - Main Grid Area -->
<DataGrid x:Name="DG" ItemsSource="{Binding Source={StaticResource TestDataSource}}"


When I enter a new item in column A, I have a method that does the following to capture the event and update the column B and C:

C++
private void TextBox_TextChanged(object sender, KeyEventArgs args)
{
    Trace.WriteLine("TextBox_TextChanged (A): dataList Count = " + dataList.Count);
    if (args.Key == Key.Return)
    {
        TextBox t = (TextBox)sender;
        Trace.WriteLine("TextBox_TextChanged: " + t.Text);

        dataList = (List<TestData>)dataViewSource.Source;
        
        // Implement this to find the particular row based on the column A key value or some other method.  Here, I only get the last row, but this can be changed easily to capture the particular row where the data was changed.
        TestData td = (TestData)dataList[dataList.Count - 1];
        td.A = t.Text + "CHANGED";
        td.B = Carl;
        td.C = 84.3;

        // NOTE - This is what I was doing earlier (NOT REQUIRED)
        //dataList.Add(td);
        Trace.WriteLine("TextBox_TextChanged : dataList Count = " + dataList.Count);

    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900