Click here to Skip to main content
16,022,335 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to send datagridview multiple cell values from one form to another form with datagridview when both forms are open. The get set works for only one string/ value.
In other words I want to go through the datagridview in form 1 and pick a cell value based on the conditions and pass it to another open form 2.

What I have tried:

I've tried to use the set accesor only but get an error data cannot be used in this context because it lacks the get accessor.

Public string Data
{
Set
{
foreach (DatagridviewRow row in datagridview2.Rows)
row.cells["scale"].Value = value;
}
}
Posted
Updated 27-Jan-19 20:51pm

Getter and setter methods don't "work for only one string/value" - they work for one instance of the class type for which the property is declared.
So a string property gets and sets a single string:
C#
public string MyStringProperty
   {
   get { return myString; }
   set { myString = value; }
   }
and an integer property works with a single integer:
C#
public int MyIntegerProperty
   {
   get { return myInt; }
   set { myInt = value; }
   }
But the type can be anything! A custom class:
C#
public MyClass MyClassProperty
   {
   get { return myClass; }
   set { myClass = value; }
   }

A DataTable:
C#
public DataTable MyDataTableProperty
   {
   get { return myDataGridView.DataSource; }
   set { myDataGridView.DataSource = value; }
   }
A List of strings:
C#
public List<string> MyStringListProperty
   {
   get { return myStringList; }
   set { myStringList = value; }
   }
If you want to pass multiple values, just package them in an appropriate collection, and create a property that gets and sets that collection...
 
Share this answer
 
v2
Comments
Robert S4r 28-Jan-19 4:17am    
I'm finding hard time packaging and setting the collection. DataTable seems to be the best option, but it throws an error
Cannot implicitly convert type object to system. Data.DataTable. an explicit conversion exists....

{
get {return datagridview1. DataSource;}
set {datagridview1.DataSource = value;}
}

Any help please!
OriginalGriff 28-Jan-19 4:27am    
What did you declare the property type as?
Robert S4r 28-Jan-19 4:41am    
public DataTable dataTableF
OriginalGriff 28-Jan-19 4:49am    
Ah. Try this:
get {return datagridview1. DataSource as DataTable;}
Robert S4r 28-Jan-19 5:10am    
This is an extract of what I've tried.(No results yet)

SOURCE DATAGRIDVIEW - FORM 2
--------------------------------------------------
public partial class Form2 : Form{
MySqlDataAdapter daStoreC = new MySqlDataAdapter(); DataSet dsStoreC = new DataSet();
public Form2()
{InitializeComponent();}

private void Form2_Load(object sender, EventArgs e)
{LOAD_STORE_CONSUMPTION();}
public DataTable MyDataTableProperty
{get { return dataGridView2.DataSource as DataTable; }set
{ dataGridView2.DataSource = value; }}

public event EventHandler DataAvailable;
protected virtual void OnDataAvailable(EventArgs e)
{EventHandler eh = DataAvailable;
if (eh != null)
{ eh(this, e); }}

private void button1_Click(object sender, EventArgs e)
{OnDataAvailable(null);}

private void LOAD_STORE_CONSUMPTION()
{Connectionss.S_DB();
string sqlStoreC = "SELECT * FROM store_stock";
try{
MySqlConnection ConnStoreC = new MySqlConnection(Connectionss.cnString);
ConnStoreC.Open();
MySqlDataAdapter daStoreC = new MySqlDataAdapter(sqlStoreC, ConnStoreC);
daStoreC.Fill(dsStoreC, "store_stock");
dataGridView2.DataSource = dsStoreC;
dataGridView2.DataMember = "store_stock";
ConnStoreC.Close();
ConnStoreC.Dispose();
}catch (Exception ex){MessageBox.Show(ex.Message);}
}}
//===========================
DESTINATION DGV - FORM 1

public partial class Form1 : Form{

public Form1() {InitializeComponent();}

private void Form1_Load(object sender, EventArgs e)
{LOAD_STORE_CONSUMPTION();}
void Form2_DataAvailable(object sender, EventArgs e)
{
Form2 form2 = sender as Form2;
if (form2 != null){
for (int i = 0; i <= dataGridView1.Rows.Count - 1; i++)
{dataGridView1.Rows[i].Cells[2].Value = form2.MyDataTableProperty;
}}
}

private void LOAD_STORE_CONSUMPTION()
{........................}

private void button1_Click(object sender, EventArgs e){
Form2 form = new Form2();
form.DataAvailable += new
EventHandler(Form2_DataAvailable);
form.Show();
}}


(BOTH FORMS ARE OPEN)
See example here: Passing data between two Forms in WinForms[^]

You might also be interested in data binding, which is the recommend way, see the part about filtering for a second form in this excellent article: A Detailed Data Binding Tutorial[^]
 
Share this answer
 
v2
Comments
Robert S4r 28-Jan-19 3:18am    
For this case, its as if form 2 has no datagridview.
RickZeeland 28-Jan-19 3:39am    
Are you referring to the data binding example ?
See Griffs solution on how to create a suitable property.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900