0

C#, .Net 4, VS2010

I have an object that contains multiple arrays of doubles (recently made them into Lists). It looks something like:

public class Channel
{
    public List<DateTime> Date = new List<DateTime>();  //DateTime
    public List<double> Val1 = new List<double>();      //
    public List<double> Val2 = new List<double>();      //
    public List<double> Val3 = new List<double>();      //
    public List<double> Val4 = new List<double>();      //
}

So now I'm trying to populate a DataGridView with a DataSource, but I only care about Date, Val1, and Val2. How can I achieve this?

Currently I'm looping through the arrays and adding them row by row to the DataGridView. But this is really slow as I have a huge file of data to work with. Isn't there an easy way to just bind my arrays and populate the grid? I can't seem to figure it out.

Thanks

Code I've tried:

Channel ch = new Channel();

List<object> datasource = new List<object>();

dataGridView1.Rows.Clear();
dataGridView1.Columns.Clear();

datasource.Add(ch.spotsList[0].Date);
datasource.Add(ch.spotsList[0].Val1);
datasource.Add(ch.spotsList[0].Val2);

dataGridView1.DataSource = datasource;

Code I'm using that works but is really slow:

for (int i = 0; i < ch.spotsList[0].Date.Count; i++)
{
    dataGridView1.Rows.Add(ch.spotsList[0].Date[i].ToString("yyyy-MM-dd HH:mm:ss"), ch.spotsList[0].Val1[i].ToString("#.##"), ch.spotsList[0].Val2[i].ToString("#.##"));
}
Baddack
  • 1,947
  • 1
  • 24
  • 33
  • fyi there is a difference between a List and an array. anyway do a simple google search on how to bind List to a datagridview this is a very simple task btw.. and there are plenty existing examples on `Stackoverflow as well as the rest of the internet` - example http://stackoverflow.com/questions/6473326/using-a-list-as-a-data-source-for-datagridview – MethodMan Apr 12 '16 at 22:09
  • Right, they were arrays and I made them Lists to be dynamic. I have googled examples, but it only works for a single list. I can't seem to find a way to bind multiple List to a datagridview (one for each column). – Baddack Apr 12 '16 at 22:12
  • perhaps you should show all relevant code where you have tried to bind the datagridview to a List – MethodMan Apr 12 '16 at 22:15
  • How about copying all the individual lists to one single list then adding that new list to your datagridview? http://stackoverflow.com/questions/4488054/merge-two-or-more-lists-into-one-in-c-sharp-net – David Oesterreich Apr 12 '16 at 22:16
  • Hmm, good idea. I just tried this: {List datasource = new List(); datasource.Add(ch.spotsList[0].Date); datasource.Add(ch.spotsList[0].Val1); datasource.Add(ch.spotsList[0].Val2); dataGridView1.DataSource = datasource;} but it gave me errors about being wrong type. – Baddack Apr 12 '16 at 22:21
  • Hey @Baddack, what error did you get? Did the error occur when you tried to set `dataGridView1.DataSource = datasource` in your example? – AGB Apr 12 '16 at 22:34
  • @AGB, I got the following: http://i.imgur.com/XMd9toc.png and it added "Count" and "Capacity" to my headers and put some numbers in. – Baddack Apr 12 '16 at 22:37
  • I think somewhere your `dataGridView1`is expecting a `List` the list it has includes an object of a type it did not expect--are you sure you wanted to add the `DateTime` instances to the `datasource` via `datasource.Add(ch.spotsList[0].Date);` ? – AGB Apr 12 '16 at 22:43
  • Thank you for adding the code that is working. That helps! – AGB Apr 12 '16 at 22:44
  • Yeah, I want the datetime for when I got val 1 and val 2. I guess I could try making them all strings. Let me try that. edit: problem is it is a list, I'd have to go through each value and make them a string. – Baddack Apr 12 '16 at 22:44

2 Answers2

2

DataGridViews aren't really designed for data models structured by column (such as yours). Rather, they are meant to be used with models that represent a row. It seems like your edit indicates that you are starting to think along those lines. In particular, hopefully your new data model is something like

public class Channel
{
  private _listings = new List<SpotsList>();

  public IList<SpotsList> SpotsList { get { return _listings; } }
}

public class SpotsList
{
  public DateTime Date { get; set; }
  public double Val1 { get; set; }
  public double Val2 { get; set; }
  public double Val3 { get; set; }
  public double Val4 { get; set; }
}

In which case, you should be able to load this into your DataGridView via the call

Channel ch = new Channel();
// Add data to the channel
dataGridView1.DataSource = ch.SpotsList;

Edit

To try and more clearly communicate what I am thinking, the code below should help you map your approach to my approach.

public class DataPoint
{
  public DateTime Date { get; set; }
  public double Val1 { get; set; }
  public double Val2 { get; set; }
}

// ... your other code ...

var dataSource = new List<DataPoint>();
for (int i = 0; i < ch.spotsList[0].Date.Count; i++)
{
  dataSource.Add(new DataPoint() 
  { 
    Date = ch.spotsList[0].Date[i],
    Val1 = ch.spotsList[0].Val1[i],
    Val2 = ch.spotsList[0].Val2[i]
  });
}
dataGridView1.DataSource = dataSource;
erdomke
  • 4,980
  • 1
  • 24
  • 30
  • Thanks for the response, I think you are right. I think the problem is, each of my spotList has N amount of datetime's, val1's, val2's, etc. So I think the approach for a datasource might not be feasible. I might just have to stick to my for loop. Thanks – Baddack Apr 12 '16 at 22:52
  • I was just trying to think about it like mscharts. In ms charts I can databind my x and y for each series, which is basically what I'm trying to do here, I have 3 vals that share a datetime, so I'm trying to bind all my values to this datagridview. I'm playing with datasets and datatables, to see if there is a way, but haven't figured it out yet. – Baddack Apr 12 '16 at 22:59
  • I understand where your mind is. Unlike MsCharts, `DataGridViews` work better with lists of points {x,y,z} than with individual lists of x, y, and z values. Hopefully my edit to my answer will help give some further inspiration to that approach. – erdomke Apr 12 '16 at 23:06
  • Thanks, this helped a lot. – Baddack Apr 12 '16 at 23:14
0

You can implement the ITypedList interface. This way you can control the generated columns.