C#中dataGridView控件数据绑定

踏上.NET的路程,菜鸟碰到的第一个问题,mark一下

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

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

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

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

4. Form1中初始化Column;

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

 

From1代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication11
{
    public partial class Form1 : Form
    {
        BindingSource source = new BindingSource();

        public Form1()
        {
            InitializeComponent();
            Column1.DataPropertyName = "_ID";
            Column2.DataPropertyName = "_Name";
            Column3.DataPropertyName = "_Value";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 dd = new Form2();
            if (dd.ShowDialog() == DialogResult.OK)
            {
                source.Clear();
                source = dd.returnValue();
                dataGridView1.DataSource = source;
 
            }
        }
    }
}

 

Form2代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication11
{
    public partial class Form2 : Form
    {
        List<DataGrid> dd = new List<DataGrid>();

        

        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            dd.Clear();
            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;
            this.Close();
        }

        public BindingSource returnValue()
        {
            BindingSource source = new BindingSource();
            source.Clear();
            foreach (DataGrid data in dd)
            {
                source.Add(data);
            }
            return source;
        }

    }
}

 

定义类代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindowsFormsApplication11
{
    class DataGrid
    {
        public string _ID { get; set; }
        public string _Name { get; set; }
        public string _Value { get; set; }
    }

}

 

转载于:https://www.cnblogs.com/yydcn/p/5262280.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值