日期:2014-05-18  浏览次数:20925 次

是不是datagridview只要绑定dataset就可以了,如果不用bindingsource要不要紧
C# code
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 testnewdgv
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        DataTable dt = new DataTable("useraction");
        private void Form1_Load(object sender, EventArgs e)
        {
            
            //DataSet ds = new DataSet();
            DataColumn column;
            column = new DataColumn("url", Type.GetType("System.String"));
            column.Caption = "url";
            dt.Columns.Add(column);
            column = new DataColumn("totaltime", Type.GetType("System.String"));
            column.Caption = "totaltime";
            dt.Columns.Add(column);
            column = new DataColumn("efftime", Type.GetType("System.String"));
            column.Caption = "efftime";
            dt.Columns.Add(column);
            //ds.Tables.Add(dt);
            //BindingSource bds = new BindingSource();
            //bds.DataSource = ds;
            dgv1.AutoGenerateColumns = true;
            dgv1.DataMember = "useraction";
            dgv1.DataSource = dt;
            dgv1.AllowUserToAddRows = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DataRow dr;
            dr = dt.NewRow();
            dr["url"] ="www.baidu.com";
            dr["totaltime"] = "01:11:11";
            dr["efftime"] = "01:11:13";
            dt.Rows.Add(dr);
        }
    }
}


大家看下,这样绑定数据源有没有什么问题?我打算这样做了,先写了个测试程序,希望大家给出宝贵意见。

------解决方案--------------------
可以直接设置 DataSource 

BindingSource还有更多的功能,具体参考msdn