分享

在两个不同的Form窗体中的DataGridView数据“传递”

 goodwangLib 2014-09-13

在写程序时,遇到要在一个Form窗体中的DataGridView中选择数据,然后添加到另外一个Form窗体中的DataGridView中。因为我想只点中某行的某个Cell就可以把这整行的内容取出来(即在某一行数据中,我随便点中一个Cell,就算选中了这整行数据)。举个例子如下:

首先我在左边选择一些Cell,然后添加就可以添加到右边那里:

效果如下:


下面是实现的代码:

1、首先在主窗体调用子窗体的时候写这个(这里我是用一个按钮来触发):

  1. private void addDatabaseButton_Click(object sender, EventArgs e)  
  2. {  
  3.      FactDatabaseForm factDB_Form = new FactDatabaseForm();  
  4.      factDB_Form.Owner = this;  
  5.      factDB_Form.Show(); // 或者factDB_Form.ShowDialog();  
  6. }  
2、在子窗体里面:
  1. private void Form2_Load(object sender, EventArgs e)  
  2. {  
  3.      mainForm = (MainForm)this.Owner;  // 获得主窗体的引用  
  4.      mainCurDGV = mainForm.curDGV;     // 获得主窗体中的那个DataGridView  
  5.   
  6.      // TODO: This line of code loads data into the 'myDataSet.FactDB' table. You can move, or remove it, as needed.  
  7.      this.factDBTableAdapter.Fill(this.myDataSet.FactDB);  
  8. }  
3、然后在按钮“把所选行增加到当前要推导的事实库中”的事件处理函数里面:
  1. private void saveToCurFactDB_Click(object sender, EventArgs e)  
  2.         {  
  3.             Dictionary<intobject> dicts = new Dictionary<intobject>();  
  4.   
  5.             // 得到选中的行(不管是以整行选中,还是只是选中某行的某个单元,认为选中了该行),并且把这行的东西放到Dictionary里面  
  6.             int key;  
  7.             foreach (DataGridViewCell cell in this.factDatebaseDataGridView.SelectedCells)  
  8.             {  
  9.                 key = Convert.ToInt32(factDatebaseDataGridView.Rows[cell.RowIndex].Cells[0].Value); // 得到该行中的那个"factID"  
  10.                 if (!mainForm.mainDict.ContainsKey(key))  // 避免有重复  
  11.                 {  
  12.                     mainForm.mainDict.Add(key, factDatebaseDataGridView.Rows[cell.RowIndex].Cells[1].Value);  
  13.                     dicts.Add(key, factDatebaseDataGridView.Rows[cell.RowIndex].Cells[1].Value);  
  14.                 }  
  15.             }  
  16.   
  17.             if(dicts.Count > 0)   // 如果选择了一些在当前事实库没有的基才执行下面的  
  18.             {  
  19.                 DataGridViewRow[] rows = new DataGridViewRow[dicts.Count];  
  20.                 foreach (var dict in dicts)  
  21.                 {  
  22.                     addItems(dict.Key, dict.Value as string);  
  23.                 }  
  24.   
  25.                 mainCurDGV.Sort(mainCurDGV.Columns[0], ListSortDirection.Ascending);  
  26.                 MessageBox.Show("增加成功!""消息");  
  27.             }  
  28.             else  
  29.             {  
  30.                 MessageBox.Show("当前事实库已经存在你选择的这些事实""提示");  
  31.             }  
  32.         }  
  33.   
  34.         private void addItems(int iFactID, string strFactName)  
  35.         {  
  36.             //此处的代码不能进行循环!必须封装为一个方法,通过方法的循环,才能实现循环!  
  37.             DataGridViewRow dgvr = new DataGridViewRow();  
  38.             foreach (DataGridViewColumn c in this.factDatebaseDataGridView.Columns)  
  39.             {  
  40.                 dgvr.Cells.Add(c.CellTemplate.Clone() as DataGridViewCell);  
  41.             }  
  42.             dgvr.Cells[0].Value = iFactID;  
  43.             dgvr.Cells[1].Value = strFactName;  
  44.             mainCurDGV.Rows.Add(dgvr);  
  45.         }  
这里对上面代码再简单说明一下 ,如果要取得DataGridView中的某一行,可以这样写:
  1. dataGridView.Rows[i];  
得到某一行的某个单元:
  1. dataGridView.Rows[i].Cells[j]  



    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多