如何给Header添加样式?

「已注销」 2010-08-29 06:09:47
RT。。我继承GridView控件,自己添加了些新功能。现在想在

protected override void OnRowDataBound(GridViewRowEventArgs e)


方法里给表头添加样式


e.Row.RowType == DataControlRowType.DataRow//判断行类型为表头


我现在添加样式不知道如何定位到该行,我想把该行的背景色置为黑,并且不透明
...全文
218 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuyq11 2010-08-29
  • 打赏
  • 举报
回复
protected override void OnRowDataBound(GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// _cssClassMouseOver不是空则执行
if (!string.IsNullOrEmpty(this._cssClassMouseOver))
{
// 在<tr>上onmouseover时<tr>的样式(css类名)
e.Row.Attributes.Add("onmouseover", "this.className = '" + this._cssClassMouseOver + "'");
// 样式的名称(css类名)
string cssClassMouseOut = "";
// 根据RowState的不同,onmouseout时<tr>的不同样式(css类名)
switch (e.Row.RowState)
{
case DataControlRowState.Alternate:
cssClassMouseOut = base.AlternatingRowStyle.CssClass;
break;
case DataControlRowState.Edit:
cssClassMouseOut = base.EditRowStyle.CssClass;
break;
case DataControlRowState.Normal:
cssClassMouseOut = base.RowStyle.CssClass;
break;
case DataControlRowState.Selected:
cssClassMouseOut = base.SelectedRowStyle.CssClass;
break;
default:
cssClassMouseOut = "";
break;
}
// 增加<tr>的dhtml事件onmouseout
e.Row.Attributes.Add("onmouseout", "this.className = '" + cssClassMouseOut + "'");
}
}
base.OnRowDataBound(e);
}
mngzilin 2010-08-29
  • 打赏
  • 举报
回复
    protected override void OnRowDataBound(GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
e.Row.Attributes.Add("style","width:300px;background-color:red;height:30px;");
base.OnRowDataBound(e);
}
阿彪兄 2010-08-29
  • 打赏
  • 举报
回复
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace CIPAceLicense
{
/// <summary>
/// DataGridComboBoxColumn summary。
/// </summary>
public class DataGridComboBoxColumn : DataGridColumnStyle
{

private DataGridComboBox m_comboBox;
private bool m_edit;
public DataGridComboBoxColumn()
{
this.m_comboBox = new DataGridComboBox();
this.m_comboBox.Visible = false;
this.m_comboBox.DropDownStyle = ComboBoxStyle.DropDownList;
this.m_comboBox.Leave += new EventHandler(m_comboBox_Leave);
this.m_comboBox.SelectionChangeCommitted += new EventHandler(m_comboBox_SelectedIndexChanged);
this.m_edit = false;
}



public ComboBox comboBox
{
get
{
return m_comboBox;
}
}


//comboBox event
//lost fucos comboBox hide
private void m_comboBox_Leave(object sender, EventArgs e)
{
this.m_comboBox.Hide();
}

//选定项发生更改并提交更改通知用户开始编辑列
private void m_comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
this.m_edit = true;
base.ColumnStartedEditing((Control)sender);
}

//重写DataGridColunmStyle
protected override void SetDataGridInColumn(DataGrid value)
{
//将comboBox加入到DataGrid控件集合中
//确保正确的DataGrid scrolling
value.Controls.Add(this.m_comboBox);
base.SetDataGridInColumn(value);
}


//当 DataGridColumnStyle 方法的 Commit 方法返回 false 时,Abort 方法被 DataGrid 使用。
//在这种情况下,列值滚动回原先的值。
//在返回之前,DataGridColumnStyle 必须结束所有编辑操作。使用 Abort 方法来实现该操作。
//System.Windows.Forms.DataGrid 控件的 EndEdit 方法间接调用 Abort(如果其 ShouldAbort 参数设置为 true)。
protected override void Abort(int rowNum)
{
this.m_edit = false;
Invalidate();
this.m_comboBox.Hide();
}


//准备单元格以便进行编辑。
protected override void Edit(CurrencyManager source, int rowNum, System.Drawing.Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible)
{
this.m_comboBox.Parent = this.DataGridTableStyle.DataGrid;
this.m_comboBox.Bounds = bounds;
this.m_comboBox.Size = new System.Drawing.Size(this.Width, this.comboBox.Height);
this.m_comboBox.SelectedValue = base.GetColumnValueAtRow(source, rowNum).ToString();
this.m_comboBox.Visible = (!readOnly) && cellIsVisible;
this.m_comboBox.BringToFront();
this.m_comboBox.Focus();
}



//如果编辑过程成功提交,则为 true;否则为 false。
protected override bool Commit(CurrencyManager dataSource, int rowNum)
{
if (this.m_edit == true)
{
this.m_edit = false;
//保存值
this.SetColumnValueAtRow(dataSource, rowNum, this.m_comboBox.SelectedValue);
}
return true;
}


//获取指定 CurrencyManager 中指定行内的值。
protected override object GetColumnValueAtRow(CurrencyManager source, int rowNum)
{
//return base.GetColumnValueAtRow (source, rowNum);
return this.m_comboBox.GetDisplayText(base.GetColumnValueAtRow(source, rowNum));
}


//用来自指定 CurrencyManager 的值设置指定行中的值。
protected override void SetColumnValueAtRow(CurrencyManager source, int rowNum, object value)
{
try
{
base.SetColumnValueAtRow(source, rowNum, value.ToString());
return;
}
catch
{ }
//下面是另外一种方法,对于使用GUID全局唯一标识符有效
try
{
base.SetColumnValueAtRow(source, rowNum, new Guid(value.ToString()));
return;
}
catch
{ }
}


protected override int GetMinimumHeight()
{
return this.m_comboBox.PreferredHeight + 2;
}


//在派生类中被重写时,将获取自动调整列的大小所用的高度。
protected override int GetPreferredHeight(System.Drawing.Graphics g, object value)
{
return FontHeight + 2;
}

//在派生类中被重写时,将获取指定值的宽度和高度。
//在用户定位到使用 DataGridColumnStyle 的 DataGridTableStyle 时将使用该宽度和高度。
protected override System.Drawing.Size GetPreferredSize(System.Drawing.Graphics g, object value)
{
//return new System.Drawing.Size ();
int widths = 0;
SizeF strF = new SizeF(0, 0);
foreach (string str in this.m_comboBox.GetDisplayText())
{
strF = g.MeasureString(str, base.DataGridTableStyle.DataGrid.Font);
if (strF.Width > widths)
{
widths = (int)Math.Ceiling(strF.Width);///////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
return new System.Drawing.Size(widths + 25, this.m_comboBox.PreferredHeight + 2);
}


//绘制具有指定 Graphics、Rectangle、CurrencyManager 和行号的 DataGridColumnStyle。
protected override void Paint(System.Drawing.Graphics g, System.Drawing.Rectangle bounds, CurrencyManager source, int rowNum)
{
//绘制具有指定 Graphics、Rectangle、CurrencyManager、行号和对齐方式的 DataGridColumnStyle。
Paint(g, bounds, source, rowNum, false);
}


//绘制具有指定 Graphics、Rectangle、CurrencyManager、行号、背景色、前景色和对齐方式的 DataGridColumnStyle。
protected override void Paint(System.Drawing.Graphics g, System.Drawing.Rectangle bounds, CurrencyManager source, int rowNum, bool alignToRight)
{
//string text = this.GetColumnValueAtRow(source,rowNum).ToString();
string text = GetColumnValueAtRow(source, rowNum).ToString();
Brush backBrush = new SolidBrush(base.DataGridTableStyle.BackColor);
Brush foreBrush = new SolidBrush(base.DataGridTableStyle.ForeColor);
Rectangle rect = bounds;
StringFormat format = new StringFormat();

// 单元格被选中
if (base.DataGridTableStyle.DataGrid.IsSelected(rowNum) == true)
{
backBrush = new SolidBrush(base.DataGridTableStyle.SelectionBackColor);
foreBrush = new SolidBrush(base.DataGridTableStyle.SelectionForeColor);
}
if (alignToRight == true)
{
format.FormatFlags = StringFormatFlags.DirectionRightToLeft;
}

switch (this.Alignment)
{
case HorizontalAlignment.Left:
format.Alignment = StringAlignment.Near;
break;
case HorizontalAlignment.Right:
format.Alignment = StringAlignment.Far;
break;
case HorizontalAlignment.Center:
format.Alignment = StringAlignment.Center;
break;
}
// Paint.
format.FormatFlags = StringFormatFlags.NoWrap;
g.FillRectangle(backBrush, rect);
rect.Offset(0, 0);
rect.Height = 0;
g.DrawString(text, this.DataGridTableStyle.DataGrid.Font, foreBrush, rect, format);
format.Dispose();
}


}
}
别样苍茫 2010-08-29
  • 打赏
  • 举报
回复
没做过 友情帮顶
wuyq11 2010-08-29
  • 打赏
  • 举报
回复
首先一定要知道 Scrapy爬虫框架 对新手非常的不友好,或者从某些视频网站上跟着视频学或者说从培训机构里学几天技能掌握的,主要原因有以下两个方面。框架模块内容太多,虽然只是实现了一个简单的爬虫工作,但是实际上完成一个页面的抓取在框架里最少要走8个以上的步骤,但是这些步骤都是基本都是依靠配置文件完成的,没有丰富的爬虫经验,这些模块很多都不知道是做什么的,也不知道怎么配置。基于框架内进行数据抓取仅限于那些通用的网站抓取,你就简单理解为一个完成重复工作的机器人就行了。但是如果是那种反爬比较厉害的网站那就是另外一种情况了,完全是爬虫工程师和网站开发者的一个博弈了,所以这种情况不适合任何一种爬虫框架。对于那些想在工作中摸鱼的Python工程师来说就一定要使用爬虫框架,你会发现省不少力气而且效率真心非常高,不过一切都是在对框架熟练掌握的基础上、和对业务的深刻理解来说来说。但凡说 Scrapy 无用的基本上没有认真阅读过 Scrapy 的源码,对于 Scrapy框架 中的众多功能在搭建爬虫系统的时候能想到用几个?而且是基本是没有做过大型的爬虫系统项目的。咱们倒着推这个事,你就知道为什么要用Scrapy框架了。我之前的单位是国家互联网的新闻中心,做的项目中的有一项是复现863课题舆情监控系统中的内容,使用的方法是 Scrapy爬虫框架 结合 Django Web 搭建的数据采集系统,抓取的目标对象包括新闻、博客、论坛等等,其中每天目标检测网站成千上万,如果不用框架去做这种数据采集得累死。1.抓取的数据存哪里?单纯Scrapy爬虫脚本写好了执行抓取任务时数据保存到哪里?ES、Mongodb、MySQL?如果做过开发的人都知道存 Mongodb 中,那些说把数据存到 MySQL 的人基本上99%是从培训机构里出来的,因为大多数培训机构爬虫是不讲 Mongodb 的。通过业务计算逻辑把数据传输到生产 ES 环境中。2.几千、几万个爬虫脚本应该怎么管理?很多刚入行的人都觉得爬虫脚本难写,其实并不是这样。最难的是如何管理密密麻麻数不清的爬虫脚本,这里介绍Python如何处理这个事情。管理方式无非集中,Web管理环境、GUI管理环境、手动管理环境,不管是哪一种都需要一定的开发基础和管理思路。比较省事的用别人写好的Scrapy管理框架,比如Gerapy爬虫管理框架。如同这样web直接一键管理爬虫脚本,更多内容看上面的文章,这里就不重复了。3.Scrapy如何面对反爬的?跟大多数单线抓取某个网站解决其反爬一样,只要把反爬的逻辑设置好,比如最简单的更换代理IP,更换header,解析JS生成cookie访问等等,都可以在框架中设置配置文件。4.如何提高代码编写效率以及数据爬取效率?一定要阅读源码,一定要阅读源码,一定要阅读源码你才能知道这个框架里到底有什么内容可以用。5.基于Python各种框架开发的爬虫定制化项目。

62,039

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧