HOW TO DISPLAY DATA RESULTS IN DATAGRIDVIEW IN ANOTHER FORM?

Pravesh Singh

Total Post:412

Points:2888
Posted by  Pravesh Singh
C# 
C#
 1491  View(s)
Ratings:
Rate this:
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;
  1. Ankita Pandey

    Post:183

    Points:1285
    Re: How to display data results in DatagridView in another Form?

    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??


    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;
    }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!