0

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!

A. Zalonis
  • 1,599
  • 6
  • 26
  • 41
  • what is this line word means??? Now you have written your question well but still not clear what you asking.. – Niranjan Singh Dec 03 '15 at 17:10
  • You have right @NiranjanKala. Actualy I want to add a new row in my carsBindingSource BUT in the first row of grid. I use carsBindingSource.Insert(0,newCar) but did not work correctly – A. Zalonis Dec 04 '15 at 08:43
  • 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;). .Add(object), .Insert(0,object) is same! – A. Zalonis Dec 04 '15 at 11:38

2 Answers2

1

If you haven't already, consider using an intermediate list for your car types:

private List<CarTypes> _CarTypes;

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

And then in the form load event, be sure this is bound to the data source:

repositoryLookupItemCarTypes.DataSource = _CarTypes;

With this, the grid should now automatically manage the instantiation and selection of the CarType object for each Cars object. You can omit this line when you add a car to the grid:

newCar.CarType = new CarType(); 

In the designer, I think it helps to alter the DisplayMember Property of the repository Item.

With this setup, any Car added to your grid should automatically have the CarType as a populated Lookup Edit.

If any of this is unclear, let me know. I did a quick and dirty solution to test this, and I obviously can't post it all, but I can tell you it did work with both Add and Insert.

Hambone
  • 15,600
  • 8
  • 46
  • 69
  • thank you for your answer. But I have already all this. With BindingSource.Add(newCar) works fine but the problem is the new line goes in the end row of grid and I want to add in first row. With Insert(0,newCar) it goes to first row but I must to click in other columns a lot of times till reload data in lookupedit repository, I try bindingSource.AddNew() command with Insert and now added 2 rows. One in first row and a row in end of grid but now works! I can't understand what happens – A. Zalonis Dec 04 '15 at 08:41
  • Well, I'm glad it works, at least. For what it's worth, you can add the binding source at design time (on the form) rather than within the code. Maybe you did that and only showed the code to illustrate it, but just in case... – Hambone Dec 04 '15 at 12:00
1

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!!

A. Zalonis
  • 1,599
  • 6
  • 26
  • 41