Search code examples
c#devexpressbindingsourcegridcontroldevexpress-windows-ui

DevExpress lookupedit repository item add new row in Xtra Grid View in first row


I have a devexpress gridcontrol with 5 columns. The first column is a lookupedit repository with some data, let say with CarTypes. To load data in grid I am using a BindingSource. In this BindingSource.DataSource I have load a IList<Cars>

and then added this binding source in dataSource of my gridcontrol like bellow

BindingSource _carsBindingSource = new BindingSource();

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

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

I have a button to add new line in my grid "Add new car" and add a new line in _carBindingSource

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


   }

Now I want to add the new line in the first row of grid.

I use Insert

_carsBindingSource.Insert(0,newCar);

But it didn't work. The lookupedit repository can't load data.

With _carsBindingSource.Add(newCar); it works fine

Can anyone help me? Thank you!


Solution

  • Actualy I found a solutions. The problem was in GridView_CustomRowCellEdit(object sender, CustomRowCellEditEventArgs e) event where I change the AllowEdit value (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;
                }
             }        
         }
    }
    

    When I use Insert(0, new Car) then because of second row whitch has value the AllowEdit was false; So I remove else code and it works

    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;
                    }
                 }        
             }
        }
    

    So finlay I found that bindingSource.Add(object) and bindingSource.Insert(0,object) is same!

    I apologize for my english!!