今天工作不忙,刚好顺便学习一下,把前几天的gridview的功能增强了一些,顺便也改进了一下。 实现了增,删,该,查,这些基本的功能。有需要的朋友可以看一下。
后台代码
public
partial
class
GridViewExercise : System.Web.UI.Page
... {
string ConStr = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
...{
SortExprssion();
SetGridView();
if (!IsPostBack)
...{
ViewState["CompanyName"] = "SupplierID";
ViewState["Direction"] = "ASC";
BindGridView();
}
}
//设置排序表达式
private void SortExprssion()
...{
GridView1.Columns[3].SortExpression = GridView1.Columns[3].HeaderText.ToString();
}
//设置gridview的属性
private void SetGridView()
...{
GridView1.AllowPaging = true;
GridView1.AllowSorting = true;
}
private void BindGridView()
...{
string QueryCon = "SELECT SupplierID,CompanyName,ContactName,Address,City FROM Suppliers";
SqlConnection NorthWindCon = new SqlConnection(ConStr);
SqlDataAdapter NorthWindDa = new SqlDataAdapter(QueryCon,ConStr);
DataSet Ds = new DataSet();
NorthWindDa.Fill(Ds, "Suppliers");
GridView1.DataKeyNames = new string[] ...{ "SupplierID" };
DataView Dv = Ds.Tables["Suppliers"].DefaultView;
//排序表达式
string SortExpress = (string)ViewState["CompanyName"] + " " + (string)ViewState["Direction"];
Dv.Sort = SortExpress;
//GridView1.DataSource = Ds.Tables["Suppliers"];
//绑定数据源
GridView1.DataSource = Dv;
GridView1.DataBind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
...{
if (GridView1.EditIndex != -1)
...{
e.Cancel = true;
Literal TxtMsg = new Literal();
TxtMsg.Text = "<script>alert('编辑模式下禁止换行')</script>";
Page.Controls.Add(TxtMsg);
}
else
...{
DetailsView1.Visible = false;
GridView1.EditIndex = e.NewEditIndex;
BindGridView();
}
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
...{
GridView1.EditIndex = -1;
BindGridView();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
...{
string KeyId = GridView1.DataKeys[e.RowIndex].Value.ToString();
string CompanyName = Server.HtmlEncode(((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[1]).Text.ToString());
string ContactName = Server.HtmlEncode(((TextBox)GridView1.Rows[e.RowIndex].Cells[5].Controls[1]).Text.ToString());
string Address = Server.HtmlEncode(((TextBox)GridView1.Rows[e.RowIndex].Cells[6].Controls[1]).Text.ToString());
string City = Server.HtmlEncode(((TextBox)GridView1.Rows[e.RowIndex].Cells[7].Controls[1]).Text.ToString());
string UpdateStr = "UPDATE Suppliers SET CompanyName='" + CompanyName + "',ContactName='" + ContactName + "'," +
"Address='" + Address + "',City='" + City + " 'WHERE SupplierID='" +KeyId + "' ";
SqlConnection UpdateCon = new SqlConnection(ConStr);
SqlCommand UpdateCmd = new SqlCommand(UpdateStr,UpdateCon);
try
... {
string ConStr = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
...{
SortExprssion();
SetGridView();
if (!IsPostBack)
...{
ViewState["CompanyName"] = "SupplierID";
ViewState["Direction"] = "ASC";
BindGridView();
}
}
//设置排序表达式
private void SortExprssion()
...{
GridView1.Columns[3].SortExpression = GridView1.Columns[3].HeaderText.ToString();
}
//设置gridview的属性
private void SetGridView()
...{
GridView1.AllowPaging = true;
GridView1.AllowSorting = true;
}
private void BindGridView()
...{
string QueryCon = "SELECT SupplierID,CompanyName,ContactName,Address,City FROM Suppliers";
SqlConnection NorthWindCon = new SqlConnection(ConStr);
SqlDataAdapter NorthWindDa = new SqlDataAdapter(QueryCon,ConStr);
DataSet Ds = new DataSet();
NorthWindDa.Fill(Ds, "Suppliers");
GridView1.DataKeyNames = new string[] ...{ "SupplierID" };
DataView Dv = Ds.Tables["Suppliers"].DefaultView;
//排序表达式
string SortExpress = (string)ViewState["CompanyName"] + " " + (string)ViewState["Direction"];
Dv.Sort = SortExpress;
//GridView1.DataSource = Ds.Tables["Suppliers"];
//绑定数据源
GridView1.DataSource = Dv;
GridView1.DataBind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
...{
if (GridView1.EditIndex != -1)
...{
e.Cancel = true;
Literal TxtMsg = new Literal();
TxtMsg.Text = "<script>alert('编辑模式下禁止换行')</script>";
Page.Controls.Add(TxtMsg);
}
else
...{
DetailsView1.Visible = false;
GridView1.EditIndex = e.NewEditIndex;
BindGridView();
}
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
...{
GridView1.EditIndex = -1;
BindGridView();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
...{
string KeyId = GridView1.DataKeys[e.RowIndex].Value.ToString();
string CompanyName = Server.HtmlEncode(((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[1]).Text.ToString());
string ContactName = Server.HtmlEncode(((TextBox)GridView1.Rows[e.RowIndex].Cells[5].Controls[1]).Text.ToString());
string Address = Server.HtmlEncode(((TextBox)GridView1.Rows[e.RowIndex].Cells[6].Controls[1]).Text.ToString());
string City = Server.HtmlEncode(((TextBox)GridView1.Rows[e.RowIndex].Cells[7].Controls[1]).Text.ToString());
string UpdateStr = "UPDATE Suppliers SET CompanyName='" + CompanyName + "',ContactName='" + ContactName + "'," +
"Address='" + Address + "',City='" + City + " 'WHERE SupplierID='" +KeyId + "' ";
SqlConnection UpdateCon = new SqlConnection(ConStr);
SqlCommand UpdateCmd = new SqlCommand(UpdateStr,UpdateCon);
try