首先看效果图:

主要实现——

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——标题行自适应单元格列宽与选中单元格变色的更多相关文章

  1. C# Excel行高、列宽、合并单元格、单元格边框线、冻结

    private _Workbook _workBook = null;private Worksheet _workSheet = null;private Excel.Application _ex ...

  2. XtraReport交叉表自适应行高及最佳列宽

    1.自适应行头的行高,绑定CustomRowHeight事件,代码如下: private Graphics gr = Graphics.FromHwnd(IntPtr.Zero); private v ...

  3. excel制作田字格,excel行高磅,列宽1/10英寸;

    打开一个excel表格,发现列宽是行高的4倍: 开始-格式中查看,发现行高14.25磅,列宽8.38*1/10英寸: 网上百度,了解1英寸=72磅: 那么列宽8.38=60.336磅: 60.336英 ...

  4. c# Excel 行高、列宽、合并单元格、单元格边框线、冻结

    http://www.2cto.com/kf/201007/52724.html 新增选择整列: Range = ((Range)(WALeTieSheet.Cells[1, PwCStartCol_ ...

  5. 【转载】jxl操作excel 字体 背景色 合并单元格 列宽等 .

    package com.email.jav; import java.io.File;import java.io.IOException;import java.net.URL; import jx ...

  6. C#导出带有格式的Excel(列宽,合并单元格,显示边框线,加背景颜色等)

    源地址:http://blog.sina.com.cn/s/blog_74f702e60101au55.html 导出excel相关设置:http://blog.csdn.net/wanmingtom ...

  7. dev gridview自动列宽和单元、行、checkbox选中和多选

    #region 自动列宽 for (int I = 0; I < gridView1.Columns.Count; I++) { this.gridView1.BestFitColumns(); ...

  8. 20180429 xlVBA套打单据自适应列宽

    Sub TestAutoAdjustColumnWidthBaseOnModel() Set ModelSheet = ThisWorkbook.Worksheets("单据模板" ...

  9. NPOI自适应列宽

    当本地没有安装Excel,但是又想导出Excel,采用Office插件是行不通的,NPOI是导出Excel的一个开源的插件.在导出Excel后,为了方便阅读,可以才采用自适应列宽的方式使得单元格的宽度 ...

随机推荐

  1. ui-grid使用详解

    HTML <pre name="code" class="html"><!--ui-grid css--> <link rel=& ...

  2. Kibana6.x.x源码结构分析笔记

  3. 安装Linux虚拟机到执行Java程序

    1.安装VMware 2.在VMware里安装 CentOs 镜像(CentOS-7.2-x86_64-DVD-1511.iso) 3.启动CentOs后如果不能上网,或者 没有 ifconfig命令 ...

  4. [转] electron实战开发详细流程

    [From] http://www.myk3.com/arc-8856.html 很久没有更新博客,人越来越懒了,唉 说好做的electron教程距离上次玩electron已经过去了好几个月了.. 这 ...

  5. poj1318 Word Amalgamation 字符串排序(qsort)

    Word Amalgamation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9794   Accepted: 4701 ...

  6. css引入 以及选择器040

    css的介绍: css(Cascading Style Sheet)  层叠样式表 作用就是给HTML页面标签议案家各种样式 定义网页效果 简单来说 就是讲网页内容和显示样式进行分离 , 提高了显示功 ...

  7. C#接收emoji表情存到sqlserver数据库

    1.将就的方法已找到,在保存前,Emoji表情字符串进行utf-8编码,然后写入数据表的nvarchar(max)字段,取出时再进行解码即可. c#的写法如下: 写入数据表前编码: string sH ...

  8. myEclipse注册码地址2019年最新

    myEclipse注册码地址2019年最新 https://www.sojson.com/myeclipse8.5/

  9. Java变量类型,实例变量 与局部变量 静态变量

    实例变量: 实例变量在类中声明,但在方法的外面,构造函数或任何块. 当空间分配给某个对象在堆中,插槽为每个实例变量创建值. 当一个对象与使用关键字 “new” 来创建,在对象被销毁销毁创建的实例变量. ...

  10. 如何在ThinkPHP中开启调试模式

    1.为什么使用调试模式? 因为调试会在我们的模板页的最后增加一些trace信息. 2.什么是trace信息? 是ThinkPHP为我们提供好的一些包含了系统运行时间.占用内存.加载时间.请求的协议.. ...