Page 1 of 1

share a query result between 2 c# forms

Posted: Sat 26 Jul 2014 09:18
by skarosg3
Hi all,
I have a problem that seems to be over my skills.
As part of an application that i am creating in c#, i want to present the contact information of the selected customer. The thing is that i want to have multiple search criteria, resulting the possibility of returning more than one customers. So i was thinking to open a new form only if the query returns more than 1 rows. If it only returns 1 row the data will be printed on the main form, so there is nothing to do there.
My problem is how to transfer the query result to the second form. I was thinking that the easiest thing is to send the query to be re executed on the secondary form, but that would be a wast of time/network traffic.
Alternative i was thinking to use an array list to store the query result and pass that to the secondary form. But i am not sure how to do that.

All ideas are welcomed and highly appreciated.
Thanos in advance,
Ilias

Re: share a query result between 2 c# forms

Posted: Wed 30 Jul 2014 14:27
by Pinturiccio
You can use the InterForm technology in order to pass the query results to the second form.

1. Suppose you have the "dt" variable of the PgSqlDataTable type on the first form, and you have just read data to it.
2. Then you create a second form.
3. On the second form you need to create a variable of the DataLink type.

Here is the example of using this technique:
Form1:

Code: Select all

public partial class Form1 : Form
{
    PgSqlConnection conn;
    PgSqlDataTable dt;

    public Form1()
    {
        InitializeComponent();
        conn = new PgSqlConnection("host=db;port=5439;uid=postgres;pwd=postgres");
        dt = new PgSqlDataTable("select * from dept", conn);
        dt.Name = "dt";
        dt.FetchAll = true;
        dt.Owner = this;
        dt.Active = true;

        Form2 form = new Form2();
        form.Show();
    }
}
Form2:

Code: Select all

public partial class Form2 : Form
{
    DataLink link;
    public Form2()
    {
        InitializeComponent();
        link = new DataLink();
        this.link.DataSource = ((object)(Devart.Common.GlobalComponentsCache.GetObjectByName("Form1.dt")));
        dataGridView1.DataSource = link; // The data grid on Form2
    }
}