Popular Datagrid Combobox Windows Forms C#

Asked

Viewed 241 times

1

I have a datagrid(c# windows Forms) that I need to use for editing and I am populating with information coming from the database of a table called "costs" inserir a descrição da imagem aqui

The Account Plan and Cost Center fields are filled in with information stored in the "costs" table. I have two other tables (Planoconta and Cost Center). My idea is to click on account plan or cost center and bring the data from the respective tables. I set the field according to image: inserir a descrição da imagem aqui

But when clicking on the field it does not bring me the data from the Planocontas table. inserir a descrição da imagem aqui I appreciate the help!

  • You have checked whether your planContasBindingSource is being populated?

  • Hello Hello late, this yes... :(

  • How do you populate it? If it is populated after it has been assigned to DataSource combo you need to call the method ResetBindings(if I’m not mistaken).

2 answers

0

Using BindingSource would need to create one for each Combobox. One planoContasBidingSource and another centroCustoBidingSource, load them with the data and configure them to be the DataSource of your respective combobox, as you did in the image. Missing load data, so I understood.

For this configuration to work it is necessary to popular the BindingSource with data from your tables. If you post the methods you use to perform queries it is easier for us to help you! Try to return IList<T> of your database query methods to be able to assign results to Bidingsource more easily.

0


I managed to solve! I am populating via code as below:

        FinanceiroDbContext db = new FinanceiroDbContext();

        PlanoConta.ValueMember = "Nome";
        PlanoConta.DisplayMember = "Nome";
        PlanoConta.DataSource = db.PlanoContas.ToList();
        CentroCusto.ValueMember = "Nome";
        CentroCusto.DisplayMember = "Nome";
        CentroCusto.DataSource = db.CentroCusto.ToList();
        GridRateio.DataSource = db.ExtratoCentroCusto.Where(x => x.NControl == ids).ToList();

What I did, was popular the combobox before popular the datagrid! Thank you all for your help!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.