第一列中的ListObject绑定错误 [英] ListObject binding bug in first column

查看:123
本文介绍了第一列中的ListObject绑定错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!


我正在实现一个excel工作簿,从绑定列表中查看数据。


例如:


公共类数据:INotifyPropertyChanged

    {

       公共字符串A {get;组; }
       公共字符串B {get;组; }
        public string C {get;组; }


       公共事件PropertyChangedEventHandler PropertyChanged;

    }


公共类DataCollect

    {

        public BindingList< Data> Entities = new BindingList< Data>();



        public DataCollect()

        {

            Entities.Add(new Data(){A =" value1",B =" 1",C =" a"}};

      &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; Entities.Add(new Data(){A =" value2",B =" 2",C =" b"}};

      &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; Entities.Add(new Data(){A =" value3",B =" 3",C =" c"}});



  &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; Entities.AllowNew = true;

            Entities.AllowEdit = true;

            Entities.AllowRemove = true;

            Entities.RaiseListChangedEvents = true;

        }
    }


我在工作簿中只有一个工作表。当我打开我的工作簿时,我将ListObject添加到此工作表并将我的BindingList设置为此控件的DataSource。





公共部分课程Sheet1

    {

        private Microsoft.Office.Tools.Excel.ListObject listObject;
$


       私人DataCollect dataCol;
$


        private void Sheet1_Startup(object sender,System.EventArgs e)

        {

           listObject = this.Controls.AddListObject(this.Range [" $ A $ 1:$ C $ 1"]," test");

        ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; dataCol = new DataCollect();

            listObject.DataSource = dataCol.Entities;

            listObject.AutoSetDataBoundColumnHeaders = true;

        }¥b $ b  

    }


然后我只在第一列编辑单元格


1)通过键入text更改单元格值 - datasource contains相应属性中的新值(正确)


2)复制上部单元格并粘贴到下部单元格 - 数据源包含列标题文本而不是新单元格值。  ;


3)使用Ctrl + D时的行为相同。



此错误仅复制第一列。


修复此错误或找到解决方法非常重要。我们的客户将使用此应用程序进行计算。



Visual Studio 2013,


.Net 4.5



由于我们的发布截止日期,请尽快回答。






解决方案

b


我认为您可能需要添加更改数据类中的通知。在更改代码后,我无法重现您的问题,如下所示:

使用System;使用System.ComponentModel 
;使用System.Runtime.CompilerServices
;

命名空间ExcelDataBinding
{
公共类数据:INotifyPropertyChanged
{
private void NotifyPropertyChanged([CallerMemberName] String propertyName ="")
{
if(PropertyChanged!= null)
{
PropertyChanged(this,new PropertyChangedEventArgs(propertyName));
}
}

private string _a;

private string _b;

private string _c;

public string A {get {return _a; } set {_a = value; NotifyPropertyChanged();
公共字符串B {get {return _b; } set {_b = value; NotifyPropertyChanged();
公共字符串C {get {return _c; } set {_c = value; NotifyPropertyChanged();

公共事件PropertyChangedEventHandler PropertyChanged;
}

公共类DataCollect
{
public BindingList< Data> Entities = new BindingList< Data>();

public DataCollect()
{
Entities.Add(new Data(){A =" value1",B =" 1",C =" a" });
Entities.Add(new Data(){A =" value2",B =" 2",C =" b"}});
Entities.Add(new Data(){A =" value3",B =" 3",C =" c"}});

Entities.AllowNew = true;

Entities.AllowEdit = true;

Entities.AllowRemove = true;

Entities.RaiseListChangedEvents = true;
}
}


公共部分类Sheet4
{
private Microsoft.Office.Tools.Excel.ListObject listObject;

私有DataCollect dataCol;

private void Sheet4_Startup(object sender,System.EventArgs e)
{
listObject = this.Controls.AddListObject(this.Range ["


A

1

Hi!

I am implementing a excel workbook, viewing a data from binding list.

For example:

public class Data : INotifyPropertyChanged
    {
        public string A { get; set; }
        public string B { get; set; }
        public string C { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;
    }

public class DataCollect
    {
        public BindingList<Data> Entities=new BindingList<Data>();

        public DataCollect()
        {
            Entities.Add(new Data(){A="value1", B="1",C= "a"});
            Entities.Add(new Data() { A = "value2", B = "2", C = "b" });
            Entities.Add(new Data() { A = "value3", B = "3", C = "c" });

            Entities.AllowNew = true;
            Entities.AllowEdit = true;
            Entities.AllowRemove = true;
            Entities.RaiseListChangedEvents = true;
        }
    }

I have only 1 worksheet in workbook. When I open my workbook, I add a ListObject to this sheet and set my BindingList as a DataSource for this control.


public partial class Sheet1
    {
        private Microsoft.Office.Tools.Excel.ListObject listObject;

        private DataCollect dataCol;

        private void Sheet1_Startup(object sender, System.EventArgs e)
        {
           listObject= this.Controls.AddListObject(this.Range["$A$1:$C$1"], "test");
            dataCol=new DataCollect();
            listObject.DataSource = dataCol.Entities;
            listObject.AutoSetDataBoundColumnHeaders = true;
        }
  
    }

Then I edit cells only in first column :

1) change cell value by typing text - datasource contains new value(correct) in appropriate property

2) copy upper cell and paste to down cell - datasource contains column header text instead of new cell value. 

3) the same behavior when use Ctrl+D .

This bug reproduces only for first column.

It is very important to fix this bug or find a workaround. Our customers will use this application for calculations.

Visual Studio 2013,

.Net 4.5

Please, answer ASAP because of our release deadlines.



解决方案

Hi,

I think you may need to add the change notification in Data class. And I’m not able to reproduce your issue after changing your code as following:

using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace ExcelDataBinding
{
    public class Data : INotifyPropertyChanged
    {
        private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private string _a;

        private string _b;

        private string _c;

        public string A { get {return _a; } set { _a = value; NotifyPropertyChanged(); } }
        public string B { get { return _b; } set { _b = value; NotifyPropertyChanged(); } }
        public string C { get { return _c; } set { _c = value; NotifyPropertyChanged(); } }

        public event PropertyChangedEventHandler PropertyChanged;
    }

    public class DataCollect
    {
        public BindingList<Data> Entities = new BindingList<Data>();

        public DataCollect()
        {
            Entities.Add(new Data() { A = "value1", B = "1", C = "a" });
            Entities.Add(new Data() { A = "value2", B = "2", C = "b" });
            Entities.Add(new Data() { A = "value3", B = "3", C = "c" });

            Entities.AllowNew = true;

            Entities.AllowEdit = true;

            Entities.AllowRemove = true;

            Entities.RaiseListChangedEvents = true;
        }
    }


    public partial class Sheet4
    {
        private Microsoft.Office.Tools.Excel.ListObject listObject;

        private DataCollect dataCol;

        private void Sheet4_Startup(object sender, System.EventArgs e)
        {
            listObject = this.Controls.AddListObject(this.Range["


A


1:


这篇关于第一列中的ListObject绑定错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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