Pergunta

I'm wondering how to solve/simplify a task that sometimes pop-ups during developement with DevExpress XtraGrid suite. Sometimes i fall into this case: Given these 2 classes that represents the model classes that comes from the Business Logic layer:

 public class Customer
    {
        public Int32 CustomerId { get; set; }
        public String Name { get; set; }
        public String Address { get; set; }
        public List<Order> Orders { get; set; }
    }

    public class Order
    {
        public Int32 OrderId { get; set; }
        public String ItemCode { get; set; }
        public Int32 Quantity { get; set; }
        public Decimal Price { get; set; }
        public DateTime Date { get; set; }
    }

I'd like to create a simple window that shows an XtraGrid that allows edit/add/remove a list of customers and its nested orders. In order to do that, i've created a simple form with a GridControl and a GridView controls with the AllowAddRow and AllowDeleteRow properties == true. Then, in Form1 class i've done the following:

//List of my customers
private List<Customer> _customers;

public Form1()
{

    //Initialize UI components
    InitializeComponent();

    //Call the provider in order to get customers 
    CustomerProvider cp = new CustomerProvider();
    _customers = cp.GetCustomers();

    //Initialize bindingSource
    BindingSource bs = new BindingSource();
    bs.DataSource = _customers;

    //Set GridControl's dataSource
    gridControl1.DataSource = bs;
}

Now i've got beautiful GridControl that shows the content of my List.

enter image description here

But now here's the problem...how can i add or delete row? Infact:

  • If i set the focus on a row and i press "Delete" key, it doesn't work.

  • If i try to add a new row, when it looses focus, it suddently disappears.

Obviously i'm missing something. Unfortunately i found the DevExpress documentation a quite confusional (in my opinion) about this kind of argument and the best practises, so i can't reach my goal.

Someone can help me?

PS. This is the hyperlink for the .csproj of my sample.

Foi útil?

Solução

Set the gridControl1.UseEmbeddedNavigator property to true. You'll get a data navigator widget at the bottom of your view.

That only gets you halfway there, however. If you want to be able to add Orders to a Customer from the grid, your Customer class needs to use BindingList instead of List for the Orders property.

See here for more information. If you find the documentation lacking, you can also find helpful resources at the Support Center.

EDIT: Some other options to consider are:

  1. Bind your grid to a database.
  2. Bind to XML data and use a DataSet.

If you're already storing your data in a database, then option 1 would be the way to go. If you're not persisting your data anywhere yet, you could go either way.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top