I have one window form in which I take a two textboxes for entering data. On other window form I take on gridview control. When I press save button of first form then data should be save in sql server table and display in gridview column of other form. How Can I pass textbox value of one form to other form and display it in gridview control?
Thanks in advance
Dorababu MekaPosted Jan 9, 2013, 8:17 AM
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;
using System.Data.SqlClient;
namespace dynamicData
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
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('" + textBox1.Text + "','" + textBox2.Text + "')", 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);
Form2 frm = new Form2();
frm.ds = ds1;
frm.Show();
}
}
}
Place a datagriview and code as follows in Form2
public partial class Form2 : Form
{
public DataSet ds { get; set; }
public Form2()
{
InitializeComponent();
}
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";
}
}
Check my attachment
Nilesh AvhadPosted Jan 10, 2013, 12:37 AM