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后,为了方便阅读,可以才采用自适应列宽的方式使得单元格的宽度 ...
随机推荐
- Luogu P1052 过河 DP
复习复习DP...都忘了QAQ... 好了这道题我其实是看题解才会的... 方程 f[i]=min(f[i-j]+v[i]) v[i]表示i是不是石头 s<=j<=t 路径压缩引用一下证明 ...
- 118th LeetCode Weekly Contest Pancake Sorting
Given an array A, we can perform a pancake flip: We choose some positive integer k <= A.length, t ...
- HDU 1232 (畅通工程) 并查集经典模板题
Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工程"的目标是使全省任何两个城镇间都可以实现交通 ...
- asp.net mvc 静态化
静态化的基本理解就是,常用的资源以文本形式保存,客户端访问时无需经过程序处理,直接下载 但是不存在的文件需要经过程序处理,文件内容如果需要有更动或删除,则直接删除文件本身 1.IIS Express ...
- [转] 运维小技巧:使用ss命令代替 netstat,和netstat说再见
[From] https://blog.csdn.net/fenglailea/article/details/51810977 ss是Socket Statistics的缩写. 顾名思义,ss命令可 ...
- Oracle笔记-Multitable INSERT 的用法
[转自] http://blog.chinaunix.net/uid-8504518-id-3310531.html 为避免日趋衰退的记忆力,参考官方E文文档<Introduction to ...
- EntityFramework 建立一对一关系
前言:本来要使用实体拆分实现一对一,但发现查询时无法单独查询,影响效率,故改用手动建立一对一关系 例: 实体类: public class TestDbContext : DbContext { pu ...
- Linux混杂设备驱动
1. Linux混杂设备驱动模型 ① 在Linux系统中,存在一类字符设备,它们拥有相同的主设备号(10),但次设备号不同,我们称这类设备为混杂设备(miscdevice).所有混杂设备形成一个链表, ...
- Linux acpi off学习的必要
ACPI是Intel(i386,x86_64,IA64)平台的标准固件规范,绝大部分OS需要从BIOS得到的信息都可以从ACPI得到,并且现在的趋势是未来的任何新的特性相关的信息都只能从ACPI得到. ...
- Merge Sorted Array II
Merge two given sorted integer array A and B into a new sorted integer array. Example A=[1,2,3,4] B= ...