asp.net 中Gridview 使用总结
1 數(shù)據(jù)庫中保存圖片名稱 在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('你確認(rèn)要刪除嗎?')");
????????? }
}
3 隱藏字段顯示(例如有些文件不需要刪除數(shù)據(jù),有些字段需要刪除)
(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)后臺代碼(當(dāng)值少的時候)
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 時間顯示
詳細(xì)說明: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 中字段超長,鼠標(biāo)滑動到字段顯示所有的字段
(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 的問題和導(dǎo)出到excel的問題我會繼續(xù)總結(jié)的
?
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/wang-123/archive/2012/03/08/2384828.html
總結(jié)
以上是生活随笔為你收集整理的asp.net 中Gridview 使用总结的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【转】Matlab中特殊符号的写法
- 下一篇: Modern C++ Design 学习