Gridview应用技巧收集
2007-12-14 09:45
423 查看
在某些情况下,DataGrid或者GridView的默认样式并不能满足日益高涨的用户的要求,很多人追求美观的样式。对表头设定背景也是其中的一个方面,那么有什么好的方法可以达到这一要求呢,我的方法如下:
DataGrid:
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == System.Web.UI.WebControls.ListItemType.Header)
{
e.Item.Attributes.Add("style", "background-image:url('background.gif')");
}
}
GridView:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Attributes.Add("style", "background-image:url('background.gif')");
}
}
在DataGrid和GridView中对表头设定背景图片,不需要后台代码。
HTML code <asp:GridViewrunat="server" ID="gvStatList" AllowPaging="false" Width="100%" CssClass="grid"
AutoGenerateColumns="false" Visible="true" ShowFooter="false">
<HeaderStyleCssClass="grid-head" />
</asp:GridView>CSS code
HTML code
[/code]
[/code]
DataGrid:
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == System.Web.UI.WebControls.ListItemType.Header)
{
e.Item.Attributes.Add("style", "background-image:url('background.gif')");
}
}
GridView:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Attributes.Add("style", "background-image:url('background.gif')");
}
}
在DataGrid和GridView中对表头设定背景图片,不需要后台代码。
HTML code <asp:GridViewrunat="server" ID="gvStatList" AllowPaging="false" Width="100%" CssClass="grid"
AutoGenerateColumns="false" Visible="true" ShowFooter="false">
<HeaderStyleCssClass="grid-head" />
</asp:GridView>CSS code
.grid-head { font-size: 14px; font-weight: normal; color: #FFFFFF; background-image: url(../images/grid-bg.gif); text-align:center; vertical-align:middle; height: 28px; }
假设这样一种模型,显示用GridView,数据源用DataSet。每次获取10条信息,按每页10条分页。 有的时候数据可能不足10条,而客户要求不足10条显示为空行,以下代码解决了这个问题。 private DataTable FillBlank(int pageSize, DataTable dt)if (dt.Rows.Count < pageSize) { for (int i = dt.Rows.Count - 1; i < pageSize; i++) { DataRow dr = dt.NewRow(); dt.Rows.Add(dr); } } return dt; } private void BindGrid(DataTable dt) { GridView.DataSource = dt; GridView.DataBind(); }
让GridView产生纵向横向的滚动条。这样就不会吧页面撑打了。
CSS code
<style type="text/css" id="print" media="print"> #leftSide, #footerSide { display:none; } </style> <style type="text/css" > .Freezing { position:relative ; table-layout:fixed; top:expression(this.offsetParent.scrollTop); z-index: 10; } .Freezing th{text-overflow:ellipsis;overflow:hidden;white-space: nowrap;padding:2px;} </style>
HTML code
<div id="Div1" style="overflow: scroll; height: 140px;width:638px" > Gridview 在这里 </div>
让GridView响应单击,双击事件的简单方法: 添加选择命令字段,并设置为不可见! 如DataView ID 为 DataView1.然后在RowDataBound事件中,加上: C# code 单击 if (e.Row.RowType == DataControlRowType.DataRow) { e.Row.Attributes.Add("onclick", "__doPostBack('GridView1','Select$" + e.Row.RowIndex + "');"); }[code]双击 if (e.Row.RowType == DataControlRowType.DataRow) { e.Row.Attributes.Add("ondbclick", "__doPostBack('GridView1','Select$" + e.Row.RowIndex + "');"); }
[/code]
合并gridview列(模板列)相同单元格 /// <summary> /// 合并gridview列(模板列)相同单元格 /// </summary> /// <param name="GridViewList"></param> /// <param name="cellNum">合并第几列单元格</param> // public static void CombineCell(GridView GridViewList, int cellNum) { int i = 0, rowSpanNum = 1; while (i < GridViewList.Rows.Count - 1) { GridViewRow gvr = GridViewList.Rows[i]; for (++i; i < GridViewList.Rows.Count; i++) { GridViewRow gvrNext = GridViewList.Rows[i]; Label lblproject1 = gvr.Cells[cellNum].FindControl("lblproject") as Label; Label lblproject2 = gvrNext.Cells[cellNum].FindControl("lblproject") as Label; if (lblproject1.Text == lblproject2.Text) { gvrNext.Cells[cellNum].Visible = false; rowSpanNum++; } else { gvr.Cells[cellNum].RowSpan = rowSpanNum; rowSpanNum = 1; break; } if (i == GridViewList.Rows.Count - 1) { gvr.Cells[cellNum].RowSpan = rowSpanNum; } } } } #endregion
[/code]
相关文章推荐
- [GridView应用技巧]让鼠标移动到哪行改变背景色
- GridView,DataList,Repeater控件应用技巧
- ToolTip的应用技巧收集
- GridView技巧收集
- 收集:javascript网页精华代码--弹出窗口的制作和应用技巧
- CRichEditCtrl相关应用技巧收集
- GridView的高级应用技巧
- Gridview应用技巧——如何为行添加事件
- GridView应用技巧------如何在没数据的时候显示表格以及填充空白行
- 【ASP.NET】GridView中的技巧收集
- Gridview应用技巧——如何为行添加事件
- C#入门(八)—GridView控件的应用技巧
- GridView,DataList,Repeater控件应用技巧
- ExtAspNet应用技巧(八) - log4net配置与使用
- 手把手教您Python多线程应用技巧
- VS 应用技巧
- 改造asp.net日历控件为弹出式日期选择控件和GridView呈现长文本字段的两个技巧
- Android HttpURLConnection应用技巧分享
- GridView 技巧(转)
- GridView使用技巧之:如何在RowCommand里获取模板列里绑定的内容?