利用2005手动配置数据源,直接将table拖到控件datagridview、bindingnavigator上,即可实现数据的绑定!
在form_load里面的代码就下面一句话:
private void Form1_Load(object sender, EventArgs e)
{
this.tBL_USERTableAdapter.Fill(this.dataSet1.TBL_USER);
}
由于考虑到代码的移植,还是想通过代码实现这样的数据源绑定操作功能,还望高手指教一二,谢谢了!!!
在form_load里面的代码就下面一句话:
private void Form1_Load(object sender, EventArgs e)
{
this.tBL_USERTableAdapter.Fill(this.dataSet1.TBL_USER);
}
由于考虑到代码的移植,还是想通过代码实现这样的数据源绑定操作功能,还望高手指教一二,谢谢了!!!
gridView.DataBind();
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=");
SqlCommand updateCommand;
updateCommand=new SqlCommand("update customer set cname=@name,caddress=@address,email=@email where cid=@id");
updateCommand.Connection=con;
updateCommand.Parameters.Add(new SqlParameter("@name",SqlDbType.NChar,20));
updateCommand.Parameters.Add(new SqlParameter("@address",SqlDbType.NChar,20));
updateCommand.Parameters.Add(new SqlParameter("@email",SqlDbType.NChar,20));
updateCommand.Parameters.Add(new SqlParameter("@id",SqlDbType.NChar,4));
updateCommand.Parameters["@id"].Value=DataGrid1.DataKeys[(int)e.Item.ItemIndex];
updateCommand.Parameters["@name"].Value=((TextBox)e.Item.Cells[5].Controls[0]).Text;
updateCommand.Parameters["@address"].Value=((TextBox)e.Item.Cells[6].Controls[0]).Text;
updateCommand.Parameters["@email"].Value=((TextBox)e.Item.Cells[7].Controls[0]).Text;
con.Open();
updateCommand.ExecuteNonQuery();
con.Close();
DataGrid1.EditItemIndex=-1;
bindGrid();
} private void EditCustomer(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex=(int)e.Item.ItemIndex;
bindGrid();
} private void CancelCustomer(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex=-1;
bindGrid();
} private void Button1_Click(object sender, System.EventArgs e)
{
SqlConnection con;
con=new SqlConnection("server=.;database=pubs;user id=sa;password=");
SqlCommand insertCommand=new SqlCommand("insert customer values(@id,@name,@address,@email)");
insertCommand.Connection=con;
insertCommand.Parameters.Add(new SqlParameter("@id",SqlDbType.NChar,4));
insertCommand.Parameters.Add(new SqlParameter("@name",SqlDbType.NChar,20));
insertCommand.Parameters.Add(new SqlParameter("@address",SqlDbType.NChar,20));
insertCommand.Parameters.Add(new SqlParameter("@email",SqlDbType.NChar,20));
insertCommand.Parameters["@id"].Value=TextBox1.Text;
insertCommand.Parameters["@name"].Value=TextBox2.Text;
insertCommand.Parameters["@address"].Value=TextBox3.Text;
insertCommand.Parameters["@email"].Value=TextBox4.Text;
con.Open();
insertCommand.ExecuteNonQuery();
con.Close();
bindGrid();
} private void DeleteCustomer(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
SqlConnection con;
con=new SqlConnection("server=.;database=pubs;user id=sa;password=");
SqlCommand deleteCommand=new SqlCommand("delete from customer where cid=@id");
deleteCommand.Connection=con;
deleteCommand.Parameters.Add(new SqlParameter("@id",SqlDbType.NChar,4));
deleteCommand.Parameters["@id"].Value=DataGrid1.DataKeys[(int)e.Item.ItemIndex];
con.Open();
deleteCommand.ExecuteNonQuery();
con.Close();
bindGrid();
} private void SelectCustomer(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if(e.Item.ItemIndex!=-1)
{
SqlConnection con;
con=new SqlConnection("server=.;database=pubs;user id=sa;password=");
SqlCommand selCommand=new SqlCommand("select cname from customer where cid=@id",con);
// selCommand.Connection=con;
selCommand.Parameters.Add(new SqlParameter("@id",SqlDbType.NChar,4));
selCommand.Parameters["@id"].Value=DataGrid1.DataKeys[(int)e.Item.ItemIndex];
con.Open();
string name=selCommand.ExecuteScalar().ToString();
Label5.Text="You selected is:"+name;
con.Close();
bindGrid();
}
} private void SortCustomer(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
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;
ds.Tables[0].DefaultView.Sort=e.SortExpression;
DataGrid1.DataBind();
}
}
}
这是一个用2003写的连数据库的例子,希望对你有些帮助
2设置数据源
3绑定数据
看下ADO吧
03和05在这方面是一样的
这个是DataGrid绑定数据源的例子,你的页面要有DataGrid控件才行
还有,我可能只需要表中的几列、列的标题名称需要修改为中文的、bindingnavigator也绑定同样的数据源?
用代码写怎么这么麻烦的,直接用向导配制5分钟就搞定了,郁闷~~~~
//****注意在下面一定要压缩尾部空格 然后在as
Tabl1 = fun.sqldataset("select Rtrim(Measureid) as Measureid,Rtrim(measureunit)as measureunit from g_ComputationUnitTable order by Measureid").Tables[0];
this.dataGridView1.Columns.Clear(); //防止再次重载时继续添加列
this.dataGridView1.AllowUserToResizeRows = false;//禁止用户调整单元格高度
this.dataGridView1.AutoGenerateColumns = false;//****禁止DataGirdView1自动创建列****
this.dataGridView1.MultiSelect = false; //禁止用户一次性操作多个列
this.dataGridView1.RowHeadersVisible = false;// 去掉表头
this.dataGridView1.AllowUserToAddRows = false;//防止用户添加行(即删除DataGridView中默认的一个空行)
this.dataGridView1.DataSource = Tabl1;//表绑定的DataGirdView1
//定义表单元格的对象
DataGridViewTextBoxColumn dcn;
dcn = new DataGridViewTextBoxColumn();//单元格实例化成列
dcn.DataPropertyName = "Measureid";//绑定数据表的相应列
dcn.ReadOnly = true;
dcn.Name = "Measureid";
dcn.Tag = "";
dcn.HeaderText = "计量编号";
dcn.Width = 78;
dcn.SortMode = DataGridViewColumnSortMode.NotSortable;
dcn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;//表格内容对齐方式
dcn.DefaultCellStyle.SelectionForeColor = Color.Black;
dcn.DefaultCellStyle.SelectionBackColor = Color.Bisque;
dcn.DefaultCellStyle.ForeColor = Color.Black;
dcn.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;//表头对齐方式
dcn.MaxInputLength = 4; //限定输入字符长度
this.dataGridView1.Columns.Add(dcn); dcn = new DataGridViewTextBoxColumn();//单元格实例化成列
dcn.DataPropertyName = "measureunit";//绑定数据表的相应列
dcn.ReadOnly = false;
dcn.Name = "measureunit";
dcn.Tag = "";
dcn.HeaderText = "计量单位";
dcn.Width = 205;
dcn.SortMode = DataGridViewColumnSortMode.NotSortable;
dcn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
dcn.DefaultCellStyle.SelectionForeColor = Color.Black;
dcn.DefaultCellStyle.SelectionBackColor = Color.Bisque;
dcn.DefaultCellStyle.ForeColor = Color.Black;
dcn.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
dcn.MaxInputLength =5;
this.dataGridView1.Columns.Add(dcn);