Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Fellows

I'm having a tough time trying to display my data information in another form which has a DataGridView, if a button is clicked

To display the results in the same Form in dgv isn't a problem but to display it in Form 2 in a dgv is giving me a headache (:

Example:
Form1
cmd.commandText: SELECT * FROM tblproducts

void btnDisplay_Click(object sender, EventArgs e)

if (comboBox1.SelectedValue.ToString() != null && comboBox2.SelectedValue.ToString() != null)

try
open connection
new Form2().ShowDialog();

?????? what next??


Form2

dataGridView1.DataSource = get Info from Form1;

Can sb help or any examples pleeaaaaseeee?
Posted
Comments
BillWoodruff 19-Oct-14 15:59pm    
You are going to have to find a way to make the Data in the DGV on your first Form available to the DGV on the second Form. Have you tried to implement this in any way ? Do you have a DataSource that you can bind to ?

See if this gives you some ideas:

http://stackoverflow.com/questions/2852167/c-sharp-same-datasource-multiple-datagridviews-data-binding-issues

Why don't you add a static class to your project, populate this in Form1 then add a property to the static class holding your list of objects, then bind this to your datagridview in Form2??
 
Share this answer
 
v2
Comments
mikybrain1 19-Oct-14 16:30pm    
jaa. I've tried it but it isn't working

Form1
open connection
new Form2().ShowDialog();
Form2.Passvalue = dataGridView1.DataSource;


public partial class DataGridView : Form2
{

private string Data;

public string Passvalue
{

get { return Data; }
set { Data = value; }

}

public DataGridView()
{
InitializeComponent();
}

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
dataGridView1.DataSource = Data;
}
frostcox 19-Oct-14 16:45pm    
Ok just place a property in Form2 like List<object> ListOfObjects and make it public, then in Form1 add your datasource to this property and then bind to your grid??
mikybrain1 19-Oct-14 16:49pm    
can u please edit my code if neccessary? am somehow absent in mind
The basic idea is to pass some data, any data. Whether you populate that in a datagridview or a textbox shouldn't really matter. I have never really worked with windows forms but this will definitely give you some ideas.

Try Passing Data Between Forms

Keep in mind that you might need to populate a lot of data in the second form but that doesn't mean you have to send all that from the first form. Like if you are fetching records from a DB, send the primary key from form1 to form2 and in form2, fetch all the records using that pkey.
Hope this helps.

Warm regards,
Subho
 
Share this answer
 
Ok just add a second constructor to Form 2 and populate it from there..... much easier

C#
public partial class Form2 : Form
 {
     string _passValue;
     public string Passvalue
     {
         get { return _passValue; }
         set { _passValue = value; }
     }

     public Form2()
     {
         InitializeComponent();
     }

     public Form2(string Data)
     {
         _passValue = Data;
     }
 }


C#
public partial class Form1 : Form
 {
     public Form1()
     {
         InitializeComponent();
         Form2 _form2 = new Form2(dataGridView1.DataSource);
         _form2.ShowDialog();

     }
 }
 
Share this answer
 

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