0

I used LINQ to load a float array from DB. Even though I succeeded in loading the array, I cannot make a DataGridView to show that array.

Here is my code

float[] balances = client.LoadBalance(id); 
// LoadBalance() give me an array of floats of customer who has the specified id
dataBalance.DataSource = balances;

I tried the following, yet they are not helping

dataBalance.Refresh();
dataBalance.Parent.Refresh();

and

dataBalance.DataSource = null;
dataBalance.DataSource = balances;

I added a breakpoint after float[] balances = client.LoadBalance(id); and I am sure that the LoadBalance(id) works. Please help!

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
Minh Triet
  • 1,190
  • 1
  • 15
  • 35

3 Answers3

1

You need dataBalance.DataBind(); after dataBalance.DataSource = balances;

Melanie
  • 3,021
  • 6
  • 38
  • 56
  • This is only for web (ASP.NET), in winform, we don't have this method and it's not neccessary at all. – Andiana Jun 19 '16 at 10:15
1

The solution, just create a Class for your balances

Let say, BalanceClass

public class BalanceClass
{
    public float balances { get; set; }
}

then

float[] balances = client.LoadBalance(id);
List<BalanceClass> bal = new List<BalanceClass>();
foreach (var item in balances)
   bal.Add(new BalanceClass() { balances = item});
ataGridView2.DataSource = bal.ToList();
spajce
  • 7,044
  • 5
  • 29
  • 44
  • It works! Can you please enlighten me why? Moreover, you do not need `bal.ToList()` since `bal` is a List, I deleted the `ToList()` and it stills works – Minh Triet Jan 25 '13 at 04:38
  • heres a good explanation about [.ToList()](http://stackoverflow.com/questions/2774099/tolist-does-it-create-a-new-list) – spajce Jan 25 '13 at 05:48
  • Thank you, I meant that why creating a new class put these values to my DB, while leaving them as a `List` does not work? – Minh Triet Jan 26 '13 at 03:01
0

You can do something like this.

     dataGridView1.DataSource = 
            (LoadBalance(id) ?? new float[0])
                .Select(x => new { Number = x })
                .ToList();
TYY
  • 2,702
  • 1
  • 13
  • 14