使用dataGridView和BindingSource的奇怪问题

时间:2011-05-13 13:51:37

标签: c#

我在使用DataGridView时遇到了一个非常奇怪的问题。我的自定义控件中有3个网格。我正在使用此实现:我已使用设计器将datagridview与bindingsource相关联。我的控件的用户使用一些参数调用initialize方法。控件填充3个内部列表(实际上是2个列表和对象字典)。填充列表后,我将列表分配给绑定源。

两个网格工作正常,但其中一个没有显示任何内容(基础列表包含数据)。如果我改变这一行:

bindingSource.DataSource = myList;

bindingSource.DataSource = myList;
datagridView.DataSource = null;
datagridView.DataSource = bindingSource;

网格将显示行,但网格上的任何事件都会引发异常(IndexOutOfRangeException“Index -1没有值。”在这行代码中。)

这是相关代码:

    public partial class DisplayTradeSplitValuesControl : UserControl
    {
        List<TradeSplittingRatio> templateDatasource_ = new List<TradeSplittingRatio>();
        List<TradeSplittingRatio> fundsDatasource_ = new List<TradeSplittingRatio>();
        Dictionary<string,List<TradeSplittingRatio>> portfolioDatasource_ =
            new Dictionary<string,List<TradeSplittingRatio>>();

        private string instrumentName_ = string.Empty;

        private RoundingRuleAbstract roundingRule_;

        public DisplayTradeSplitValuesControl()
        {
            InitializeComponent();
        }

        public void Initialize(string portfolioName, string instrumentName, double value)
        {
            ConfigManager.GetConfigManager().LoadPortfolioCache();
            ConfigManager.GetConfigManager().LoadPortfolioRatioCache();            
            instrumentName_ = instrumentName;
            roundingRule_ = GetGranularity(instrumentName_);
            PopulateTemplateDatasource(portfolioName, value);
            PopulateFundDatasource(portfolioName, value);
            PopulatePortfolioAndManagedAccountDatasource();
            PopulateGrid();
        }

        private void PopulateGrid()
        {
            templateSplittingRatioBindingSource.DataSource = templateDatasource_;
            fundTradeSplittingRatioBindingSource.DataSource = fundsDatasource_;
            templateDataGridView.Refresh();
            //fundsDataGridView.DataSource = null; //not working hack!!!
            //fundsDataGridView.DataSource = fundTradeSplittingRatioBindingSource.DataSource;
            fundsDataGridView.Refresh();
            if (fundsDatasource_.Count > 0)
            {
                string portfolioName = fundsDatasource_[0].ToPortfolio;
                portfolioTradeSplittingRatioBindingSource.DataSource = portfolioDatasource_[portfolioName];

                portfolioDataGridView.Refresh();
            }

        }

        private void PopulateTemplateDatasource(string portfolioName, double value)
        {
            templateDatasource_.Clear();
            TradeSplittingRatio template = new TradeSplittingRatio(string.Empty, portfolioName, 0, value, 0);
            templateDatasource_.Add(template);
        }

        private void PopulateFundDatasource(string portfolioName, double value)
        {
            fundsDatasource_.Clear();
            if (ConfigManager.GetConfigManager().ActivePortfolioCache.ContainsKey(portfolioName) == true)
            {
                Portfolio portfolio = ConfigManager.GetConfigManager().ActivePortfolioCache[portfolioName];
                if (ConfigManager.GetConfigManager().ActivePortfolioRatioCache.ContainsKey(portfolio) == true)
                {
                    List<KeyValuePair<Portfolio, double>> portfolioRatio =
                        ConfigManager.GetConfigManager().ActivePortfolioRatioCache[portfolio];

                    foreach (KeyValuePair<Portfolio, double> ratio in portfolioRatio)
                    {
                        fundsDatasource_.Add(new TradeSplittingRatio(portfolioName,
                                                               ratio.Key.Identifier,
                                                               ratio.Value,
                                                               roundingRule_.Calculate(new Ratio(ratio.Value).Calculate(value)),
                                                               roundingRule_.Granularity));
                    }
                }
            }
        }

        private void PopulatePortfolioAndManagedAccountDatasource()
        {
            portfolioDatasource_.Clear();

            List<TradeSplittingRatio> cloneFundsDatasource = new List<TradeSplittingRatio>();
            foreach (TradeSplittingRatio fund in fundsDatasource_)
            {
                cloneFundsDatasource.Add((TradeSplittingRatio)fund.Clone());
            }

            foreach (TradeSplittingRatio fund in cloneFundsDatasource)
            {
                if (portfolioDatasource_.ContainsKey(fund.ToPortfolio) == false)
                    portfolioDatasource_.Add(fund.ToPortfolio,new List<TradeSplittingRatio>());

                Portfolio portfolio = ConfigManager.GetConfigManager().ActivePortfolioCache[fund.ToPortfolio];
                List<KeyValuePair<Portfolio, double>> portfolioRatio =
                    ConfigManager.GetConfigManager().ActivePortfolioRatioCache[portfolio];

                foreach (KeyValuePair<Portfolio, double> ratio in portfolioRatio)
                {
                    portfolioDatasource_[fund.ToPortfolio].Add(new TradeSplittingRatio
                        (fund.ToPortfolio,
                        ratio.Key.Identifier,
                        ratio.Value,
                        roundingRule_.Calculate(new Ratio(ratio.Value).Calculate(fund.Amount)),
                        roundingRule_.Granularity));
                }
            }
        }

        private RoundingRuleAbstract GetGranularity(string instrumentName)
        {
            ConfigManager.GetConfigManager().LoadBondCache();
            ConfigManager.GetConfigManager().LoadInstrumentCache();

            if (ConfigManager.GetConfigManager().ActiveInstrumentCache.ContainsKey(instrumentName))
            {
                if (string.Equals(ConfigManager.GetConfigManager().ActiveInstrumentCache[instrumentName].InstrumentType.Identifier,"CASH") ||
                    string.Equals(ConfigManager.GetConfigManager().ActiveInstrumentCache[instrumentName].InstrumentType.Identifier, "FX"))
                {
                    return new RoundingRule.RoundHalfUpRule(ConfigManager.GetConfigManager().ActiveInstrumentCache[instrumentName].Currency.Granularity);
                }
                if (ConfigManager.GetConfigManager().ActiveBondCache.ToList().
                        Where(c => c.Value.InstrumentId == ConfigManager.GetConfigManager().ActiveInstrumentCache[instrumentName].InstrumentId).Count() > 0)
                {
                    return new RoundingRule.RoundHalfUpRule(
                        ConfigManager.GetConfigManager().ActiveBondCache.ToList().
                        Where(c => c.Value.InstrumentId == ConfigManager.GetConfigManager().ActiveInstrumentCache[instrumentName].InstrumentId).First().Value.Granularity);
                }
            }
            return new RoundingRule.RoundHalfUpRule(1.0); 
        }

        private void fundsDataGridView_SelectionChanged(object sender, EventArgs e)
        {
            if (fundsDataGridView.Focused)
            {
                TradeSplittingRatio fund = (TradeSplittingRatio)fundsDataGridView.SelectedRows[0].DataBoundItem;
                portfolioTradeSplittingRatioBindingSource.DataSource = portfolioDatasource_[fund.ToPortfolio];
                portfolioDataGridView.Refresh();                
            }
            //Validate();
        }
    //other stuff not important
    }
}

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

我没有通过所有代码,我跳到最后,这部分跳出来对我:

        private void fundsDataGridView_SelectionChanged(object sender, EventArgs e)
        {
            if (fundsDataGridView.Focused)
            {
                TradeSplittingRatio fund = (TradeSplittingRatio)fundsDataGridView.SelectedRows[0].DataBoundItem;
                portfolioTradeSplittingRatioBindingSource.DataSource = portfolioDatasource_[fund.ToPortfolio];
                portfolioDataGridView.Refresh();                
            }
            //Validate();
        }

即使您尚未检查是否包含任何项目,也可以访问DataGridView.SelectedRows[0]。当选择变为空时(通常因为数据源被更改或清除),SelectionChanged完全可以触发。

如果您未能在此处测试边界,则可能您在其他地方未能这样做。您所提供的错误消息确切地说:某些东西,可能是一个事件,是在索引为-1的情况下引发的,并且您正在尝试访问第-1行,这当然不存在。

<强>更新

我引用的代码的另一个潜在问题是聚焦检查。我不确定你为何关注焦点,但是DGV在用户当前正在编辑的单元格中使用(单个)子控件。如果“编辑控件”具有焦点,则DGV.Focused将为false。