📄 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();
}
}
//填充数据方法
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();
}
//实现分页
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('确认删除吗?');");
}
}
//实现排序
protected void DataGrid1_SortCommand(object source, DataGridSortCommandEventArgs e)
{
//判断目前的排序现状
if (ViewState["order"] == null)
{
ViewState["order"] = "ASC";
}
else
{
if (ViewState["order"].ToString() == "ASC")
{
ViewState["order"] = "DESC";
}
else
{
ViewState["order"] = "ASC";
}
}
//按排序绑定数据
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 + -