将datagridview内容以新格式复制到datagridview

时间:2015-09-16 13:04:00

标签: datagridview

这里我有两个表单(Form1和Cinvo),每个表单都有一个datagridview,我想将所有行从Form1 datagridview复制到Cinvo datagridview,我已经做了一些编码,它只从Form1 datagridview复制到Cinvo datagridview 。但是我想要复制所有行,所以如果有人可以做一些修改来获得我想要的结果。

Form1中

public partial class Form1 : Form
{
    double total = 0;


    public Form1()
    {
        InitializeComponent();

    }

    private void label2_Click(object sender, EventArgs e)
    {

    }
    string st1;
    string st2;
    string st3;
    string st4;
    string st5;
    string st6;

    private void button1_Click(object sender, EventArgs e)
    {
        if (textBox6.Text == null && comboBox1.Text == null)
        {
            dataGridView1.Enabled = false;
        }

        else
        {

            st1 = textBox6.Text;
            st2 = comboBox1.Text;
            st3 = comboBox2.Text;
            st4 = textBox7.Text;
            st5 = textBox8.Text;
            double x = float.Parse(textBox7.Text);
            double y = float.Parse(textBox8.Text);
            double p = x * y;

            total = total + p;
            textBox9.Text = total.ToString ();


            st6 = Convert.ToString(p);



            string[] row = { st1, st2, st3, st4, st5, st6};
            dataGridView1.Rows.Add(row[0], row[1], row[2], row[3], row[4],row[5]);

        }

        textBox6.Text = null;
        textBox7.Text = null;
        textBox8.Text = null;

        comboBox1.Text = null;
        comboBox2.Text = null;

        //double g = float.Parse(textBox7.Text);
        //double h = float.Parse(textBox8.Text);
        //double gh = g * g;
        //double total = 0;
        //total = total + gh;
        //textBox9.Text = Convert.ToString(total);


    }

    private void button2_Click(object sender, EventArgs e)
    {
        textBox1.Text = null;
        textBox2.Text = null;
        comboBox3.Text = null;
        comboBox4.Text = null;

        textBox6.Text = null;
        textBox7.Text = null;
        textBox8.Text = null;
        textBox9.Text = null;

       // dataGridView1.Rows.Clear();

    }

    private void button3_Click(object sender, EventArgs e)
    {
        if (comboBox3.Text ==""|| textBox2.Text == ""|| comboBox4.Text =="") 
        {
            MessageBox.Show("Sorry, Customer is not registered");

        }

        else
        {
            MessageBox.Show("asc");
        }
    }

    private void button6_Click(object sender, EventArgs e)
     {

       //  float x=(dataGridView1.Rows[i].Cells[4].ToString());






     } 

    private void button4_Click(object sender, EventArgs e)
    {
        string a1 = null;
        string a2 = null;
        string a3 = null;
        string a4 = null;
        string a5 = null;
        string a6 = null;


        for(int i =0; i<dataGridView1.RowCount;i++)
        {
            a1 = dataGridView1.Rows[i].Cells[0].Value.ToString();
            a2 = dataGridView1.Rows[i].Cells[1].Value.ToString();
            a3 = dataGridView1.Rows[i].Cells[2].Value.ToString();
            a4 = dataGridView1.Rows[i].Cells[3].Value.ToString();
            a5 = dataGridView1.Rows[i].Cells[4].Value.ToString();
            a6 = dataGridView1.Rows[i].Cells[5].Value.ToString();


        }


        Cinvo ops = new Cinvo(a1, a2, a3, a4, a5,a6);

       // Cinvo nwcn = new Cinvo();
        ops. ShowDialog();

    }

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {

    }

    private void button7_Click(object sender, EventArgs e)
    {
        try
        {
            dataGridView1.Rows.RemoveAt(dataGridView1.CurrentCell.RowIndex);
        }

        catch ( Exception ex)

        {

        }

    }

    private void button5_Click(object sender, EventArgs e)
    {
        int i = dataGridView1.CurrentRow.Index;

        DataGridViewRow row = dataGridView1.Rows[i];
        row.Cells[0].Value = textBox6.Text;
        row.Cells[1].Value = comboBox1.Text;
        row.Cells[2].Value = comboBox2.Text;
        row.Cells[3].Value = textBox7.Text;
        row.Cells[4].Value = textBox8.Text;


    }

    private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        int i = e.RowIndex;
        DataGridViewRow row = dataGridView1.Rows[i];
        textBox6.Text = row.Cells[0].Value.ToString();
        comboBox1.Text = row.Cells[1].Value.ToString();
        comboBox2.Text = row.Cells[2].Value.ToString();
        textBox7.Text = row.Cells[3].Value.ToString();
        textBox8.Text = row.Cells[4].Value.ToString();

    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void groupBox2_Enter(object sender, EventArgs e)
    {

    }

Cinvo

   public partial class Cinvo : Form
{
    private string a1;
    private string a2;
    private string a3;
    private string a4;
    private string a5;
    private string a6;
    //public Cinvo()
    //{
    //    InitializeComponent();
    //    //Form1 frm1 = new Form1();

    //}


    public Cinvo(string a1, string a2, string a3, string a4, string a5, string a6)
    {
        InitializeComponent();
        // TODO: Complete member initialization
        this.a1 = a1;
        this.a2 = a2;
        this.a3 = a3;
        this.a4 = a4;
        this.a5 = a5;
        this.a6 = a6;


            string[] row = { a1, a2, a3, a4, a5, a6 };
               dataGridView1.Rows.Add(row[0], row[1], row[2], row[3], row[4], row[5]);

   }


    private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
    {

    }

    private void Cinvo_Load(object sender, EventArgs e)
    {



    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        float x = float.Parse(textBox3.Text);
        float y = float.Parse(textBox1.Text);
        float z = y - x;
        string final = Convert.ToString(z);
        textBox2.Text = final;
    }

1 个答案:

答案 0 :(得分:0)

这样的事情对你有用吗?

CopyDataGridViews(dataGridView1, dataGridView2);

功能:

public void CopyDataGridViews(DataGridView SourceDGV, DataGridView DestinationDGV)
{
    // Clear Destination DataGridView
    DestinationDGV.Columns.Clear();
    DestinationDGV.Rows.Clear();

    // Create Destination Columns
    for (int j = 0; j < SourceDGV.Columns.Count; j++)
    {
        DestinationDGV.Columns.Add(SourceDGV.Columns[j].Clone() as DataGridViewColumn);
    }

    // Create Destination Rows
    for (int i = 0; i < SourceDGV.Rows.Count - 1; i++)
    {
        DestinationDGV.Rows.Add(SourceDGV.Rows[i].Clone() as DataGridViewRow);
    }

    // Copy Data to Destination
    for (int column = 0; column < SourceDGV.Columns.Count; column++)
    {
        for (int row = 0; row < SourceDGV.Rows.Count; row++)
        {
            DestinationDGV.Rows[row].Cells[column].Value = SourceDGV.Rows[row].Cells[column].Value;
        }
    }
}
相关问题