DataGridTextColumn-添加动态行-刷新DataGrid [英] DataGridTextColumn - Adding dynamic rows - refresh the DataGrid

查看:79
本文介绍了DataGridTextColumn-添加动态行-刷新DataGrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个我无法解决的问题.不胜感激.

我有以下情况:

1)映射到具有DataGridTextColumns和DataGridTemplateColumns的DataGrid的CollectionViewSource.
2)我的DataGrid可以很好地加载数据并在数据模型项值更改时进行更新.
3)我想向网格中添加动态行,以便可以直接在行/列之一中输入一些文本,并在相关行中填充其余数据.

例如,我有A,B,C列和以下数据:

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



现在,我想在A列中输入一个新ID(15345).我想用数据(Carl和84.3)填充此新行的B和C列.

我遇到的问题是刷新DataGrid之后添加了新行,但是我现在有4行,如下所示:



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



如何解决此问题?

谢谢,

Manish



How can I fix this problem?

Thanks,

Manish

推荐答案



在考虑了问题之后,解决方案非常简单并且效果很好.

我正在做的是创建对象的新实例并将其插入到dataModel中.我应该做的是从dataViewSource获取dataModel,然后找到已更新的行,并与dataModel中的对象一起更新数据项.

为了解释,假设我有一个实现INotifyPropertyChanged的对象TestData.

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.

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");
            }
        }



我的viewModel是List< testdata>绑定到DataGrid的对象集,如下所示:



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

// 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");





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





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





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



当我在A列中输入新项目时,我有一种方法可以执行以下操作以捕获事件并更新B列和C列:



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:

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);

    }


这篇关于DataGridTextColumn-添加动态行-刷新DataGrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆