asp.net的GridView控件使用方法大全
前台.aspx
<asp:Label ID="tplb" runat="server" Text="总页数:"></asp:Label>
<asp:Label ID="lblPageCount" runat="server" Text=""></asp:Label>
<asp:Label ID="curLabel" runat="server" Text="当前页:"></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="条/页"></asp:Label>
后台
#region分页
protected void BindFollowExamInfoGridView(int PersonID)
{
int currentpage = Convert.ToInt32(lblPage.Text);
DataTable dt = new DataTable();
dt = feibf.GetByPersonIDFollowExamInfo(PersonID); //查询指定人的随访信息记录
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
=======自带分页
#region自带分页
protected void FollowExamInfoGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
FollowExamInfoGridView.PageIndex = e.NewPageIndex;
BindFollowExamInfoGridView(Convert.ToInt32(HiddenPersonID.Value));
}
#endregion
选中Grid View 的实现
#region实现选中行
<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)
{
//当有编辑列时,避免出错,要加的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('你确认要删除:"" + 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 显示无表头的空纪录,EmptyDataText="没有记录"
2 显示表头的空纪录
DataTable dt = new DataTable();
dt = feibf.GetByPersonIDFollowExamInfo(PersonID); //查询指定人的随访信息记录
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();
//新建单元格对象
FollowExamInfoGridView.Rows[0].Cells.Add(new TableCell());
//合并单元格
FollowExamInfoGridView.Rows[0].Cells[0].ColumnSpan = columnCount;
//设置单元格提示内容
FollowExamInfoGridView.Rows[0].Cells[0].Style.Value = "text-align:center";
FollowExamInfoGridView.Rows[0].Cells[0].Text = "此人无随访信息";
}
GridView 的导出
EnableEventValidation="false"
#region导出
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");
// 如果设置为GetEncoding("GB2312");导出的文件将会出现乱码!!!
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.ContentType = "application/ms-excel";//设置输出文件类型为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详细信息的显示
前台
<script type="text/javascript" >
function Tooltip(cella,cellb)
{
document.getElementById("dc").innerText = "详细信息:"+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>
后台
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)
{
//首先判断是否是数据行
if (e.Row.RowType == DataControlRowType.DataRow)
{
//当有编辑列时,避免出错,要加的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('你确认删除:"" + e.Row.Cells[1].Text + ""')");
//当鼠标停留时更改背景色
e.Row.Attributes.Add("onmouseover", "color=this.style.backgroundColor;this.style.backgroundColor='#00A9FF'");
//当鼠标移开时还原背景色
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控件的一些基础使用大全,希望对大家有所用处。
相关文章
- 这篇文章主要为大家详细介绍了ASP.NET购物车的实现过程,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-22
- 在开发过程中,使用Visual Studio的断点调试功能可以很方便帮我们调试发现程序存在的错误,同样Visual Studio也支持对SQL Server里面的存储过程进行调试,下面就让我们看看具体的调试方法。...2021-09-22
ASP.NET Core根据环境变量支持多个 appsettings.json配置文件
这篇文章主要介绍了ASP.NET Core根据环境变量支持多个 appsettings.json配置文件,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-09-22- 这篇文章主要介绍了记一次EFCore类型转换错误及解决方案,帮助大家更好的理解和学习使用asp.net core,感兴趣的朋友可以了解下...2021-09-22
- 这篇文章主要介绍了C#实现3步手动建DataGridView的方法,实例分析了C#实现手动创建DataGridView的原理与技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
详解ASP.NET Core 中基于工厂的中间件激活的实现方法
这篇文章主要介绍了ASP.NET Core 中基于工厂的中间件激活的实现方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-09-22- 这篇文章主要介绍了C#中DataGridView动态添加行及添加列的方法,涉及C#中DataGridView针对行与列动态操作的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
asp.net通过消息队列处理高并发请求(以抢小米手机为例)
这篇文章主要介绍了asp.net通过消息队列处理高并发请求(以抢小米手机为例),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-09-22ASP.NET 2.0中的数据操作:使用两个DropDownList过滤的主/从报表
在前面的指南中我们研究了如何显示一个简单的主/从报表, 该报表使用DropDownList和GridView控件, DropDownList填充类别,GridView显示选定类别的产品. 这类报表用于显示具有...2016-05-19ASP.NET单选按钮控件RadioButton常用属性和方法介绍
RadioButton又称单选按钮,其在工具箱中的图标为 ,单选按钮通常成组出现,用于提供两个或多个互斥选项,即在一组单选钮中只能选择一个...2021-09-22C#中datagridview使用tooltip控件显示单元格内容的方法
这篇文章主要介绍了C#中datagridview使用tooltip控件显示单元格内容的方法,实例分析了C#控件的相关使用技巧,需要的朋友可以参考下...2020-06-25ASP.NET中iframe框架点击左边页面链接 右边显示链接页面内容
这篇文章主要介绍了ASP.NET中iframe框架点击左边页面链接,右边显示链接页面内容的实现代码,感兴趣的小伙伴们可以参考一下...2021-09-22- ASP.NET Web API具有与ASP.NET MVC类似的编程方式,ASP.NET Web API不仅仅具有一个完全独立的消息处理管道,而且这个管道比为ASP.NET MVC设计的管道更为复杂,功能也更为强大。下面创建一个简单的Web API项目,需要的朋友可以参考下...2021-09-22
- 这篇文章主要介绍了ASP.NET连接MySql数据库的2个方法及示例,使用的是MySQL官方组件和ODBC.NET,需要的朋友可以参考下...2021-09-22
- 这篇文章主要介绍了C#中GridView动态添加列的实现方法,涉及C#中GridView的相关使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
- 下面小编就为大家带来一篇浅析GridView中显示时间日期格式的问题。小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2021-09-22
- 这篇文章主要介绍了Asp.Net使用Bulk实现批量插入数据的方法,对于进行asp.net数据库程序设计非常有借鉴价值,需要的朋友可以参考下...2021-09-22
在ASP.NET 2.0中操作数据之二十九:用DataList和Repeater来显示数据
本文主要讲解ASP.NET 2.0中如何使用DataList 和 Repeater 来呈现数据,DataList包含一个table标记,而Repeater不会添加任何额外的代码,个人在实际开发中更推荐使用Repeater。...2021-09-22- 这篇文章主要介绍了获取DataTable选择第一行某一列值,需要的朋友可以参考下...2021-09-22
- 这篇文章介绍了Asp.net动态生成html页面的方法,有需要的朋友可以参考一下...2021-09-22