plz help.
Set focus to another form
I have two forms. One is for enter data and other is to show that data in the gridview. Scenario is that when I press save button of one form which is for to enter data in table then it get saved into table and form get closed and that data is get binded to the gridview control of other form which only shows that data. When button save is pressed I want to show the form i.e. form on which gridview is present to show data and the form which is for enter data is should be get closed. How can I do that?
Dorababu MekaPosted Jan 10, 2013, 8:36 AM
private void button1_Click(object sender, EventArgs e)
{
System.Threading.Thread mythread = new System.Threading.Thread(new System.Threading.ThreadStart(OpenNewFrom));
mythread.Start();
FirstName = textBox1.Text;
LastName = textBox2.Text;
this.Close();
}
public static void OpenNewFrom()
{
SqlConnection con = new SqlConnection("Data Source=SYSTEM19\\SYSTEM19;Initial Catalog=linqDB;Persist Security Info=True;User ID=sa;Password=reddy@123");
SqlCommand cmd = new SqlCommand("insert into Employee values('" + FirstName + "','" + LastName + "')", con);
con.Open();
cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
con.Close();
cmd = new SqlCommand("Select * from Employee", con);
con.Open();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds1 = new DataSet();
da.Fill(ds1);
Application.Run(new Form2(ds1));//Create an instance of your new form. No need to call show method.
}
And your Form2 as per earlier code remove Form_Load code and add this
public Form2(DataSet ds1)
{
InitializeComponent();
dataGridView1.AutoGenerateColumns = true;
dataGridView1.DataSource = ds1;
dataGridView1.DataMember = "Table";
}
Final look of Form2
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace dynamicData
{
public partial class Form2 : Form
{
public DataSet ds { get; set; }
public Form2(DataSet ds1)
{
InitializeComponent();
dataGridView1.AutoGenerateColumns = true;
dataGridView1.DataSource = ds1;
dataGridView1.DataMember = "Table";
}
private void button1_Click(object sender, EventArgs e)
{
}
private void Form2_Load(object sender, EventArgs e)
{
//dataGridView1.AutoGenerateColumns = true;
//dataGridView1.DataSource = ds;
//dataGridView1.DataMember = "Table";
}
}
}
Dorababu MekaPosted Jan 11, 2013, 4:16 AM
Nilesh AvhadPosted Jan 11, 2013, 2:10 AM
Dorababu MekaPosted Jan 10, 2013, 8:21 AM
http://www.c-sharpcorner.com/Forums/Thread/198513/C-Sharp-win-forms.aspx