0

In my form I have this DataGridView (grid1), filled from a sqllite database. enter image description here

I want select the column with the name "cap" and insert the column's values into an array. How can I do?

  1. I don't find a way to select the column cap by name, so I decided to indicate it with the index, but I don't like this way..
  2. I don't know ho to insert these values into an array. there are a lot of columns/cell method but I don't figured out which one can help me!

I tryed to do this way (from an old answer here in the site) but it gives me error of "out of bounded matrix"

     int[] wareCap = new int[grid1.Rows.Count];
       ... 
     //How I filled my dgv, if this can help
    var context = new ForliCesenaEntities2(); 
     BindingSource bi1 = new BindingSource();  
    bi1.DataSource = context.warehouses.ToList();
    grid1.DataSource = bi1;
    grid1.Refresh();

    //How I try to insert the values into int[] wareCap 
    for (int i = 0; i<(grid1.Rows.Count-1); i++)
    wareCap[i] = Convert.ToInt16(grid1.Rows[i].Cells[1].Value);

Thanks in advice

6
  • Which DataSource you are using? Commented Nov 2, 2016 at 11:27
  • Ah ok see it. Sorry. Commented Nov 2, 2016 at 11:27
  • grid1.Rows[i].Cells["columname"] should do for your first point Commented Nov 2, 2016 at 11:28
  • Check your datagridview's rows count, maybe you need to count to grid1.Rows.Count - 2 Commented Nov 2, 2016 at 11:31
  • Don't datagridviews have a header row as well? Commented Nov 2, 2016 at 11:32

3 Answers 3

2

First you gridview has to be filled with values, then:

List<int> intValues = new List<int>();
foreach (DataGridViewRow row in grid.Rows)
{
    intValues.Add(int.Parse(row.Cells["cap"].Value.ToString()));
}

int[] array = intValues.ToArray();

should do the trick.

Alternatively, you could use LINQ.

int[] intArray = dataGridView1.Rows
                .Cast<DataGridViewRow>()
                .Select(row => int.Parse(row.Cells["cap"].Value.ToString())).ToArray();

UPDATE:

The above solution might crash if the DataGridView's AllowUserToAddRows property is set to true. Because in that case your last row is the row for entering a new record, the Value is null, so invoking the ToString method on it causes a NullReferenceException.

I see two possibilities:

  • set your datagridview's AllowUserToAddRows property to false (but the user won't be able to add new rows),
  • use the following solution

Check if the row is the row for entering a new record:

int[] intArray = dataGridView1.Rows
                .Cast<DataGridViewRow>()
                .Where(row => !row.IsNewRow)
                .Select(row => Convert.ToInt32(row.Cells["cap"].Value.ToString())).ToArray();
Sign up to request clarification or add additional context in comments.

3 Comments

Using you linq suggestion give me and error: " Object reference not set to an instance of an object." Using your first suggestion , at "DataRow row" gives me error Additional information: Unable to cast of type 'System.Windows.Forms.DataGridViewRow' object type 'System.Data.DataRow'.
"First you gridview has to be filled with values" I've already filled with the data from database...or I have to do something else to fill the dgv?
You have used DataRow which is used for DataSet and DataTable for data grid view control it must be DatGridViewRow type instead of DataRow.
0

You already got all your values in a List, because you do sth. like this:

bi1.DataSource = context.warehouses.ToList();

You can easily store this in a reference:

var values = context.warehouses.ToList();
bi1.DataSource = values;

Your Grid is build based on Objekts in this List. So this should build your array:

int[] array = new int[values.Count];
int counter = 0;
foreach (var object in values)
{
  array[counter++] = object.CAP;
}

Comments

0
int[] wareCap = new int[grid1.Rows.Count];
foreach(DataGridViewRow row in grid1.Rows)
{
    wareCap.Add(Convert.ToInt32(cell["cap"].Value.ToString()));
}

1 Comment

Error "row.Cells[0]" the instruction foreach can't work with variables o type "system.windows.form.datagridviewcell" because this don't have a public definition for "get enumerator"

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.