使用winforms

时间:2015-10-26 08:38:40

标签: c# winforms datagridview

我的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();

        }
    }
}

窗口2:

public partial class PrintBill : Form
{
    public PrintBill()
    {

        InitializeComponent();
        Billing bg = new Billing();                   
        dataGridView1.DataSource = bg.RetrieveData();

    }   
}

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

2 个答案:

答案 0 :(得分:1)

实际上,您可以将datarow / DataTable设置为全局变量,以使其可供其他类访问。而且,您可以创建一个返回数据表的公共方法,如:

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

答案 1 :(得分:1)

作为一个不错的选择,您可以使用textureview作为DataTable的数据来源,当您需要将数据传递到其他表单时,请使用DataGridView方法Copy }。这是一个例子:

你的GridForm:

DataTable

你的其他形式:

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