首页 > 学院 > 开发设计 > 正文

[Winform] DataGridView(FAQ)

2019-11-17 02:19:58
字体:
来源:转载
供稿:网友

[Winform] DataGridView(FAQ)

Q1. 如何使单元格不可编辑?

A:设置ReadOnly属性,可以设置的对象包括DataGridViewRow(行)、DataGridViewColumn(列)、DataGridViewCell(单元格)以及自身DataGridView对象均可设置ReadOnly属性来限制单元格的编辑状态。

扩展:需要注意的是,当DataGridView通过DataSource绑定数据自动生成行列时,如果直接在Form的构造函数初始化界面InitializeComponent后直接设置ReadOnly属性,会造成一些意想不到的效果……

  1 public MainForm()  2 {  3     InitializeComponent()  4   5     application.DoEvents()  6     dataGridView.DataSource = Person.GetPersons()  7     dataGridView[0, 0].ReadOnly = true  8     dataGridView.Rows[2].ReadOnly = true  9     dataGridView.Columns[1].ReadOnly = true 10 } 

此时对DataGridViewCell、DataGridViewRow的ReadOnly设置无效,而对DataGridViewColumn的ReadOnly设置有效。

另外,ReadOnly属性只是限制用户在界面上对单元格内容编辑的限制,并不影响在编程代码中对该单元格的编辑以及删除行列操作。

当然,你也可以在CellBeginEdit事件中对单元格进行判断,然后直接结束编辑即可,如下:

  1 dataGridView.CellBeginEdit += (sender, e) =>  2 {  3     if (e.RowIndex == 0 && e.ColumnIndex == 0)  4     {  5         e.Cancel = true  6         // 或者  7         // dataGridView.EndEdit();  8     }  9 }

Q2. 如何禁用单元格(Disable)?

A:DataGridView不支持设置单元格的不可用状态,所以采用折中办法,使该“禁用”单元格的选中状态和其背景颜色相同,给人暗示性的外观提示该单元格“禁用”。

有一种便捷的方式,就是将该“禁用”单元格的选中颜色设置成非选中颜色,即如果未选中前是白底黑字,则将该“禁用”单元格的选中状态也改成白底黑字即可,对单元格、行和列均适用,举例如下:

  1 dataGridView[2, 2].Style.SelectionBackColor = Color.White  2 dataGridView[2, 2].Style.SelectionForeColor = Color.Black  3   4 dataGridView.Rows[1].DefaultCellStyle.SelectionBackColor = Color.White  5 dataGridView.Rows[1].DefaultCellStyle.SelectionForeColor = Color.Black  6   7 dataGridView.Columns[0].DefaultCellStyle.SelectionBackColor = Color.White  8 dataGridView.Columns[0].DefaultCellStyle.SelectionForeColor = Color.Black

需要注意的是,同Q1中一样,在InitializeComponent方法后面直接操作,其中对单元格的设置无效,对行、列的设置有效!!

但是这种方法对文本内容的单元有效,对于DataGridViewButtonColumn、DataGridViewCheckBoxColumn、DataGridViewComboBoxColumn等其他特殊列就没效果了,毕竟对于特殊列,禁用单元格就是要禁用其中的特殊控件,这时候就需要重写其中的单元格模版了,以DataGridViewButtonColumn为例,代码如下:

public class DataGridViewButtonColumnExt : DataGridViewButtonColum{    public DataGridViewButtonColumnExt()    {        this.CellTemplate = new DataGridViewButtonCellExt()    }}public class DataGridViewButtonCellExt : DataGridViewButtonCell{    PRivate bool _Enabled;// 设置该单元格是否可用    /// <summary>    /// 单元格是否可用    /// </summary>    public bool Enabled    {        get        {            return _Enabled        }        set        {            _Enabled = value        }    }    public override object Clone()    {        DataGridViewButtonCellExt cell =(DataGridViewButtonCellExt)base.Clone()        cell.Enabled = this.Enabled        return cell    }    public DataGridViewButtonCellExt()    {        this._Enabled = true    }    protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds,        int rowIndex, DataGridViewElementStates elementState, object value,        object formattedValue, string errorText, DataGridViewCellStyle cellStyle,        DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)    {        if (!this._Enabled)        {            // 绘制背景            if ((paintParts & DataGridViewPaintParts.Background) == DataGridViewPaintParts.Background)            {                SolidBrush cellBackground = new SolidBrush(cellStyle.BackColor)                graphics.FillRectangle(cellBackground, cellBounds)                cellBackground.Dispose()            }            // 绘制边框            if ((paintParts & DataGridViewPaintParts.Border) == DataGridViewPaintParts.Border)            {                PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle)            }            Rectangle buttonArea = cellBound            Rectangle buttonAdjustment = this.BorderWidths(advancedBorderStyle)            buttonArea.X += buttonAdjustment.X            buttonArea.Y += buttonAdjustment.Y            buttonArea.Height -= buttonAdjustment.Height            buttonArea.Width -= buttonAdjustment.Width                        // 绘制按钮控件            ButtonRenderer.DrawButton(graphics, buttonArea, PushButtonState.Disabled)            // 绘制文本内容            if (this.FormattedValue is String)            {                TextRenderer.DrawText(graphics, (string)this.FormattedValue,                    this.DataGridView.Font, buttonArea, SystemColors.GrayText)            }        }        else        {            base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue,                errorText, cellStyle, advancedBorderStyle, paintParts)        }    }}
View Code

下面是CheckBox列的重写例子,因为复选框有两种禁用效果,一种选中时禁用,一种是未选中时禁用,所以加了一个IsChecked属性:

public class DataGridViewCheckBoxColumnExt : DataGridViewCheckBoxColum{public DataGridViewCheckBoxColumnExt(){this.CellTemplate = new DataGridViewCheckBoxCellExt()}}public class DataGridViewCheckBoxCellExt : DataGridViewCheckBoxCell{private bool _Enableprivate bool _IsChecked/// <summary>/// 是否选中/// </summary>public bool IsChecked{get{return _IsChecked}set{_IsChecked = value}}/// <summary>/// 是否可用/// </summary>public bool Enable{get{return _Enable}set{_Enable = value}}public DataGridViewCheckBoxCellExt(){_Enable = true_IsChecked = false}public override object Clone(){DataGridViewCheckBoxCellExt dgvcce = (DataGridViewCheckBoxCellExt)base.Clone()dgvcce.Enable = this.Enabledgvcce.IsChecked = this.IsCheckedreturn dgvcce}protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds,int rowIndex, DataGridViewElementStates elementState, object value, object formattedValue,string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle,DataGridViewPaintParts paintParts){if (!_Enable){// 绘制背景if ((paintParts & DataGridViewPaintParts.Background) == DataGridViewPaintParts.Background){SolidBrush cellBackground = new SolidBrush(cellStyle.BackColor)graphics.FillRectangle(cellBackground, cellBounds)cellBackground.Dispose()}// 绘制边框if ((paintParts & DataGridViewPaintParts.Border) == DataGridViewPaintParts.Border){PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle)}Point pos = cellBounds.Locatio// 调整位置居中pos.X = pos.X + cellBounds.Width / 2 - 7pos.Y = pos.Y + cellBounds.Height / 2 - 7// 绘制按钮控件CheckBoxRenderer.DrawCheckBox(graphics, pos,IsChecked ? CheckBoxState.CheckedDisabled : CheckBoxState.UncheckedDisabled)}else{base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value,formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts)}}}
View Code

Q3. 如何在单元格内同时显示图片和文字?

A:参考网上查到的资料,将文本列(DataGridViewTextBoxColum)中的文本向右偏移,然后在前面置入图片;

public class TextAndImageColumn : DataGridViewTextBoxColum{    private Image m_ImageValue    private Size m_ImageSize    public
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表