Reputation: 69
I have two pages in my windows application. Both pages have a gridview control. I can pass textbox values into 2nd form but my problem is, I dont know how to pass whole gridview data into 2nd form.
Form1:
public partial class Billing : Form
{
DataTable dt;
public Billing()
{
InitializeComponent();
SetDataTable();
txtBillNo.Text = Convert.ToString(billno);
}
public void btnEnter_Click(object sender, EventArgs e)
{
int row = dgvBilling.RowCount;
DataRow dr;
if (dgvBilling.RowCount != 0)
{
dr = dt.NewRow();
dr["IName"] = txtIName.Text.Trim();
dr["Icode"] = txtIcode.Text.Trim();
dr["Quantity"] = txtQuantity.Text.Trim();
dr["UnitPrice"] = txtUnitPrice.Text.Trim();
int amount = Convert.ToInt32(txtQuantity.Text.Trim()) * Convert.ToInt32(txtUnitPrice.Text.Trim());
dr["amount"] = Convert.ToString(amount);
dt.Rows.Add(dr);
int total = 0;
for (int i = 0; i < dgvBilling.Rows.Count; ++i)
{
total += Convert.ToInt32(dgvBilling.Rows[i].Cells[5].Value);
txtTotal.Text = "Rs/" + "" + Convert.ToString(total);
}
}
else
{
//dt = SetDataTable();
dr = dt.NewRow();
dr["IName"] = txtIName.Text.Trim();
dr["Icode"] = txtIcode.Text.Trim();
dr["Quantity"] = txtQuantity.Text.Trim();
dr["UnitPrice"] = txtUnitPrice.Text.Trim();
int amount = Convert.ToInt32(txtQuantity.Text.Trim()) * Convert.ToInt32(txtUnitPrice.Text.Trim());
dr["Amount"] = Convert.ToString(amount);
dt.Rows.Add(dr);
int total = 0;
for (int i = 0; i < dgvBilling.Rows.Count; ++i)
{
total += Convert.ToInt32(dgvBilling.Rows[i].Cells[5].Value);
txtTotal.Text = "Rs/" + "" + Convert.ToString(total);
}
RetrieveData();
}
}
public DataTable SetDataTable()
{
dt = new DataTable();
DataColumn dc = new DataColumn("IName", typeof(string));
dt.Columns.Add(dc);
dc = new DataColumn("Icode", typeof(string));
dt.Columns.Add(dc);
dc = new DataColumn("Quantity", typeof(string));
dt.Columns.Add(dc);
dc = new DataColumn("UnitPrice", typeof(string));
dt.Columns.Add(dc);
dc = new DataColumn("Amount", typeof(string));
dt.Columns.Add(dc);
DataGridViewCheckBoxColumn checkBoxColumn = new DataGridViewCheckBoxColumn();
checkBoxColumn.HeaderText = "";
checkBoxColumn.Width = 50;
checkBoxColumn.Name = "checkBoxColumn";
dgvBilling.Columns.Insert(0, checkBoxColumn);
dgvBilling.DataSource = dt;
return dt;
}
private void btnPrintBill_Click(object sender, EventArgs e)
{
PrintBill pb = new PrintBill();
pb.Controls["lblNetAmount"].Text = txtTotal.Text;
pb.Show();
}
}
}
form2:
public partial class PrintBill : Form
{
public PrintBill()
{
InitializeComponent();
Billing bg = new Billing();
dataGridView1.DataSource = bg.RetrieveData();
}
}
When I click PrintBill button I want to show 2nd form with the gridview values of first form...
Upvotes: 1
Views: 2541
Reputation: 125207
As a good option you can use a DataTable
as data source of your DataGridView
and when you need to pass data to other form, use Copy
method of that DataTable
. Here is an example:
Your GridForm:
public partial class GridForm : Form
{
public GridForm()
{
InitializeComponent();
}
//we will this as data source
private DataTable table;
private void GridForm_Load(object sender, EventArgs e)
{
//Create the data table here and bind it to grid
table = new DataTable();
table.Columns.Add(new DataColumn("IName", typeof(string)));
table.Columns.Add(new DataColumn("Icode", typeof(string)));
table.Columns.Add(new DataColumn("Quantity", typeof(string)));
table.Columns.Add(new DataColumn("UnitPrice", typeof(string)));
table.Columns.Add(new DataColumn("Amount", typeof(string)));
this.dataGridView1.DataSource = table;
}
private void passDataButton_Click(object sender, EventArgs e)
{
//When you need to pass data, use Copy method of data table
var clone = table.Copy();
//Pass data to other form
var f = new OtherForm(clone);
f.ShowDialog();
}
}
Your OtherForm:
public partial class OtherForm : Form
{
DataTable table;
public OtherForm(DataTable value)
{
InitializeComponent();
//get passed data and put it in some member variable for next usages
table = value;
//Bind data to other grid or do other things here or in Form Load event
this.dataGridView1.DataSource = table;
}
}
Upvotes: 1
Reputation: 177
Actually, you can make that datarow/DataTable a global variable to make it accessible to other class. And also, you can create a public method that returns a datatable like :
public DataTable RetrieveData()
{
DataTable dt;
//retrive necessary data here
return dt;
}
Upvotes: 1