应用AutoFilter Sort时,ListObject不会更新基础DataSource Current属性 [英] ListObject does not update underlying DataSource Current property when AutoFilter Sort is applied

查看:107
本文介绍了应用AutoFilter Sort时,ListObject不会更新基础DataSource Current属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我们正在开发一个Excel 2010加载项,使用Visual Studio 2010开发,,ListObject绑定到BindingSource,BindingSource本身绑定到T的列表。 

Hello,

We are working on an Excel 2010 Add-in, developed with Visual Studio 2010, with a ListObject bound to a BindingSource which is itself bound to a List of T. 

在ListObject的Microsoft.Office.Tools.Excel.ListObject.BeforeDoubleClick事件处理程序中,我们希望根据参数"Range target"从List中获取关联的T对象。双击了。我们使用ListObect的
DataSource属性(它是一个BindingSource)来获取"当前"属性。要获得我们的项目。

$
例如: 

protected void PromotionListDoubleClick(范围目标,参考布尔取消)

{

促销targetPromotion =((BindingSource)ListObject.DataSource)。当前促销;

}



但是,当用户使用Excel的AutoFilter功能对表格进行排序时,这一切都会失效。  "当前"应用排序时不会更新。  仅当用户手动点击ListObject表中的另一个单元格时,
然后才会出现"当前"表格。属性已更新。



这是Excel的InterOp中的错误吗?  是否有解决方法?
b
$
到目前为止,我们一直在使用一些丑陋的代码来获取T,基于该表的另一个唯一列,但是这种方法不可持续,我们不能在没有唯一列的表格中使用它。



谢谢,

Ilia

In the Microsoft.Office.Tools.Excel.ListObject.BeforeDoubleClick event handler of the ListObject, we would like to get the associated T object from the List, based on the parameter "Range target" that was double clicked. We were using ListObect's DataSource property, which is a BindingSource, to get to the "Current" item to get our T.

Ex.: 
protected void PromotionListDoubleClick(Range target, ref bool cancel)
{
Promotion targetPromotion = ((BindingSource) ListObject.DataSource).Current as Promotion;
}

However, this all breaks down when the user applies a sort to the table, using AutoFilter feature of Excel.  The "Current" is not updated when the sort is applied.  Only if the user manually clicks on another cell in the ListObject’s table, then "Current" property is updated.

Is that a bug in the Excel's InterOp?  Is there a workaround for it?

So far, we have been using some ugly code to fetch out T, based on another unique column of the table, but this method is not sustainable, and we cannot use it in a table that does not have a unique column.

Thanks,
Ilia

PS:是否与此帖有关?

PS: Is it related to this post?

http://social.msdn.microsoft.com/论坛/ vstudio / zh-CN / 1a41e19b-6c27-468c-98ea-8dee258423fb / excel-listobject-in-office-2010-does-not-update-bindingsourcecurrentitem-index

http://social.msdn.microsoft.com/Forums/vstudio/en-US/1a41e19b-6c27-468c-98ea-8dee258423fb/excel-listobject-in-office-2010-does-not-update-bindingsourcecurrentitem-index

推荐答案

您好,

我按照以下步骤重现问题:

I reproduce the issue follow below steps:

1.创建Excel文档加载项目

2. 在sheet1中添加listobject控件

3. 绑定如下所示的列表对象:

1. Create excel document add-in project
2. Add listobject control in sheet1
3. Bind listobject like below:

private void PrepareData()
        {
            employeeTable = new System.Data.DataTable("Employees");

            System.Data.DataColumn column = employeeTable.Columns.Add
                ("Id", typeof(int));
            column.AllowDBNull = false;

            employeeTable.Columns.Add("FirstName", typeof(string));
            employeeTable.Columns.Add("LastName", typeof(string));
            employeeTable.Columns.Add("Age", typeof(int));

            employeeTable.Rows.Add(id, "Nancy", "Anderson", "56");
            employeeTable.Rows.Add(id, "Robert", "Brown", "44");
            id++;

            BindingSource binddingSource = new BindingSource();
            binddingSource.DataSource = employeeTable;
            list1.DataSource = binddingSource;
        }


<跨度>&NBSP;&NBSP;&NBSP; 4.  
排序或过滤前获取当前项目

void PromotionListDoubleClick(Microsoft.Office.Interop.Excel.Range
    Target, ref bool Cancel)
        {
       object obj = ((BindingSource)list1.DataSource).Current;
            DataRowView drv = ((BindingSource)list1.DataSource).Current as DataRowView;
            MessageBox.Show(drv["FirstName"].ToString());        }


<跨度>&NBSP;&NBSP;&NBSP; 5.  
比较排序或过滤后的当前项目,当前是没有改变。


BindingSource类
用于
通过在
Windows窗体控件和数据源之间提供货币管理,更改通知和其他服务,简化表单上的数据绑定控制。所以它与
不完全兼容
ListObject接口
属于 Microsoft.Office.Tools.Excel 命名空间。要获得所选行,我建议您使用以下代码:

But BindingSource Class is used to simplify binding controls on a form to data by providing currency management, change notification, and other services between Windows Forms controls and data source. So it is not fully compatible with ListObject Interface which belongs to Microsoft.Office.Tools.Excel namespace. To get the selected row, I suggest you to use the code below:

(list1.ListRows[list1.SelectedIndex].Range[columnIndex] as Excel.Range).Text





这篇关于应用AutoFilter Sort时,ListObject不会更新基础DataSource Current属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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