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://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>


12 以后我遇到的gridview 的checkbox 的問題和導出到excel的問題我會繼續總結的

 


 


 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM