Click here to Skip to main content
15,917,618 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a two different form.one form has one textbox and one button and another form has datagrid. if you write a id for ex:12345 and click the button all information comes a database and form fill.


I want to do this:if ı search a person with id and click the button another form open and this person's information fill in datagrid.
Posted

In the constructor of your second form (the one with the datagrid on it) add a variable.

Then use this variable to load the data into the datagrid.

You can then pass the 'id' from form one to form two by passing it in the constructor.

Pseudo code (from memory)
form 2 constructor
C#
public form2(int id)
    {
        InitializeComponent();
//load data here according to id variable
    }


calling form2

C#
form2 frm = new form2(12345);
       frm.show();
 
Share this answer
 
On your first form, a button handler to get the data from the text box & open the second form

C#
private void buttonHandler_Activate(object sender, System.EventArgs e)
{
    // Assuming you've done some sort of validation
    int customerId = Convert.ToInt32(this.personTextBox.Text);

    // Assuming your second form is called 'Gridform'!
    using (GridForm grid = new GridForm ())
    {
        grid.GetCustomerDetails(customerId);

        // Doesn't have to be a dialog here
        grid.ShowDialog(this);
    }
}


On your second form (GridForm), a public method that takes the customer id and populates the screen.

C#
public void GetCustomerDetails(int customerId)
{
   // Some sort of datbase operation here to get the data
   // This is copy-paste from microsoft 'DataSet' MSDN article

      string cString = "Persist Security Info=False;Integrated Security=SSPI;database=northwind;server=mySQLServer";
      SqlConnection myConnection = new SqlConnection(cString);
      // Create a SqlDataAdapter.
      SqlDataAdapter myAdapter = new SqlDataAdapter();
      myConnection.Open();
      SqlCommand myCommand = new SqlCommand("SELECT * FROM MyTable WHERE CustomerId = " + customerId.ToString(),
      myConnection);
      myCommand.CommandType = CommandType.Text;
   
      myAdapter.SelectCommand = myCommand;
      ds = new DataSet("Customers");
      myAdapter.Fill(ds);

// Now you've got data in a dataset, bind it to your grid on this form
    this.myGridReference.DataSource = ds;
    this.myGridReference.DataBind();
}


Disclaimer - Dodgy data access that I don't endorse at all! But you get the idea

* On you 1st form, validate data entry, create instance of second form, pass collected data to second form

* 2nd form has method to receive entry and populate itself
 
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