使用winforms将gridview数据(一个页面)传递给另一个页面'

本文关键字:另一个 一个 winforms gridview 数据 使用 | 更新日期: 2023-09-27 18:19:17

我的windows应用程序中有两个页面。两个页面都有一个gridview控件。我可以传递文本框值到第二形式,但我的问题是,我不知道如何传递整个gridview数据到第二形式。

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();
    }   
}

当我点击PrintBill按钮时,我想显示第二个表单与第一个表单的gridview值…

使用winforms将gridview数据(一个页面)传递给另一个页面'

作为一个很好的选择,您可以使用DataTable作为您的DataGridView的数据源,当您需要将数据传递到其他形式时,使用该DataTableCopy方法。下面是一个例子:

你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();
    }
}
你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;
    }
}

实际上,您可以将该datarow/DataTable设置为全局变量,以便其他类可以访问它。此外,您还可以创建一个返回数据表的公共方法,例如:

public DataTable RetrieveData()
{
     DataTable dt;
//retrive necessary data here
    return dt;
}