GridView——标题行自适应单元格列宽与选中单元格变色
首先看效果图:

主要实现——
1.前台GridView代码:
<asp:GridView Height="100%" Width="98%" ID="DataGrid1" runat="server" AutoGenerateColumns="False"
EnableModelValidation="True" Font-Size="9pt" style="overflow:scroll"
onrowcommand="DataGrid1_RowCommand" AllowPaging="True"
onpageindexchanging="DataGrid1_PageIndexChanging" PageSize=""
onrowdatabound="DataGrid1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="图片">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="false"
CommandArgument ='<%# Eval("SEQ_NO") %>' CommandName="ViewPicture" Text="图片"></asp:LinkButton>
</ItemTemplate>
<HeaderStyle />
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:TemplateField>
<asp:BoundField DataField="CUSTOMS_NAME" HeaderText="关区">
<HeaderStyle></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundField>
主要事件是: onrowdatabound="DataGrid1_RowDataBound"
2.后台代码:
protected void DataGrid1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//固定标题行列宽
if (e.Row.RowType == DataControlRowType.Header)//当前是标题行
{
//保持列不变形有两种方法:
//方法一是设置cell的自动换行属性为false,方法二是用html标记的方式实现不换行; 就是一个空格,可以让网格线和里面的内容留有一定的距离保持美观。
for (int i = ; i < e.Row.Cells.Count; i++)
{
////方法一:
//e.Row.Cells[i].Text = " " + e.Row.Cells[i].Text + " ";
//e.Row.Cells[i].Wrap = false; //方法二:
e.Row.Cells[i].Text = "<nobr> " + e.Row.Cells[i].Text + " </nobr>";
}
}
else if (e.Row.RowType == DataControlRowType.DataRow)//当前是数据行
{
e.Row.Attributes.Add("onclick", e.Row.ClientID.ToString() + ".checked=true;selectx(this)");//点击行变色
// e.Row.Attributes.Add("onmouseover", "if(this!=prevselitem){this.style.backgroundColor='#Efefef'}");//当鼠标停留时更改背景色
// e.Row.Attributes.Add("onmouseout", "if(this!=prevselitem){this.style.backgroundColor='#ffffff'}");//当鼠标移开时还原背景色
//e.Row.Attributes["style"] = "Cursor:hand"; //设置悬浮鼠标指针形状为"小手"
//GridView1.HeaderRow.Cells[10].Visible = false;
// e.Row.Cells[10].Visible = false;//隐藏选择按钮
//String evt = Page.ClientScript.GetPostBackClientHyperlink(sender as System.Web.UI.WebControls.GridView, "Select$" + e.Row.RowIndex.ToString());
// e.Row.Attributes.Add("onclick", evt);//执行选择行GridView1_SelectedIndexChanged事件 //固定单元格行列宽
for (int i = ; i < e.Row.Cells.Count; i++)
{
////方法一:
//e.Row.Cells[i].Text = " " + e.Row.Cells[i].Text + " ";
e.Row.Cells[i].Wrap = false;
}
} }
注意:针对标题行,方法一和方法二都试用,针对数据行,则只适用方法一,且方法一的 e.Row.Cells[i].Text = " " + e.Row.Cells[i].Text + " "; 要注释掉 ,即前后增加空格不能用,因为数据行如果遇到模板列TemplateField,列中的数据将会被替换成空内容。
选中的单元格变色,需要加上如下JS代码:
<script type="text/javascript">
var prevselitem = null;
function selectx(row) {
if (prevselitem != null) {
prevselitem.style.backgroundColor = '#DDEDFF';
}
row.style.backgroundColor = 'PeachPuff';
prevselitem = row; }
</script>
此外,如果想让GridView出现横向滚动条,需要在外面套一个如下的div: <div style="width: 100%; height: auto; overflow: scroll;text-align:center;">
GridView——标题行自适应单元格列宽与选中单元格变色的更多相关文章
- C# Excel行高、列宽、合并单元格、单元格边框线、冻结
private _Workbook _workBook = null;private Worksheet _workSheet = null;private Excel.Application _ex ...
- XtraReport交叉表自适应行高及最佳列宽
1.自适应行头的行高,绑定CustomRowHeight事件,代码如下: private Graphics gr = Graphics.FromHwnd(IntPtr.Zero); private v ...
- excel制作田字格,excel行高磅,列宽1/10英寸;
打开一个excel表格,发现列宽是行高的4倍: 开始-格式中查看,发现行高14.25磅,列宽8.38*1/10英寸: 网上百度,了解1英寸=72磅: 那么列宽8.38=60.336磅: 60.336英 ...
- c# Excel 行高、列宽、合并单元格、单元格边框线、冻结
http://www.2cto.com/kf/201007/52724.html 新增选择整列: Range = ((Range)(WALeTieSheet.Cells[1, PwCStartCol_ ...
- 【转载】jxl操作excel 字体 背景色 合并单元格 列宽等 .
package com.email.jav; import java.io.File;import java.io.IOException;import java.net.URL; import jx ...
- C#导出带有格式的Excel(列宽,合并单元格,显示边框线,加背景颜色等)
源地址:http://blog.sina.com.cn/s/blog_74f702e60101au55.html 导出excel相关设置:http://blog.csdn.net/wanmingtom ...
- dev gridview自动列宽和单元、行、checkbox选中和多选
#region 自动列宽 for (int I = 0; I < gridView1.Columns.Count; I++) { this.gridView1.BestFitColumns(); ...
- 20180429 xlVBA套打单据自适应列宽
Sub TestAutoAdjustColumnWidthBaseOnModel() Set ModelSheet = ThisWorkbook.Worksheets("单据模板" ...
- NPOI自适应列宽
当本地没有安装Excel,但是又想导出Excel,采用Office插件是行不通的,NPOI是导出Excel的一个开源的插件.在导出Excel后,为了方便阅读,可以才采用自适应列宽的方式使得单元格的宽度 ...
随机推荐
- 隐藏入口文件的apache配置
AllowOverride None->AllowOverride All #LoadModule rewrite_module modules/mod_rewrite.so->LoadM ...
- js 中的! 和 !! 的区别
Js中!的用法是比较灵活的,它除了做逻辑运算常常会用!做类型判断,可以用!与上对象来求得一个布尔值,1.!可将变量转换成boolean类型,null.undefined和空字符串取反都为false,其 ...
- jvm双亲委派模型
其实,双亲委派模型并不复杂.自定义类加载器也不难!随便从网上搜一下就能搜出一大把结果,然后copy一下就能用.但是,如果每次想自定义类加载器就必须搜一遍别人的文章,然后复制,这样显然不行.可是自定义类 ...
- C# MODBUS 通信
背景 电厂有多组监控设备,需要在指定的设备上显示某些数据(其他设备对接过来的).通信协议是modbus主从结构. 源码: http://download.csdn.net/download/wolf1 ...
- (转)Linux 最大进程数
Linux 最大进程数 原文:https://www.cnblogs.com/pangguoping/p/5792075.html 前言 使用环境:centos 7系统 一.查看用户打开的最大进程数 ...
- [TimesTen]TT7001: User authentication failed
在使用sqldeveloper连接TimesTen一直报[TimesTen][TimesTen 11.2.2.8.0 ODBC Driver][TimesTen]TT7001: User authen ...
- .NET平台常用框架整理
基于.NET平台常用的框架整理 转自:http://www.cnblogs.com/hgmyz/p/5313983.html 首先谢谢楼主,以后备用 自从学习.NET以来,优雅的编程风格,极度简单 ...
- Excel&&word&&PPT
1. Excel 1.1 制作下拉框 选中单元格或列--> 菜单"数据" --> "数据验证"-->"设置" --> ...
- repoquery详解——linux查看包依赖关系的神器
repoquery是yum扩展工具包yum-utils中的一个工具,所有如果你没有repoquery命令的话,可以先 sudo yum install yum-utils 安装yum-utils包.是 ...
- 数组和json的相互转换
json_encode() <?php /*****一维数组*********/ //有键 $arr = array( 'a'=>1, 'b'=>2, 'c'=>3, ); $ ...