GridView教程(1)

来源:互联网 发布:apps定位软件 编辑:程序博客网 时间:2024/06/01 09:10

1 数据库中保存图片名称 在gridview 中展示图片
(1)前台代码
<asp:GridView ID="gvwattaxhmentlist" runat="server" AutoGenerateColumns="False" SkinID="GvList"
GridLines="None" OnRowCommand="gvwattaxhmentlist_RowCommand" OnRowDataBound="gvwattaxhmentlist_RowDataBound"
DataKeyNames="FileName">
<Columns>
<asp:TemplateField HeaderText="序号">
<ItemTemplate>
<%#Container.DataItemIndex+1%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="附件名称" ItemStyle-HorizontalAlign="Left" ItemStyle-VerticalAlign="Middle">
<ItemTemplate>
<img id="Img1" src='<%#Page.ResolveUrl("~/images/")+Eval("FileTypeImage")%>' runat="server"
alt="文件类型" style="width: 20px; height: 20px" />
<asp:Label ID="Labe2" runat="server" Text='<%# Eval("FileName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

2 gridview 删除的提示
(1)前台代码
<asp:GridView ID="gvwattaxhmentlist" runat="server" AutoGenerateColumns="False" SkinID="GvList"
GridLines="None" OnRowCommand="gvwattaxhmentlist_RowCommand" OnRowDataBound="gvwattaxhmentlist_RowDataBound"
DataKeyNames="FileName">
<Columns>
<asp:TemplateField HeaderText="删除">
<ItemTemplate>
<asp:LinkButton ID="btnDelete" runat="server" CommandArgument='<%# Eval("ID") %>'
CommandName="del" OnClientClick='return window.confirm("是否删除?")'>删除</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
(2)后台代码
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[5].Attributes.Add("onclick", "return confirm('你确认要删除吗?')");

}
}


3 隐藏字段显示(例如有些文件不需要删除数据,有些字段需要删除)
(1)前台代码
<asp:BoundField DataField="Depth" HeaderText="" Visible="false" />
(2)后台代码
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[5].Visible = false;
}


4 字段更改,例如(男女的显示)
(1)前台代码
<asp:TemplateField HeaderText="性别">
<ItemTemplate>
<%#Eval("Sex").ToString().Trim()=="0"?"男":"女" %>
</ItemTemplate>
</asp:TemplateField>

<%# Eval("IsFolder").ToString()=="True"?"1":"0" %>
(2)后台代码(当值少的时候)
Protected void gvUserList_RowDataBound(object sender, GridViewRowEventArgs e)
{
//格式化代码
if(e.Row.Cells[9].Text=='1')
{
e.Row.Cells[9].Text="XXX";
}
else
{
e.Row.Cells[9].Text="YYY";
}
}

5 时间显示
详细说明:http://blog.csdn.net/yycc2008/article/details/4222564
(1)前台代码
<asp:BoundField HeaderText="时间" DataField="Time" DataFormatString="{0:d}" HtmlEncode="false" HtmlEncode ="False" />
<asp:BoundField HeaderText="时间" DataField="Time" DataFormatString="{0:MM-dd}" HtmlEncode="false" HtmlEncode ="False" />
<asp:BoundField HeaderText="时间" DataField="Time" DataFormatString="{0:yyyy-MM-dd}" HtmlEncode="false" HtmlEncode ="False" />


6 gridview 中隔行变色的问题
(1)后台的代码
protected void gvwattaxhmentlist_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#afd6f5';");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor;");
}

7 gridview 中超长字段显示
(1)后台代码
#region gridview 超长字段的显示;
public string SubStr(string sString, int nLeng)
{
if (sString.Length <= nLeng)
{
return sString;
}
string sNewStr = sString.Substring(0, nLeng);
sNewStr = sNewStr + "...";
return sNewStr;
}
#endregion

protected void gvwattaxhmentlist_RowDataBound(object sender, GridViewRowEventArgs e)
{
//判断文字的显示大小
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[4].Text = SubStr(e.Row.Cells[4].Text, 12);
}
}


8 gridview 中字段超长,鼠标滑动到字段显示所有的字段
(1)前台代码
<asp:GridView ID="gdvCom" runat="server" AutoGenerateColumns="False" GridLines="None"
OnRowDataBound="gdvCom_RowDataBound" SkinID="GvListFlat">
<Columns>
<asp:TemplateField HeaderText="沟通情况">
<ItemTemplate>
<asp:Image ID="imgCom" runat="server" ImageUrl='<%# "../images/comm"+ Eval("Result")+".png" %>'
ToolTip='<%# Eval("Result").ToString()=="1"?"需要再次沟通":"沟通成功" %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
(2)后台代码
protected void grv_provincefile_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[1].ToolTip = e.Row.Cells[1].Text;
}
}


9 文件大小显示(b kb mb)
(1)后台代码
protected void gvwattaxhmentlist_RowDataBound(object sender, GridViewRowEventArgs e)
{
//判断文件的大小
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[1].Attributes.Add("style", "padding:0px 2px 10px 0px");
int size = Utils.StrToInt(e.Row.Cells[2].Text.Trim(), -1);
if (size > 1024 * 1024)
{
e.Row.Cells[2].Text = ((double)((long)(size / (1024 * 1024) * 100)) / 100).ToString(".00 ") + "MB";
}
else if (size > 1024)
{
e.Row.Cells[2].Text = ((double)((long)(size / (1024) * 100)) / 100).ToString(".00 ") + "KB";
}
else
{
e.Row.Cells[2].Text = ((double)((long)(size * 100)) / 100).ToString(".00 ") + "B";
}
e.Row.Cells[4].Text = SubStr(e.Row.Cells[4].Text, 12);
//e.Row.Cells[1].Text = SubStr(e.Row.Cells[1].Text, 12);
}
}

10 gridview 删除,查看,修改
(1)前台代码
<asp:GridView ID="grv_provincefile" runat="server" AutoGenerateColumns="False" SkinID="GvList"
GridLines="None" OnRowCommand="grv_provincefile_RowCommand">
<Columns>
<asp:TemplateField HeaderText="操作">
<ItemTemplate>
<asp:LinkButton ID="btnDown" runat="server" CommandArgument='<%# Eval("fileinfoid") %>'
CommandName="down" >下载</asp:LinkButton>
<asp:LinkButton ID="lbldel" runat="server" CommandName="del" CommandArgument='<%# Eval("Code") %>'
OnClientClick='return window.confirm("确定要删除吗?")'>删除</asp:LinkButton>
<asp:LinkButton ID="btnCheck" runat="server" CommandArgument='<%# Eval("DepartmentID") %>'
CommandName="sel">查看</asp:LinkButton>
<asp:LinkButton ID="btnModify" runat="server" CommandArgument='<%# Eval("DepartmentID") %>'
CommandName="upd">修改</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
(2)后台代码
protected void grv_provincefile_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "down")
{
//编写下载代码
}
else if (e.CommandName == "del")
{
//编写删除代码
}
else if (e.CommandName == "sel")
{
//编写查询代码
}
else if (e.CommandName == "upd")
{
//编写修改代码
}
}


11 gridview 中使用CheckBoxField 表示checkbox样式
(1)前台代码
<asp:GridView ID="gdvList" runat="server" SkinID="GvList" GridLines="None" AutoGenerateColumns="False">
<Columns>
<asp:CheckBoxField HeaderText="是否签订合同" DataField="IsContract" />
<asp:CheckBoxField HeaderText="是否已付款" DataField="IsPay" />
</Columns>
</asp:GridView>