0

I've got a situation in which I want to populate a DataGridView from a list of arrays. Each row contains the values of a single array. The size of the arrays can change dynamically due to user interaction, but all the arrays in the list always have identical sizes.

I can dynamically adjust the columns of the DataGridView instance to reflect the size of the arrays, and I simply name the columns after the array indices ("1", "2", etc.). But I need to know how to shove the actual data at the DataGridView so it can update its rows.

I understand I can use a BindingSource to act as intermediary between the data source (the list of arrays) and the DataGridView, but I don't know how to set it up to perform the necessary mapping.

Thanks.

1
  • You cannot bind a ListView to multiple arrays. The ListView has only one Source. Commented Sep 26, 2012 at 17:47

1 Answer 1

2

Let's assume you've got a set of arrays like this (it may differ but you should get the idea):

var row1 = new[] { "A", "B", "C" }
var row2 = new[] { "D", "E", "F" }

And so now let's build a bindable object:

DataTable table = new DataTable();
for (int i = 0; i < row1.Length; i++) { table.Columns.Add(i.ToString(), typeof(string)); }
table.LoadDataRow(row1, true);
table.LoadDataRow(row2, true);

And now let's bind the grid:

dataGridView.DataSource = table.DefaultView;
Sign up to request clarification or add additional context in comments.

4 Comments

I appreciate your response. Just a bit o' background: I have used binding with a DataSet as the DataSource and a Table as the DataMember. I've also created an ad hoc type with get/set accessors on it and put instances of it in a list and treated the list as a DataSource for a DGV. But now I'm wanting to take a list of arrays (whose sizes are variable but the same across the entire list at any one time) and shove that into a DGV. I'm wondering if a BindingSource (or some subtype) can act as the binding intermediary for this situation.
For the first 2 methods, I was able to set up the columns of the DGV at design time (and then populate the rows with actual data at run time). With the dynamic arrays, it doesn't seem like design-time (um) design of the DGV is feasible or desirable ...
I did something very similar. Didn't realize the power of setting up a DataTable on the fly having all the desired characteristics (specifically: the column names).
@dweld, I'm glad I could be of assistance and I apologize that I didn't respond to those other two comments. I must have seen the notifications but then got distracted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.