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
- Define a Form2 variable in form1 which point to form2:
Form2 Frm2;
- Define a public DataTable in Form2:
Public DataTable Dt;
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(); }
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; } }
handle
OnCellClick
event of form1's dataGridView1:private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { AddRowInDataTable(e.RowIndex); Frm2.ShowDialog(); }
精彩评论