Today we will discuss about Pass TextBox value into another form DataGridview using C#.Net Language.
Here we used 2 Class Files and Two Form.
2 Class Files are ClsAddintoiTable and ClsBL.
Two forms are Form1 and Form2.
Class Name: ClsAddintoiTable
Class Name: ClsAddintoiTable
namespace DatagridviewTasks
{
class ClsAddintoiTable
{
DataTable dt;
public DataTable Addintotable(ClsBL cls)
{
dt = new DataTable();
dt.Columns.Add("First Name");
dt.Columns.Add("Last Name");
DataRow dr; dr = dt.NewRow();
dr["First Name"] = cls.FirstName;
dr["Last Name"] = cls.LastName;
dt.Rows.Add(dr.ItemArray);
return dt;
}
}
}
Class Name: ClsBL
namespace DatagridviewTasks
{
class ClsBL
{
private static string _FirstName;
private static string _LastName;
public string FirstName
{
get { return _FirstName; } set { _FirstName = value;
}
}
public string LastName
{
get { return _LastName; } set { _LastName = value;
}
}
}
}
Form 2
private void button1_Click(object sender, EventArgs e)
{
cls.FirstName = textBox1.Text;
cls.LastName = textBox2.Text;
clsAdd.Addintotable(cls);
this.Close();
}
public DataTable CopyValue()
{
DataTable dt1 = new DataTable();
dt1 = clsAdd.Addintotable(cls).Copy();
return dt1;
}
Form 1
DataTable dt;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
dt = frm.CopyValue();
dataGridView1.DataSource = dt;
}
private void button2_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.ShowDialog();
}