当前位置:编程学习 > asp >>

asp.net 中Gridview 使用总结

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://www.zzzyk.com/kf/201203/122342.html 
(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)
            {
         &

补充:Web开发 , ASP.Net ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,