【问题标题】:DevExpress lookupedit repository item add new row in Xtra Grid View in first rowDevExpress 查找编辑存储库项在第一行的 Xtra Grid 视图中添加新行
【发布时间】:2015-12-03 09:02:08
【问题描述】:

我有一个 5 列的 devexpress 网格控件。第一列是一个带有一些数据的查找编辑存储库,比如说 CarTypes。 要在网格中加载数据,我使用的是BindingSource。在这个BindingSource.DataSource 我已经加载了一个IList<Cars>

然后 在我的 gridcontrol 的 dataSource 中添加了这个绑定源 像下面的

BindingSource _carsBindingSource = new BindingSource();

private void BindData(IList<Cars> data)
{
            _carsBindingSource.DataSource = data;

            carsGridControl.BeginUpdate();
            carsGridControl.DataSource = _carsBindingSource;
            carsGridControl.RefreshDataSource();
            carsGridControl.EndUpdate();
 }

我有一个按钮可以在我的网格“添加新车”中添加新行并在 _carBindingSource 中添加新行

    private void AddNewRow()
    {
                _newRow = true;
                _carsBindingSource.AllowNew = true;
                Cars newCar = new Cars();
                newCar.CarType = new CarType();            
                _carsBindingSource.Add(newCar );
                //_carsBindingSource.Insert(0,newCar);


   }

现在我想在网格的第一行添加新行。

我用Insert

_carsBindingSource.Insert(0,newCar);

但它没有用。 lookupedit repository 无法加载数据。

_carsBindingSource.Add(newCar); 可以正常工作

谁能帮帮我?谢谢!

【问题讨论】:

  • 这行字是什么意思???现在你已经把你的问题写得很好,但仍然不清楚你在问什么..
  • 你有权利@NiranjanKala。实际上,我想在我的 carsBindingSource 中添加一个新行,但在网格的第一行。我使用 carsBindingSource.Insert(0,newCar) 但不能正常工作
  • 实际上我找到了解决方案。问题出在 GridView_CustomRowCellEdit(object sender, CustomRowCellEditEventArgs e) 事件中,我更改了 AllowEdit 值 (e.Column.OptionsColumn.AllowEdit = true;)。 .Add(object), .Insert(0,object) 一样!

标签: c# devexpress bindingsource gridcontrol devexpress-windows-ui


【解决方案1】:

如果您还没有,请考虑为您的汽车类型使用中间列表:

private List<CarTypes> _CarTypes;

// Elsewhere in the code...
_CarTypes = GetCarTypes();

然后在表单加载事件中,确保这是绑定到数据源的:

repositoryLookupItemCarTypes.DataSource = _CarTypes;

有了这个,网格现在应该自动管理每个 Cars 对象的 CarType 对象的实例化和选择。将汽车添加到网格时可以省略此行:

newCar.CarType = new CarType(); 

在设计器中,我认为更改存储库项的DisplayMember 属性会有所帮助。

通过此设置,添加到网格中的任何汽车都应自动将 CarType 作为填充的查找编辑。

如果有任何不清楚的地方,请告诉我。我做了一个快速而肮脏的解决方案来测试它,我显然不能全部发布,但我可以告诉你它确实适用于 AddInsert

【讨论】:

  • 感谢您的回答。但我已经拥有了这一切。使用 BindingSource.Add(newCar) 可以正常工作,但问题是新行位于网格的最后一行,我想添加到第一行。使用 Insert(0,newCar) 它会转到第一行,但我必须多次单击其他列,直到在 lookupedit 存储库中重新加载数据,我尝试使用 Insert 的 bindingSource.AddNew() 命令,现在添加了 2 行。一个在第一行,一个在网格末尾,但现在可以工作了!我不明白会发生什么
  • 好吧,我很高兴它至少有效。对于它的价值,您可以在设计时(在表单上)而不是在代码中添加绑定源。也许你这样做了,只是展示了代码来说明它,但以防万一......
【解决方案2】:

实际上我找到了解决方案。 问题出在 GridView_CustomRowCellEdit(object sender, CustomRowCellEditEventArgs e) 事件中 我在哪里更改 AllowEdit 值(e.Column.OptionsColumn.AllowEdit = true;)。

private void gridView_CustomRowCellEdit(object sender, CustomRowCellEditEventArgs e)
{
    string cName = e.Column.FieldName;
    GridView gv = sender as GridView;

    if (cName == "CarType.IdApi")
    {
        if (isNewRow)
        {
            Cars cars= (Cars)gv.GetRow(e.RowHandle);

            int a = e.RowHandle;
            if (cars.ID== 0 && e.RowHandle == 0)
            {
               e.Column.OptionsColumn.AllowEdit = true;
            }
            else
            {
               e.Column.OptionsColumn.AllowEdit = false;
            }
         }        
     }
}

当我使用 Insert(0, new Car) 时,因为第二行 whitch 有价值 AllowEdit 是假的; 所以我删除了else 代码,它可以工作了

private void gridView_CustomRowCellEdit(object sender, CustomRowCellEditEventArgs e)
    {
        string cName = e.Column.FieldName;
        GridView gv = sender as GridView;

        if (cName == "CarType.IdApi")
        {
            if (isNewRow)
            {
                Cars cars= (Cars)gv.GetRow(e.RowHandle);

                int a = e.RowHandle;
                if (cars.ID== 0 && e.RowHandle == 0)
                {
                   e.Column.OptionsColumn.AllowEdit = true;
                }
             }        
         }
    }

所以finlay我发现bindingSource.Add(object)bindingSource.Insert(0,object)是一样的!

我为我的英语道歉!!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-09
    • 2021-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-26
    • 1970-01-01
    相关资源
    最近更新 更多