asp.net的GridView控件使用方法大全
前臺(tái).aspx
<asp:Label ID="tplb" runat="server" Text="總頁(yè)數(shù):"></asp:Label>
<asp:Label ID="lblPageCount" runat="server" Text=""></asp:Label>
<asp:Label ID="curLabel" runat="server" Text="當(dāng)前頁(yè):"></asp:Label>
<asp:Label ID="lblPage" Text="1" runat="server"></asp:Label>
<asp:LinkButton ID="lblFirstButton" runat="server" OnClick="lblFirstButton_Click" >|<</asp:LinkButton>
<asp:LinkButton ID="lblPreButton" runat="server" OnClick="lblPreButton_Click" ><</asp:LinkButton>
<asp:LinkButton ID="lblNextButton" runat="server" OnClick="lblNextButton_Click" >></asp:LinkButton>
<asp:LinkButton ID="lblLastButton" runat="server" OnClick="lblLastButton_Click" >>|</asp:LinkButton>
<asp:DropDownList ID="ddlPage" runat="server" Width="40px" AutoPostBack="True"
OnSelectedIndexChanged="ddlPage_SelectedIndexChanged">
<asp:ListItem>10</asp:ListItem>
<asp:ListItem>15</asp:ListItem>
<asp:ListItem>20</asp:ListItem>
<asp:ListItem>30</asp:ListItem>
</asp:DropDownList>
<asp:Label ID="PageSizeLabel" runat="server" Text="條/頁(yè)"></asp:Label>
后臺(tái)
#region分頁(yè)
protected void BindFollowExamInfoGridView(int PersonID)
{
int currentpage = Convert.ToInt32(lblPage.Text);
DataTable dt = new DataTable();
dt = feibf.GetByPersonIDFollowExamInfo(PersonID); //查詢(xún)指定人的隨訪信息記錄
if (dt.Rows.Count > 0)
{
FollowExamInfoGridView.DataSource = dt;
FollowExamInfoGridView.DataBind();
PagedDataSource ps = new PagedDataSource();
ps.DataSource = dt.DefaultView;
ps.AllowPaging = true;
ps.PageSize = Convert.ToInt32(ddlPage.SelectedValue);
lblPageCount.Text = ps.PageCount.ToString();
this.lblPreButton.Enabled = true;
this.lblNextButton.Enabled = true;
ps.CurrentPageIndex = currentpage - 1;
if (currentpage == 1)
{
this.lblPreButton.Enabled = false;
this.lblFirstButton.Enabled = false;
}
else
{
this.lblPreButton.Enabled = true;
this.lblFirstButton.Enabled = true;
}
if (currentpage == ps.PageCount)
{
this.lblNextButton.Enabled = false;
this.lblLastButton.Enabled = false;
}
else
{
this.lblNextButton.Enabled = true;
this.lblLastButton.Enabled = true;
}
FollowExamInfoGridView.DataSource = ps;
FollowExamInfoGridView.DataBind();
}
}
protected void lblPreButton_Click(object sender, EventArgs e)
{
this.lblPage.Text = Convert.ToString(Convert.ToUInt32(lblPage.Text) - 1);
BindFollowExamInfoGridView(Convert.ToInt32(Request.QueryString["PersonID"]));
}
protected void lblNextButton_Click(object sender, EventArgs e)
{
this.lblPage.Text = Convert.ToString(Convert.ToUInt32(lblPage.Text) + 1);
BindFollowExamInfoGridView(Convert.ToInt32(Request.QueryString["PersonID"]));
}
protected void lblFirstButton_Click(object sender, EventArgs e)
{
this.lblPage.Text = "1";
BindFollowExamInfoGridView(Convert.ToInt32(Request.QueryString["PersonID"]));
}
protected void lblLastButton_Click(object sender, EventArgs e)
{
this.lblPage.Text = lblPageCount.Text;
BindFollowExamInfoGridView(Convert.ToInt32(Request.QueryString["PersonID"]));
}
protected void ddlPage_SelectedIndexChanged(object sender, EventArgs e)
{
lblPage.Text = "1";
BindFollowExamInfoGridView(Convert.ToInt32(Request.QueryString["PersonID"]));
}
#endregion
排序
Allowsort = "true"
sortExpression = "ID"
DataView dv = SortBindGrid(dt);
#region排序
protected void FollowExamInfoGridView_Sorting(object sender, GridViewSortEventArgs e)
{
ViewState["sortexpression"] = e.SortExpression;
if (ViewState["sortdirection"] == null)
{
ViewState["sortdirection"] = "asc";
}
else
{
if (ViewState["sortdirection"].ToString() == "asc")
{
ViewState["sortdirection"] = "desc";
}
else
{
ViewState["sortdirection"] = "asc";
}
}
BindFollowExamInfoGridView(Convert.ToInt32(HiddenPersonID.Value));
}
public DataView SortBindGrid(DataTable table)
{
if (table != null)
{
DataView dv = table.DefaultView;
if (ViewState["sortexpression"] != null && ViewState["sortdirection"] != null)
{
dv.Sort = ViewState["sortexpression"].ToString() + " " + ViewState["sortdirection"].ToString();
}
return dv;
}
else
{
return null;
}
}
#endregion
=======自帶分頁(yè)
#region自帶分頁(yè)
protected void FollowExamInfoGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
FollowExamInfoGridView.PageIndex = e.NewPageIndex;
BindFollowExamInfoGridView(Convert.ToInt32(HiddenPersonID.Value));
}
#endregion
選中Grid View 的實(shí)現(xiàn)
#region實(shí)現(xiàn)選中行
<SelectedRowStyle BackColor="AliceBlue" ForeColor="Gray" />
<asp:CommandField ShowSelectButton="True"/>
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onclick", "this.cells[0].childNodes[0].click()");
}
protected void GridViewRegiment_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = GridViewRegiment.SelectedRow;
int RegimentID = Convert.ToInt32(row.Cells[1].Text);
Response.Redirect("UpdateRegimentation.aspx?RegimentID=" + RegimentID);
}
#endregion
顯示顏色和刪除
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//int i;
//for (i = 0; i < GridViewRegiment.Rows.Count; i++)
//{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//當(dāng)有編輯列時(shí),避免出錯(cuò),要加的RowState判斷
if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
{
((ImageButton)e.Row.Cells[2].FindControl("IBtndelete")).Attributes.Add("onclick", "javascript:return confirm('你確認(rèn)要?jiǎng)h除:"" + e.Row.Cells[0].Text + ""嗎?')");
}
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#00A9FF'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#E6F5FA'");
}
//}
}
GridView空的處理
1 顯示無(wú)表頭的空紀(jì)錄,EmptyDataText="沒(méi)有記錄"
2 顯示表頭的空紀(jì)錄
DataTable dt = new DataTable();
dt = feibf.GetByPersonIDFollowExamInfo(PersonID); //查詢(xún)指定人的隨訪信息記錄
DataView dv = SortBindGrid(dt);
if (dt.Rows.Count > 0)
{
FollowExamInfoGridView.DataSource = dv;
FollowExamInfoGridView.DataBind();
}
else
{
//添加新行顯示表頭
dt.Rows.Add(dt.NewRow());
FollowExamInfoGridView.DataSource = dt;
FollowExamInfoGridView.DataBind();
//處理新行
int columnCount = FollowExamInfoGridView.Rows[0].Cells.Count;
//清除掉該空行的全部單元格
FollowExamInfoGridView.Rows[0].Cells.Clear();
//新建單元格對(duì)象
FollowExamInfoGridView.Rows[0].Cells.Add(new TableCell());
//合并單元格
FollowExamInfoGridView.Rows[0].Cells[0].ColumnSpan = columnCount;
//設(shè)置單元格提示內(nèi)容
FollowExamInfoGridView.Rows[0].Cells[0].Style.Value = "text-align:center";
FollowExamInfoGridView.Rows[0].Cells[0].Text = "此人無(wú)隨訪信息";
}
GridView 的導(dǎo)出
EnableEventValidation="false"
#region導(dǎo)出
public override void VerifyRenderingInServerForm(Control control)
{
}
protected void BtnPrint_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.xls");
// 如果設(shè)置為GetEncoding("GB2312");導(dǎo)出的文件將會(huì)出現(xiàn)亂碼?。?!
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.ContentType = "application/ms-excel";//設(shè)置輸出文件類(lèi)型為excel文件。
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.AfficheGV.RenderControl(oHtmlTextWriter);
Response.Output.Write(oStringWriter.ToString());
Response.Flush();
Response.End();
}
#endregion
ToolTip GridView詳細(xì)信息的顯示
前臺(tái)
<script type="text/javascript" >
function Tooltip(cella,cellb)
{
document.getElementById("dc").innerText = "詳細(xì)信息:"+cellb;
document.getElementById("id").innerText = "ID:"+cella;
x= event.clientX+document.body.scrollLeft;
y=event.clientY+document.body.scrollTop+20;
toolTipLayer.style.display="inline";
toolTipLayer.style.left=x;
toolTipLayer.style.top=y;
}
</script>
<div id="toolTipLayer" style=" position:absolute; display:none;
background-color:Aqua; border-color:Blue; border-style:solid;
border-color:Blue; border-width:1px; " >
<table>
<tr><td>Affiche</td></tr>
<tr><td id ="dc"></td></tr>
<tr><td id ="id"> </td></tr>
</table>
</div>
后臺(tái)
protected void AfficheGV_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
{
1 e.Row.Attributes.Add("onmouseover", "Tooltip('" +e.Row.Cells[0].Text.ToString()+ "','"+e.Row.Cells[1].Text.ToString()+"')");
2 e.Row.Attributes.Add("onmouseover","javascript:Tooltip('e.Row.Cells[0].Text');");
3 e.Row.Attributes.Add("onmouseover", "Tooltip('e.Row.Cells[0].Text')");
} }
}
#region自帶編輯
protected void GVAffiche_RowEditing(object sender, GridViewEditEventArgs e)
{
GVAffiche.EditIndex = e.NewEditIndex;
BindGVAffiche();
}
protected void GVAffiche_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GVAffiche.EditIndex = -1;
MyAffiche.DelAfficeBF( Convert.ToInt32(GVAffiche.DataKeys[e.RowIndex].Value.ToString()));
BindGVAffiche();
}
protected void GVAffiche_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int id = Convert.ToInt32(((TextBox)(GVAffiche.Rows[e.RowIndex].Cells[0].Controls[0])).Text.ToString().Trim());
string dc = ((TextBox)(GVAffiche.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim();
MyAffiche.UpdateAfficheBf(id,dc);
GVAffiche.EditIndex = -1;
BindGVAffiche();
}
protected void GVAffiche_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GVAffiche.EditIndex = -1;
BindGVAffiche();
}
#endregion
#region樣式的控制
protected void GVAffiche_RowDataBound(object sender, GridViewRowEventArgs e)
{
//首先判斷是否是數(shù)據(jù)行
if (e.Row.RowType == DataControlRowType.DataRow)
{
//當(dāng)有編輯列時(shí),避免出錯(cuò),要加的RowState判斷
if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
{
((Button)e.Row.Cells[7].FindControl("btnDel")).Attributes.Add("onclick","javascript:return confirm('你確認(rèn)刪除:"" + e.Row.Cells[1].Text + ""')");
//當(dāng)鼠標(biāo)停留時(shí)更改背景色
e.Row.Attributes.Add("onmouseover", "color=this.style.backgroundColor;this.style.backgroundColor='#00A9FF'");
//當(dāng)鼠標(biāo)移開(kāi)時(shí)還原背景色
e.Row.Attributes.Add("onmouseout","this.style.backgroundColor=color");
GVAffiche.Attributes.Add("style", "word-break:keep-all;word-wrap:normal");
//GVAffiche.Attributes.Add("style", "word-break:break-all;word-wrap:break-word");
if (e.Row.Cells[1].Text == "444")
{
e.Row.Cells[1].BackColor = System.Drawing.Color.Red;
}
}
}
}
#endregion
以上是GridView控件的一些基礎(chǔ)使用大全,希望對(duì)大家有所用處。
- ASP.NET 2.0/3.5中直接操作Gridview控件插入新記錄
- asp.net GridView控件鼠標(biāo)移動(dòng)某行改變背景顏色(方法一)
- asp.net GridView控件中模板列CheckBox全選、反選、取消
- asp.net GridView控件中實(shí)現(xiàn)全選的解決方案
- ASP.NET2.0中用Gridview控件操作數(shù)據(jù)的代碼
- ASP.NET GridView控件在列上格式化時(shí)間及DataFormatString使用
- asp.net中GridView控件遍歷的小例子
- Asp.net的GridView控件實(shí)現(xiàn)單元格可編輯方便用戶(hù)使用
- ASP.NET4 GridView的四種排序樣式詳解
- ASP.NET使用GridView導(dǎo)出Excel實(shí)現(xiàn)方法
- asp.net gridview分頁(yè):第一頁(yè) 下一頁(yè) 1 2 3 4 上一頁(yè) 最末頁(yè)
- ASP.NET中為GridView添加刪除提示框的方法
- asp.net中GridView數(shù)據(jù)鼠標(biāo)移入顯示提示信息
- 如何用jQuery實(shí)現(xiàn)ASP.NET GridView折疊伸展效果
- ASP.NET GridView中加入RadioButton不能單選的解決方案
- 靈活掌握asp.net中g(shù)ridview控件的多種使用方法(上)
- 靈活掌握asp.net中g(shù)ridview控件的多種使用方法(下)
相關(guān)文章
Entity?Framework使用Code?First的實(shí)體繼承模式
本文詳細(xì)講解了Entity?Framework使用Code?First的實(shí)體繼承模式,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03異步調(diào)用webservice返回responseXML為空的問(wèn)題解決方法
異步調(diào)用webservice返回responseXML為空,詳細(xì)很多朋友都遇到過(guò)類(lèi)似的問(wèn)題吧,接下來(lái)為大家提供詳細(xì)的解決方案,感興趣的朋友可以參考下哈2013-04-04.NET Core API CORS的實(shí)現(xiàn)
這篇文章主要介紹了.NET Core API CORS的實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08MessagePack 和System.Text.Json 序列化和反序列化性能及對(duì)比分析
這篇文章主要介紹了MessagePack 和System.Text.Json 序列化和反序列化性能及對(duì)比分析,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-01-01ASP.NET編程獲取網(wǎng)站根目錄方法小結(jié)
這篇文章主要介紹了ASP.NET編程獲取網(wǎng)站根目錄方法,較為詳細(xì)的分析了ASP.NET針對(duì)網(wǎng)站目錄及物理路徑的操作技巧,并給出了實(shí)例予以總結(jié),需要的朋友可以參考下2015-11-11.net 中的SqlConnection連接池機(jī)制詳解
.net 中通過(guò) SqlConnection 連接 sql server,我們會(huì)發(fā)現(xiàn)第一次連接時(shí)總是很耗時(shí),但后面連接就很快,這個(gè)其實(shí)和SqlConnection 的連接池機(jī)制有關(guān)2013-04-04ASP.NET中等安全模式的一些經(jīng)驗(yàn)分享
如果你正在開(kāi)發(fā)一個(gè)通用型的Web產(chǎn)品,比如BBS、CMS、BLOG這類(lèi)的,那么,建議你閱讀以下本篇文章。2010-01-01.net搜索查詢(xún)并實(shí)現(xiàn)分頁(yè)實(shí)例
.net搜索查詢(xún)并實(shí)現(xiàn)分頁(yè)實(shí)例,需要的朋友可以參考一下2013-03-03