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

新手请教:用代码如何实现控件与数据源的绑定?
利用2005手动配置数据源,直接将table拖到控件datagridview、bindingnavigator上,即可实现数据的绑定!
在form_load里面的代码就下面一句话:
private void Form1_Load(object sender, EventArgs e)
{
  this.tBL_USERTableAdapter.Fill(this.dataSet1.TBL_USER);
}
由于考虑到代码的移植,还是想通过代码实现这样的数据源绑定操作功能,还望高手指教一二,谢谢了!!!

------解决方案--------------------
楼主有必要看一下ADO.NET的相关知识!
------解决方案--------------------
gridView.DataSource = dataSet1.TBL_USER;
gridView.DataBind();
------解决方案--------------------
楼上的就是一个比较典型绑定GridView的ADO.NET的例子!
------解决方案--------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace MyDataGrid
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.Label Label4;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.TextBox TextBox3;
protected System.Web.UI.WebControls.TextBox TextBox4;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Label Label5;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void bindGrid()
{
SqlConnection con;
con=new SqlConnection("server=.;database=pubs;user id=sa;password=");
SqlDataAdapter fillcmd=new SqlDataAdapter("select * from customer",con);
DataSet ds=new DataSet();
fillcmd.Fill(ds);
DataGrid1.DataSource=ds.Tables[0].DefaultView;
DataGrid1.DataBind();
}

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!Page.IsPostBack)
{
bindGrid();

}
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.DataGrid1.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.SelectCustomer);
this.DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.CancelCustomer);
this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.EditCustomer);
this.DataGrid1.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.SortCustomer);
this.DataGrid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.UpdateCustomer);
this.DataGrid1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DeleteCustomer);
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void UpdateCustomer(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
SqlConnection con;
con=new SqlConnection("server=.;database=pubs;user id=sa;password=");