日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

asp.net 中Gridview 使用总结

發布時間:2025/7/14 asp.net 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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的問題我會繼續總結的

?


?


?

轉載于:https://www.cnblogs.com/wang-123/archive/2012/03/08/2384828.html

總結

以上是生活随笔為你收集整理的asp.net 中Gridview 使用总结的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。