怎么写dataGrid绑定数据库的代码
我学开发一个小型的销售网站,我用ASP.NET和SQL2000来做,我想在werbform5.aspx里用dataGrid绑定数据库shop的shangpin表,想绑定表...
我学开发一个小型的销售网站,我用ASP.NET和SQL2000来做,
我想在werbform5.aspx里用dataGrid绑定数据库shop的shangpin表,想绑定表中的name,price,quality三个属性,我已经在web.config里添加后四行的连接代码:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="ConnectionString" value="uid=sa;password=;database=shop;server=(local)"/>
</appSettings>
我的dataGrid控件代码如下:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace WebApplication4
{
/// <summary>
/// WebForm5 的摘要说明。
/// </summary>
public class WebForm5 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.DropDownList DropDownList1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected SqlConnection GetConnection()
{
string conn=ConfigurationSettings.AppSettings["ConnectionString"];
SqlConnection myConn = new SqlConnection(conn);
return myConn;
}
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
public void DLBind()
{
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.DropDownList1.SelectedIndexChanged += new System.EventHandler(this.DropDownList1_SelectedIndexChanged);
this.DataGrid1.SelectedIndexChanged += new System.EventHandler(this.DataGrid1_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
{
}
private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
}
}
}
请高手帮我完善一下代码,谢谢了 展开
我想在werbform5.aspx里用dataGrid绑定数据库shop的shangpin表,想绑定表中的name,price,quality三个属性,我已经在web.config里添加后四行的连接代码:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="ConnectionString" value="uid=sa;password=;database=shop;server=(local)"/>
</appSettings>
我的dataGrid控件代码如下:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace WebApplication4
{
/// <summary>
/// WebForm5 的摘要说明。
/// </summary>
public class WebForm5 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.DropDownList DropDownList1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected SqlConnection GetConnection()
{
string conn=ConfigurationSettings.AppSettings["ConnectionString"];
SqlConnection myConn = new SqlConnection(conn);
return myConn;
}
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
public void DLBind()
{
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.DropDownList1.SelectedIndexChanged += new System.EventHandler(this.DropDownList1_SelectedIndexChanged);
this.DataGrid1.SelectedIndexChanged += new System.EventHandler(this.DataGrid1_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
{
}
private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
}
}
}
请高手帮我完善一下代码,谢谢了 展开
1个回答
展开全部
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using Wuqi.Webdiyer;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridView();
}
}
#region 绑定GridView
/// <summary>
/// 绑定GridView
/// </summary>
protected void BindGridView()
{
SqlConnection conn = DBConn.conn();
SqlDataAdapter per = new SqlDataAdapter();
DataSet ds = new DataSet();
try
{
// 总数
this.AspNetPager1.RecordCount = DataClass.CountNum("select count(*) from members");
// 查询内容
per.SelectCommand = new SqlCommand("select * from members", conn);
// 填充数据
per.Fill(ds, this.AspNetPager1.PageSize * (this.AspNetPager1.CurrentPageIndex - 1), this.AspNetPager1.PageSize, "members");
//ds.Tables["members"];
this.GridView1.DataSource = ds.Tables["members"];
this.GridView1.DataBind();
}
catch(SqlException e)
{
Response.Write(e.Errors.ToString());
}
finally
{
conn.Close();
per.Dispose();
ds.Dispose();
}
}
#endregion
#region 分页后绑定
protected void AspNetPager1_PageChanged(object sender, EventArgs e)
{
BindGridView();
}
#endregion
#region 排序
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
#region // 判断排序方式
if (ViewState["Order"] == null)
{
ViewState["Order"] = "ASC";
}
else
{
if (ViewState["Order"].ToString() == "ASC")
{
ViewState["Order"] = "DESC";
}
else
{
ViewState["Order"] = "ASC";
}
}
#endregion
// 重新绑定
SqlConnection conn = DBConn.conn();
SqlDataAdapter per = new SqlDataAdapter();
DataSet ds = new DataSet();
try
{
per.SelectCommand = new SqlCommand("select * from members", conn);
// 总页数
this.AspNetPager1.RecordCount = DataClass.CountNum("select count(*) from members");
// 填充数据
per.Fill(ds,this.AspNetPager1.PageSize *(this.AspNetPager1.CurrentPageIndex - 1),this.AspNetPager1.PageSize, "members");
ds.Tables["members"].DefaultView.Sort = e.SortExpression + " " + ViewState["Order"].ToString();
this.GridView1.DataSource = ds.Tables["members"].DefaultView;
this.GridView1.DataBind();
}
catch
{
Response.Write("错误,捕获异常");
}
finally
{
conn.Close();
per.Dispose();
ds.Dispose();
}
}
#endregion
#region 删除
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string DelID = this.GridView1.DataKeys[e.RowIndex].Value.ToString();
if (DataClass.DelFun("members", "uid = '" + DelID + "'"))
{
Jscript.Alert("删除成功!");
BindGridView();
}
else
{
Jscript.Alert("参数错误,请重试!");
}
}
#endregion
#region 变色
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//执行循环,保证每条数据都可以更新
for (int i = 0; i < GridView1.Rows.Count; i++)
{
//首先判断是否是数据行
if (e.Row.RowType == DataControlRowType.DataRow)
{
//当鼠标停留时更改背景色
e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#00A9FF'");
//当鼠标移开时还原背景色
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
}
}
}
#endregion
#region 取消
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
this.GridView1.EditIndex = -1;
BindGridView();
}
#endregion
#region 修改
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string UpdateID = this.GridView1.DataKeys[e.RowIndex].Value.ToString();
string[] Row = { "0", "1", "2", "3", "4", "5","6","7","8","9" };
for (Int32 i = 2; i < 8; i++)
{
Row[i] = ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[i].Controls[0]).Text.ToString().Trim();
}
string SqlStr = "update members set userName = '"
+ Row[2] + "' , userPass = '"
+ Row[3] + "' , userAge = '"
+ Row[4] + "' , userSex = '"
+ Row[5] + "' , userCity = '"
+ Row[6] + "' , userEmail = '"
+ Row[7] + "' where uid = '"+UpdateID+"'";
SqlConnection conn = DBConn.conn();
SqlCommand cmd = new SqlCommand(SqlStr, conn);
try
{
conn.Open();
cmd.ExecuteNonQuery();
}
catch (SqlException ex)
{
Response.Write(ex.Errors.ToString());
}
finally
{
conn.Close();
cmd.Dispose();
}
this.GridView1.EditIndex = -1;
BindGridView();
}
#endregion
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
this.GridView1.EditIndex = e.NewEditIndex;
BindGridView();
}
}
我这有 ,,也实现了分页功能
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using Wuqi.Webdiyer;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridView();
}
}
#region 绑定GridView
/// <summary>
/// 绑定GridView
/// </summary>
protected void BindGridView()
{
SqlConnection conn = DBConn.conn();
SqlDataAdapter per = new SqlDataAdapter();
DataSet ds = new DataSet();
try
{
// 总数
this.AspNetPager1.RecordCount = DataClass.CountNum("select count(*) from members");
// 查询内容
per.SelectCommand = new SqlCommand("select * from members", conn);
// 填充数据
per.Fill(ds, this.AspNetPager1.PageSize * (this.AspNetPager1.CurrentPageIndex - 1), this.AspNetPager1.PageSize, "members");
//ds.Tables["members"];
this.GridView1.DataSource = ds.Tables["members"];
this.GridView1.DataBind();
}
catch(SqlException e)
{
Response.Write(e.Errors.ToString());
}
finally
{
conn.Close();
per.Dispose();
ds.Dispose();
}
}
#endregion
#region 分页后绑定
protected void AspNetPager1_PageChanged(object sender, EventArgs e)
{
BindGridView();
}
#endregion
#region 排序
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
#region // 判断排序方式
if (ViewState["Order"] == null)
{
ViewState["Order"] = "ASC";
}
else
{
if (ViewState["Order"].ToString() == "ASC")
{
ViewState["Order"] = "DESC";
}
else
{
ViewState["Order"] = "ASC";
}
}
#endregion
// 重新绑定
SqlConnection conn = DBConn.conn();
SqlDataAdapter per = new SqlDataAdapter();
DataSet ds = new DataSet();
try
{
per.SelectCommand = new SqlCommand("select * from members", conn);
// 总页数
this.AspNetPager1.RecordCount = DataClass.CountNum("select count(*) from members");
// 填充数据
per.Fill(ds,this.AspNetPager1.PageSize *(this.AspNetPager1.CurrentPageIndex - 1),this.AspNetPager1.PageSize, "members");
ds.Tables["members"].DefaultView.Sort = e.SortExpression + " " + ViewState["Order"].ToString();
this.GridView1.DataSource = ds.Tables["members"].DefaultView;
this.GridView1.DataBind();
}
catch
{
Response.Write("错误,捕获异常");
}
finally
{
conn.Close();
per.Dispose();
ds.Dispose();
}
}
#endregion
#region 删除
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string DelID = this.GridView1.DataKeys[e.RowIndex].Value.ToString();
if (DataClass.DelFun("members", "uid = '" + DelID + "'"))
{
Jscript.Alert("删除成功!");
BindGridView();
}
else
{
Jscript.Alert("参数错误,请重试!");
}
}
#endregion
#region 变色
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//执行循环,保证每条数据都可以更新
for (int i = 0; i < GridView1.Rows.Count; i++)
{
//首先判断是否是数据行
if (e.Row.RowType == DataControlRowType.DataRow)
{
//当鼠标停留时更改背景色
e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#00A9FF'");
//当鼠标移开时还原背景色
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
}
}
}
#endregion
#region 取消
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
this.GridView1.EditIndex = -1;
BindGridView();
}
#endregion
#region 修改
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string UpdateID = this.GridView1.DataKeys[e.RowIndex].Value.ToString();
string[] Row = { "0", "1", "2", "3", "4", "5","6","7","8","9" };
for (Int32 i = 2; i < 8; i++)
{
Row[i] = ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[i].Controls[0]).Text.ToString().Trim();
}
string SqlStr = "update members set userName = '"
+ Row[2] + "' , userPass = '"
+ Row[3] + "' , userAge = '"
+ Row[4] + "' , userSex = '"
+ Row[5] + "' , userCity = '"
+ Row[6] + "' , userEmail = '"
+ Row[7] + "' where uid = '"+UpdateID+"'";
SqlConnection conn = DBConn.conn();
SqlCommand cmd = new SqlCommand(SqlStr, conn);
try
{
conn.Open();
cmd.ExecuteNonQuery();
}
catch (SqlException ex)
{
Response.Write(ex.Errors.ToString());
}
finally
{
conn.Close();
cmd.Dispose();
}
this.GridView1.EditIndex = -1;
BindGridView();
}
#endregion
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
this.GridView1.EditIndex = e.NewEditIndex;
BindGridView();
}
}
我这有 ,,也实现了分页功能
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询