设置RadGridView数据源时应用程序挂起/冻结

时间:2016-03-02 13:22:45

标签: c# winforms telerik radgrid radgridview

我有一个winforms应用程序,当我最小化窗口时,我需要进程仍在运行。在我设置RadGrid数据源之前一切正常:radGrid1.DataSource = datasource1; 当我以这种方式设置数据源时,应用程序就会冻结,并且没有更多的事情发生。 经过一些搜索后,我将代码修改为: radGrid1.BeginUpdate(); radGrid1.DataSource = datasource1; 这样我可以设置数据源,但我的网格丢失了格式。 如果我添加radGrid1.EndUpdate()它也会冻结。

我该怎么做才能加载数据源并且不会丢失我的radgrid的格式?

祝你好运

2 个答案:

答案 0 :(得分:1)

From the telerik docs

  

要防止网格遍历该集合中的所有数据字段,请将GridViewTemplate.AutoGenerateColumns属性设置为False。在这种情况下,您在排序,分组等时可能使用的其他字段应包含在MasterGridViewTemplate.Columns集合中。使用这些设置,将仅提取用作列FieldName属性的属性或在MasterGridViewTemplate.Columns中指定的属性。

应该修复你所描述的"丢失格式"的问题。 第二个问题,程序冻结,不是我在Windows窗体环境中使用RadGridViews的许多场合遇到的。

我唯一能想到的是你的数据源集合太大,或者集合中的项目有太多的字段RadGridView正在尝试生成AutoGenerateColumns属性为何时的列设为true

答案 1 :(得分:0)

我今天刚刚在一个使用Telerik RadGridView 2017.2.613.40的应用中遇到了这个问题。我在代码中定义了列:

 _grid.Columns.AddRange(
            //...more columns
            new GridViewCheckBoxColumn
            {
                Name = "IsSmallLabel",
                FieldName = "IsSmallLabel",
                MinWidth = 100,
                MaxWidth = 100,
                IsVisible = true,
                ReadOnly = false,
                HeaderText = "Small label"
            },
            new GridViewCheckBoxColumn
            {
                Name = "IsTechCard",
                FieldName = "IsTechCard",
                MinWidth = 100,
                MaxWidth = 100,
                IsVisible = true,
                ReadOnly = false,
                HeaderText = "Tech card"
            });

还有一个OnCellFormatting事件:

    private void OnCellFormatting(object sender, CellFormattingEventArgs e)
    {
        try
        {
            var checkCell = e.CellElement as GridCheckBoxCellElement;

            if (checkCell == null) return;

            var poItem = e.Row.DataBoundItem as PurchaseOrderItem;

            if (poItem == null) return;

            if (string.Equals(e.Column.Name, "IsSmallLabel", StringComparison.OrdinalIgnoreCase))
            {
                checkCell.Visibility = !string.IsNullOrEmpty(poItem.LotNo) ? ElementVisibility.Visible : ElementVisibility.Collapsed;
                checkCell.Value = !string.IsNullOrEmpty(poItem.LotNo);
            }
            else if (string.Equals(e.Column.Name, "IsTechCard", StringComparison.OrdinalIgnoreCase))
            {
                checkCell.Visibility = poItem.TechnologyCard != null ? ElementVisibility.Visible : ElementVisibility.Collapsed;
                checkCell.Value = poItem.TechnologyCard != null;
            }
        }
        catch (Exception ex)
        {
            _logger.Error(ex);
        }
    }

模型中实际上缺少了这两个字段,将它们添加在一起解决了应用程序挂起的问题。该问题似乎与格式化功能有关,因为删除它也会使应用程序停止挂起。