0
votes

I have a an issue in my windows application project, a very small one I'm afraid which for some reason I can't get a hang of the solution.

I want to display data in a Data grid programmatically, but it returns only the rows based on the number of records fetched from the database WITHOUT Data in it. Below is my code

private void BindDataToGrid()
{
    var teller = new TellerService().GetByUsername(username);
    var transactions = new PocTransactionService().GetByTellerId(teller.TellerId);
    BindingList<PocTransaction> trans = new BindingList<PocTransaction>(transactions);
    var source = new BindingSource(trans, null);
    dgvTransactions.DataSource = source;
}

Not that the variable transactions gets populated in the code, but for some reason, it's not displaying on grid. It's really driving me nuts. I trust one of you here to show me the way out.

1
why not var source = new BindingSource(trans);? - Gabriel GM
BindingSource doesn't take less than two arguments Gabriel. But thanks for your response. - Kacey
Second parameter (DataMember) default value is "". Maybe that will help.. When working with BindingSource, I usually use the default constructor, then set the DataSource. - Gabriel GM
Still didn't work Gabriel. Can you share the code you use to make it work? - Kacey
Ohh.. and I didn't see before, but you're using BindingList AND BindingSource.. try binding the BindingList directly : dgvTransactions.DataSource = trans;. - Gabriel GM

1 Answers

0
votes

I finally figured it out. There is nothing wrong with the code I wrote in the first place. I just didn't set the DataPropertyName for each of the columns I defined on the grid. Setting them appropriately to the datasource property gave my the desired result.

All thanks to Gabriel for his help so far. I appreciate.