RT。。我继承GridView控件,自己添加了些新功能。现在想在 protected override void OnRowDataBound(GridViewRowEventArgs e)
方法里给表头添加样式
e.Row.RowType == DataControlRowType.DataRow//判断行类型为表头
我现在添加样式不知道如何定位到该行,我想把该行的背景色置为黑,并且不透明

解决方案 »

  1.   

    扩展GridView控件
      

  2.   

    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();
            }
        }
    }
      

  3.   

        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);
        }
      

  4.   

    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);
            }