今天下了个2005,玩了玩GRIDVIEW,觉得功能确实强大。下面是一点用法。
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;public partial class test : System.Web.UI.Page
{
SqlConnection con;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Data_Bind();
} } #region 数据绑定 private void Data_Bind()
{
string str = "server=localhost;uid=sa;pwd=;database=lsa_shifang";
con = new SqlConnection(str);
SqlDataAdapter DA = new SqlDataAdapter("SELECT * FROM TEST_MODEL", con);
DataSet ds = new DataSet();
DA.Fill(ds); GridView1.DataSource = ds;
GridView1.DataBind();
}
#endregion #region 翻页
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
Data_Bind(); }
#endregion #region 编辑
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
Data_Bind();
}
#endregion
#region 取消
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
Data_Bind();
}
#endregion #region 更新
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow gr = GridView1.Rows[e.RowIndex];
string str = "server=localhost;uid=sa;pwd=;database=lsa_shifang";
con = new SqlConnection(str);
string up = "update test_model set ask=@ask where id=@id";
SqlCommand com = new SqlCommand(up, con);
com.Connection.Open();
com.Parameters.AddWithValue("@ask", SqlDbType.VarChar);
com.Parameters["@ask"].Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[6].Controls[0]).Text.Trim();
com.Parameters.AddWithValue("@id", SqlDbType.Int);
com.Parameters["@id"].Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[7].Controls[0]).Text.Trim(); com.ExecuteNonQuery();
com.Connection.Close();
GridView1.EditIndex = -1;
this.Page.RegisterStartupScript("","<SCRIPT>alert('更新成功!')</SCRIPT>");
Data_Bind(); }
#endregion #region 删除
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string str = "server=localhost;uid=sa;pwd=;database=lsa_shifang";
con = new SqlConnection(str);
string del = "delete test_model where id=@id";
SqlCommand com = new SqlCommand(del, con);
com.Connection.Open();
com.Parameters.AddWithValue("@id", SqlDbType.Int);
com.Parameters["@id"].Value = GridView1.Rows[e.RowIndex].Cells[7].Text;
com.ExecuteNonQuery();
com.Connection.Close();
this.Page.RegisterStartupScript("", "<SCRIPT>alert('删除成功!')</SCRIPT>");
Data_Bind();
}
#endregion
}
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;public partial class test : System.Web.UI.Page
{
SqlConnection con;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Data_Bind();
} } #region 数据绑定 private void Data_Bind()
{
string str = "server=localhost;uid=sa;pwd=;database=lsa_shifang";
con = new SqlConnection(str);
SqlDataAdapter DA = new SqlDataAdapter("SELECT * FROM TEST_MODEL", con);
DataSet ds = new DataSet();
DA.Fill(ds); GridView1.DataSource = ds;
GridView1.DataBind();
}
#endregion #region 翻页
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
Data_Bind(); }
#endregion #region 编辑
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
Data_Bind();
}
#endregion
#region 取消
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
Data_Bind();
}
#endregion #region 更新
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow gr = GridView1.Rows[e.RowIndex];
string str = "server=localhost;uid=sa;pwd=;database=lsa_shifang";
con = new SqlConnection(str);
string up = "update test_model set ask=@ask where id=@id";
SqlCommand com = new SqlCommand(up, con);
com.Connection.Open();
com.Parameters.AddWithValue("@ask", SqlDbType.VarChar);
com.Parameters["@ask"].Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[6].Controls[0]).Text.Trim();
com.Parameters.AddWithValue("@id", SqlDbType.Int);
com.Parameters["@id"].Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[7].Controls[0]).Text.Trim(); com.ExecuteNonQuery();
com.Connection.Close();
GridView1.EditIndex = -1;
this.Page.RegisterStartupScript("","<SCRIPT>alert('更新成功!')</SCRIPT>");
Data_Bind(); }
#endregion #region 删除
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string str = "server=localhost;uid=sa;pwd=;database=lsa_shifang";
con = new SqlConnection(str);
string del = "delete test_model where id=@id";
SqlCommand com = new SqlCommand(del, con);
com.Connection.Open();
com.Parameters.AddWithValue("@id", SqlDbType.Int);
com.Parameters["@id"].Value = GridView1.Rows[e.RowIndex].Cells[7].Text;
com.ExecuteNonQuery();
com.Connection.Close();
this.Page.RegisterStartupScript("", "<SCRIPT>alert('删除成功!')</SCRIPT>");
Data_Bind();
}
#endregion
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货