Reputation: 1304
I am having difficulties in passing the values of a DataGridView to a class. I want to read all contents of the datagridview of a form using a separate class. I tried the following (suppose dgv1 is an existing dataGridView with contents):
public void buttonClick()
{
SeparateClass separateClass = new SeparateClass();
separateClass.getDataGridViewDetails(dgv1);
}
and the following code receives dgv1 in a separate class:
public void getDataGridViewDetails(DataGridView dgv1)
{
string celValue = dgv1.Rows[0].Cells[0].Value.ToString();
}
but returns a null value, which means I have failed to pass the value of the existing DataGridView to another class. Can you tell me what is the correct code to pass the value of a DataGridView to another class. Thanks a lot. . .
Upvotes: 1
Views: 3982
Reputation: 1533
In cases like this I would access the DataGridView from my Class using the following..
In your form you would have..
public void buttonClick()
{
SeparateClass separateClass = new SeparateClass();
seperateClass.formObj = this;
seperateClass.getDataGridViewDetails();
}
In your class you would have..
public Form1 formObj;
public void getDataGridViewDetails()
{
string celValue = formObj.dataGridView1.Rows[0].Cells[0].Value.ToString();
}
Also, as mentioned in the comments above, you would need to set the DataGridView Modifier to Public
Upvotes: 2