tags:

views:

143

answers:

2

Coding on Visual C# since a few days ago. Trying to access the elements in a DataGrid using the following code

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    textBox2.Text = this.dataGridView1.SelectedCells().Value;
}

Throws the next exception

Non-invocable member 'System.Windows.Forms.DataGridView.SelectedCells' cannot be used like a method.

What's the problem with SelectedCells then? What's the best practice?

EDIT: I guess I'll just convert the member to string and see how it goes.

+2  A: 

Selected Cells is a property not a method.

DataGridView.SelectedCells Property

How to: Get the Selected Cells, Rows, and Columns in the Windows Forms DataGridView Control

astander
I'm still reading the article, very helpful.
agaragaragar
+1  A: 

From Msdn: DataGridView.SelectedCells - Gets the collection of cells selected by the user.

Test to see if SelectedCell.Count = 1 (only one cell was selected) then textBox2.Text = SelectedCells(0).Value

Alternativelly try using the CurrentCell property.

Ando
Thanks, Ando. Do you know if apart from CurrentCell I can use a property to sum up all the integers in a selection? e.g. cell A + cell B + cell C
agaragaragar
There is no property that will automatically do this for you.If you go to http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.selectedcells.aspx you have there an example "The following code example demonstrates how to use the SelectedCells collection to find the sum of the values in the selection"Let me know if this is what you are looking for.
Ando