基本信息
源码名称:如何利用dataGridView控件实现不同窗体之间的数据传递
源码大小:0.18M
文件格式:.rar
开发语言:C#
更新时间:2017-11-14
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍


不同窗体dataGridView之间的数据传递,通过数据绑定实现(假设Form2中的数据要传递到Form1中):

1. 定义一个类,存放dataGridView的数据;

2. Form2中定义dataGridView数据列表,并将数据存放进去;

3.From2中定义返回绑定数据的函数,返回绑定数据;

4. Form1中初始化Column

5. 在Form1中出发Form2,在结束后将From2返回的绑定数据传到Form1中的dataGridView中,完成数据传递。


{
        List<DataGrid> dd = new List<DataGrid>();
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            dd.Clear();
            //-1是因为datagrivew最后一项为空
            for (int i = 0; i < dataGridView1.Rows.Count-1; i )
            {
                DataGrid aa = new DataGrid();
                aa._ID = dataGridView1.Rows[i].Cells[0].Value.ToString();
                aa._Name = dataGridView1.Rows[i].Cells[1].Value.ToString();
                aa._Value = dataGridView1.Rows[i].Cells[2].Value.ToString();
                dd.Add(aa);
            }
            this.DialogResult = DialogResult.OK;
        }
        public BindingSource returnValue()
        {
            BindingSource source = new BindingSource();
            source.Clear();
            foreach (DataGrid data in dd)
            {
                source.Add(data);
            }
            return source;
        }