开发者

Transfer records

Suppose I have a datagridview with 100 records

When I select a record

record go to the Datagridview form2

Then when I came back to form1

And others chose a record

It also goes to form2, but there is also the previous r开发者_高级运维ecession

Want datagridview in form2 remains constant

I hope you understand what I mean


  1. Define a Form2 variable in form1 which point to form2: Form2 Frm2;
  2. Define a public DataTable in Form2: Public DataTable Dt;
  3. Initilize Frm2 and dt in your form load event:

    private void Form1_Load(object sender, EventArgs e)
    {
      this.Frm2 = new Form2();
      this.Frm2.Dt = new DataTable();
    }
    
  4. Here's where it gets done:

        public void AddRowInDataTable(int SelectedIndex)
    {
        //Add the columns
        DataColumn col = null;
        //For each columns in the datagridveiw add a new column to data table
        foreach (DataGridViewColumn dgvCol in dataGridView1.Columns)
        {
            col = new DataColumn(dgvCol.Name);
            if (!Frm2.dt.Columns.Contains(dgvCol.Name))
                Frm2.dt.Columns.Add(col);
        }
        //Add the selected row from the datagridview
        DataRow row = null;
        row = Frm2.dt.Rows.Add();
        foreach (DataGridViewColumn column in dataGridView1.Columns)
        {
            row[column.Index] = dataGridView1.Rows[SelectedIndex].Cells[column.Index].Value;
        }
    }  
    
  5. handle OnCellClick event of form1's dataGridView1:

    private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
     AddRowInDataTable(e.RowIndex);
     Frm2.ShowDialog();  
    }
    
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜