国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

[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
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 平乡县| 松原市| 开远市| 民权县| 辉县市| 古交市| 桦甸市| 金塔县| 漳浦县| 德昌县| 桑植县| 黄平县| 仲巴县| 拉萨市| 松阳县| 田东县| 新津县| 黄石市| 玉环县| 万荣县| 碌曲县| 蒲江县| 台北县| 蒙阴县| 东方市| 长沙县| 轮台县| 邳州市| 五大连池市| 阳江市| 祁东县| 丰都县| 新丰县| 泌阳县| 九台市| 波密县| 五台县| 桦川县| 达拉特旗| 汽车| 鄱阳县|