c# datagridview与DataSet绑定, 列与数据库表里面的列一一对应

参考代码1:

自己模拟出数据,并分别对dataGridView赋值。

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;
using System.Collections;
 
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        DataTable tblDatas = new DataTable("Datas");
        private void Form1_Load(object sender, EventArgs e)
        {
            DataColumn dc = null;
            dc = tblDatas.Columns.Add("ID", Type.GetType("System.Int32"));
            dc.AutoIncrement = true;//自动增加
            dc.AutoIncrementSeed = 1;//起始为1
            dc.AutoIncrementStep = 1;//步长为1
            dc.AllowDBNull = false;//
 
            dc = tblDatas.Columns.Add("Product", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("Version", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("Description", Type.GetType("System.String"));
 
            DataRow newRow;
            for (int i = 0; i < 20; i++)
            {
                newRow = tblDatas.NewRow();
                newRow["Product"] = "大话西游";
                newRow["Version"] = "2.0";
                newRow["Description"] = "我很喜欢";
                tblDatas.Rows.Add(newRow);
            }
 
            newRow = tblDatas.NewRow();
            newRow["Product"] = "梦幻西游";
            newRow["Version"] = "3.0";
            newRow["Description"] = "比大话更幼稚";
            tblDatas.Rows.Add(newRow);
            dataGridView1.DataSource = tblDatas;
            //dataGridView1
        }
 
        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            //datagridview变化DataSet数据跟随变化;
 
            tblDatas.Rows[e.RowIndex][e.ColumnIndex] = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            //这个是DataSet数据变化datagridview跟随变化;
            //这个要根据实际来的,这里只有修改dataSet中一个单元格的数据。
            tblDatas.Rows[5][1] = "5555555555";
        }
    }
}

 

参考代码2

手动分别赋值方式。

DataSet ds_sp = new DataSet();
DataTable dt_sp = new DataTable();

if (string.IsNullOrEmpty(strFilter))
{
strFilter = " 1=1";
}
dt_sp = DbHelper.getSqlTaskList(tablename, "*", sortExpr, this.SGVTask.PageSize, pageIndex, 0, strFilter, sortDirect);//Sql 中使用翻页函数

DataTable dtTask = dt_sp;

for (int i = 0; i < dtTask.Rows.Count; i++)
{
DataRow dr = dt.NewRow();
dr["YMD"] = dtTask.Rows[i]["YMD"].ToString();
dr["SDSHAN"] = dtTask.Rows[i]["SDSHAN"].ToString();
dr["ABALPH"] = dtTask.Rows[i]["ABALPH"].ToString();
dr["XSJE"] = dtTask.Rows[i]["XSJE"].ToString();
dr["FenTan_Month"] = dtTask.Rows[i]["FenTan_Month"].ToString();
dr["Cont_KeBian"] = dtTask.Rows[i]["Cont_KeBian"].ToString();
dr["Contract_Num"] = dtTask.Rows[i]["Contract_Num"].ToString();
dr["FenTan_Mode"] = dtTask.Rows[i]["FenTan_Mode"].ToString();
dr["FenTan_BL_Je"] = dtTask.Rows[i]["FenTan_BL_Je"].ToString();
dr["CJ"] = dtTask.Rows[i]["CJ"].ToString();
dr["FixSmOrderNum"] = dtTask.Rows[i]["FixSmOrderNum"].ToString();
dr["ftfs"] = dtTask.Rows[i]["ftfs"].ToString();
dr["FTDAI"] = dtTask.Rows[i]["FTDAI"].ToString();
dr["DAIKM"] = dtTask.Rows[i]["DAIKM"].ToString();
dr["FTJIE"] = dtTask.Rows[i]["FTJIE"].ToString();
dr["JIEKM"] = dtTask.Rows[i]["JIEKM"].ToString();
dr["MEMO"] = dtTask.Rows[i]["MEMO"].ToString();
dt.Rows.Add(dr);
}
this.SGVTask.DataSource = dt;
this.SGVTask.DataBind();

 

参考方式3 映射方式:

先在DataGridView中定义一些列,将这些列的DataPropertyName设置为查询得到的数据库的返回的DataSet中相关DataTable的列名称。

具体的属性设置可参考本博客文章:C# DataGridView的列对象属性探讨 (未完待续)

 

 

 

参考

c# datagridview 如何与DataSet绑定实现如下目标

C#怎样使 dataGridView1显示数据的时候,dataGridView1的列与数据库表里面的列一一对应

 

posted @ 2016-10-18 17:27  wenglabs  阅读(1687)  评论(0编辑  收藏  举报