?? datagrid.aspx.cs
字號:
?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.OleDb;
public partial class DataGrid : System.Web.UI.Page
{
//page加載事件
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
bingtodataGrid();
}
}
//填充數(shù)據(jù)方法
private void bingtodataGrid()
{
OleDbConnection con = DB.mycon();
con.Open();
OleDbCommand com = new OleDbCommand("select distinct bsID,bsName from bmTable", con);
OleDbDataAdapter da = new OleDbDataAdapter(com);
DataSet ds = new DataSet();
da.Fill(ds, "bmTable");
this.DataGrid1.DataKeyField = "bsID";
this.DataGrid1.DataSource = ds.Tables["bmTable"];
this.DataGrid1.DataBind();
}
//實現(xiàn)分頁
protected void DataGrid1_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
{
this.DataGrid1.CurrentPageIndex = e.NewPageIndex;
this.bingtodataGrid();
}
//頁面特殊顯示效果
protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType== ListItemType.AlternatingItem)
{
//鼠標懸停高亮顯示
e.Item.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#fff5ee'");
e.Item.Attributes.Add("onmouseout","this.style.backgroundColor=c");
//刪除詢問
((LinkButton)(e.Item.Cells[3].Controls[0])).Attributes.Add("onclick","return confirm('確認刪除嗎?');");
}
}
//實現(xiàn)排序
protected void DataGrid1_SortCommand(object source, DataGridSortCommandEventArgs e)
{
//判斷目前的排序現(xiàn)狀
if (ViewState["order"] == null)
{
ViewState["order"] = "ASC";
}
else
{
if (ViewState["order"].ToString() == "ASC")
{
ViewState["order"] = "DESC";
}
else
{
ViewState["order"] = "ASC";
}
}
//按排序綁定數(shù)據(jù)
OleDbConnection con = DB.mycon();
con.Open();
OleDbCommand com = new OleDbCommand("select distinct bsID,bsName from bmTable", con);
OleDbDataAdapter da = new OleDbDataAdapter(com);
DataSet ds = new DataSet();
da.Fill(ds, "bmTable");
ds.Tables["bmTable"].DefaultView.Sort = e.SortExpression + " " + ViewState["order"].ToString();
this.DataGrid1.DataSource = ds.Tables["bmTable"].DefaultView;
this.DataGrid1.DataBind();
}
//刪除選中行
protected void DataGrid1_DeleteCommand(object source, DataGridCommandEventArgs e)
{
string bsID = this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
string bmName = ((TextBox)(e.Item.Cells[2].Controls[0])).Text;
OleDbConnection con = DB.mycon();
con.Open();
OleDbCommand cmd = new OleDbCommand("delete from bmTable where bsID='"+bsID.ToString().Trim()+"'",con);
cmd.ExecuteNonQuery();
bingtodataGrid();
}
//編輯按鈕事件
protected void DataGrid1_EditCommand(object source, DataGridCommandEventArgs e)
{
this.DataGrid1.EditItemIndex = e.Item.ItemIndex;
this.bingtodataGrid();
}
//取消按鈕事件
protected void DataGrid1_CancelCommand(object source, DataGridCommandEventArgs e)
{
this.DataGrid1.EditItemIndex = -1;
this.bingtodataGrid();
}
//更新按鈕事件
protected void DataGrid1_UpdateCommand(object source, DataGridCommandEventArgs e)
{
string bsID = this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
string bmName=((TextBox)(e.Item.Cells[2].Controls[0])).Text;
OleDbConnection con = DB.mycon();
con.Open();
OleDbCommand cmd = new OleDbCommand("update bmTable set bsName='" + bmName + "' where bsID='" + bsID + "'", con);
cmd.ExecuteNonQuery();
bingtodataGrid();
}
//顯示checked被選中的行的ID
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "";
foreach (System.Web.UI.WebControls.DataGridItem dl in this.DataGrid1.Items)
{
CheckBox chk = (CheckBox)dl.FindControl("chkselect");
if (chk.Checked)
{
Label1.Text += dl.Cells[1].Text;
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -