程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> 使用DataGridView控件常見問題解答

使用DataGridView控件常見問題解答

編輯:關於C#
 

1. 如何使指定的單元格不可編輯?

ReadOnly屬性決定了單元格中的數據是否可以編輯,可以設置單元格的ReadOnly 屬性,也可以設置DataGridViewRow.ReadOnly 或DataGridViewColumn.ReadOnly使得一行或一列所包含的單元格都是只讀的。 默認情況下,如果一行或一列是只讀的,那麼其包含的單元格也會使只讀的。

 

不過你仍可以操作一個只讀的單元格,比如選中它,將其設置為當前單元格,但用戶不能修改單元格的內容。注意,即使單元格通過ReadOnly屬性設置為只讀,仍然可以通過編程的方式修改它,另外ReadOnly也不會影響用戶是否可以刪除行。

2. 如何讓一個單元格不可用(disable)?

單元格可以設置為只讀而不可編輯,但DataGridView卻沒提供使單元格不可用的支持。一般意義上,不可用意味著用戶不能進行操作,通常會帶有外觀的暗示,如灰色。沒有一種簡單的方法來創建那種不可操作的單元格,但提供一個暗示性的外觀告訴用戶某單元格不可用還是可行的。內置的單元格類型沒有進行不可用設置的屬性,下面的例子擴展了DataGridViewButtonCell ,參照常見控件的Enabled屬性,為其添加了Enabled屬性,如果該屬性設置為false,那麼其外觀狀態將類似於普通按鈕的不可用狀態。

 

public class DataGridViewDisableButtonColumn : DataGridViewButtonColumn

{

public DataGridViewDisableButtonColumn()

{

this.CellTemplate = new DataGridViewDisableButtonCell();

}

}

 

public class DataGridViewDisableButtonCell : DataGridViewButtonCell

{

private bool enabledValue;

public bool Enabled

{

get {

return enabledValue;

}

set {

enabledValue = value;

}

}

 

// Override the Clone method so that the Enabled property is copied.

public override object Clone()

{

DataGridViewDisableButtonCell cell =

(DataGridViewDisableButtonCell)base.Clone();

cell.Enabled = this.Enabled;

return cell;

}

 

// By default, enable the button cell.

public DataGridViewDisableButtonCell()

{

this.enabledValue = 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)

{

// The button cell is disabled, so paint the border,

// background, and disabled button for the cell.

if (!this.enabledValue)

{

// Draw the cell background, if specified.

if ((paintParts & DataGridViewPaintParts.Background) ==

DataGridViewPaintParts.Background)

{

SolidBrush cellBackground =

new SolidBrush(cellStyle.BackColor);

graphics.FillRectangle(cellBackground, cellBounds);

cellBackground.Dispose();

}

 

// Draw the cell borders, if specified.

if ((paintParts & DataGridViewPaintParts.Border) ==

DataGridViewPaintParts.Border)

{

PaintBorder(graphics, clipBounds, cellBounds, cellStyle,

advancedBorderStyle);

}

 

// Calculate the area in which to draw the button.

Rectangle buttonArea = cellBounds;

Rectangle buttonAdjustment =

this.BorderWidths(advancedBorderStyle);

buttonArea.X += buttonAdjustment.X;

buttonArea.Y += buttonAdjustment.Y;

buttonArea.Height -= buttonAdjustment.Height;

buttonArea.Width -= buttonAdjustment.Width;

 

// Draw the disabled button.

ButtonRenderer.DrawButton(graphics, buttonArea,

PushButtonState.Disabled);

 

// Draw the disabled button text.

if (this.FormattedValue is String)

{

TextRenderer.DrawText(graphics,

(string)this.FormattedValue,

this.DataGridView.Font,

buttonArea, SystemColors.GrayText);

}

}

else

{

// The button cell is enabled, so let the base class

// handle the painting.

base.Paint(graphics, clipBounds, cellBounds, rowIndex,

elementState, value, formattedValue, errorText,

cellStyle, advancedBorderStyle, paintParts);

}

}

}

3. 如何避免用戶將焦點設置到指定的單元格?

默認情況下DataGridView的操作(navigation)模型在限制用戶將焦點置於指定的單元格方面沒有提供任何支持。你可以實現自己的操作邏輯,這需要重寫合適的鍵盤、導航、鼠標方法,如DataGridView.OnKeyDown, DataGridView.ProcessDataGridViewKey, DataGridView.SetCurrentCellAddressCore, DataGridView.SetSelectedCellCore, DataGridView.OnMouseDown。

4. 如何使所有單元格總是顯示控件(不論它是否處於編輯狀態)?

DataGridView 控件只支持在單元格處於編輯狀態時顯示真實的控件(如TextBox)。DataGridView 沒有被設計為顯示多控件或為每行重復顯示控件。DataGridView 在單元格不被編輯時為其繪制對應控件的外觀,該外觀可能是你想要的。例如,DataGridViewButtonCell 類型的單元格,不管它是否處於編輯狀態,總是表現為一個按鈕。

5. Why does the cell text show up with “square” characters where they should be new lines(TODO,未能實現該效果)?

By default, text in a DataGridViewTextBoxCell does not wrap. This can be controlled via the WrapMode property on the cell style (e.g. DataGridView.DefaultCellStyle.WrapMode). Because text doesn’t wrap, new line characters in the text do not apply and so they are displayed as a “non-printable” character. This is similar to setting a TextBox’s Text property to the same text when the TextBox’s MultiLine property is false.

6. 如何在單元格內同時顯示圖標和文本?

DataGridView控件沒有對在同一單元格內同時顯示圖標和文本提供支持。但通過實現自定義的繪制事件,如CellPaint 事件,你可以輕松實現這個效果。

 

下面這段代碼擴展了DataGridViewTextBoxColumn 和DataGridViewTextBoxCell類,將一個圖片顯示在文本旁邊。這個示例使用了DataGridViewCellStyle.Padding 屬性來調整文本的位置,重寫了Paint 方法來繪制圖片。該示例可以得到簡化,方法是處理CellPainting 事件,在這裡實現類似的功能。

 

public class TextAndImageColumn:DataGridViewTextBoxColumn

{

private Image imageValue;

private Size imageSize;

 

public TextAndImageColumn()

{

this.CellTemplate = new TextAndImageCell();

}

 

public override object Clone()

{

TextAndImageColumn c = base.Clone() as TextAndImageColumn;

c.imageValue = this.imageValue;

c.imageSize = this.imageSize;

return c;

}

 

public Image Image

{

get { return this.imageValue; }

set

{

if (this.Image != value) {

this.imageValue = value;

this.imageSize = value.Size;

 

if (this.InheritedStyle != null) {

Padding inheritedPadding = this.InheritedStyle.Padding;

this.DefaultCellStyle.Padding = new Padding(imageSize.Width,
inheritedPadding.Top, inheritedPadding.Right,
inheritedPadding.Bottom);

}

}

}

}

private TextAndImageCell TextAndImageCellTemplate

{

get { return this.CellTemplate as TextAndImageCell; }

}

internal Size ImageSize

{

get { return imageSize; }

}

}

 

public class TextAndImageCell : DataGridViewTextBoxCell

{

private Image imageValue;

private Size imageSize;

 

public override object Clone()

{

TextAndImageCell c = base.Clone() as TextAndImageCell;

c.imageValue= this.imageValue;

c.imageSize = this.imageSize;

return c;

}

 

public Image Image

{

get {

if (this.OwningColumn == null ||
this.OwningTextAndImageColumn == null) {

 

return imageValue;

}

else if (this.imageValue != null) {

return this.imageValue;

}

else {

return this.OwningTextAndImageColumn.Image;

}

}

set {

if (this.imageValue != value) {

this.imageValue = value;

this.imageSize = value.Size;

 

Padding inheritedPadding = this.InheritedStyle.Padding;

this.Style.Padding = new Padding(imageSize.Width,
inheritedPadding.Top, inheritedPadding.Right,
inheritedPadding.Bottom);

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved