2012-12-23 32 views
4

是否有任何方法在運行時將標籤插入到DataGridView單元格 - 例如,我想在每個單元格的頂部角落中插入一個紅色小數字?我是否需要創建一個新的DataGridViewColumn類型,或者在填充DataGridView時只需添加一個Label?如何添加標籤到DataGridView單元格

編輯我現在在嘗試做這個使用Cell畫爲每Neolisk的建議,但我不確定如何真正得到要顯示的標籤。我有下面的代碼,在那裏我現在設置前添加標籤文本作爲細胞的TagValue

private void dgvMonthView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) 
{ 
    DataGridView dgv = this.dgvMonthView; 
    DataGridViewCell cell = dgv[e.ColumnIndex, e.RowIndex]; 

    Label label = new Label(); 
    label.Text = cell.Tag.ToString(); 
    label.Font = new Font("Arial", 5); 
    label.ForeColor = System.Drawing.Color.Red; 
} 

誰能解釋:我現在就可以「附加」 labelcell

EDIT 2 - 解決方案我不能完全得到它的工作上面的方式,讓已經結束了子類的DataGridViewColumn和細胞並重寫Paint事件有使用的DrawString添加任何文本存儲在Tag而不是一個標籤按neolisk的建議:

class DataGridViewLabelCell : DataGridViewTextBoxCell 
{ 
    protected override void Paint(Graphics graphics, 
            Rectangle clipBounds, 
            Rectangle cellBounds, 
            int rowIndex, 
            DataGridViewElementStates cellState, 
            object value, 
            object formattedValue, 
            string errorText, 
            DataGridViewCellStyle cellStyle, 
            DataGridViewAdvancedBorderStyle advancedBorderStyle, 
            DataGridViewPaintParts paintParts) 
    { 
     // Call the base class method to paint the default cell appearance. 
     base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, 
      value, formattedValue, errorText, cellStyle, 
      advancedBorderStyle, paintParts); 

     if (base.Tag != null) 
     { 
      string tag = base.Tag.ToString(); 
      Point point = new Point(base.ContentBounds.Location.X, base.ContentBounds.Location.Y); 
      graphics.DrawString(tag, new Font("Arial", 7.0F), new SolidBrush(Color.Red), cellBounds.X + cellBounds.Width - 15, cellBounds.Y); 
     } 
    } 
} 

public class DataGridViewLabelCellColumn : DataGridViewColumn 
{ 
    public DataGridViewLabelCellColumn() 
    { 
     this.CellTemplate = new DataGridViewLabelCell(); 
    } 
} 

實現爲:

DataGridViewLabelCellColumn col = new DataGridViewLabelCellColumn(); 
dgv.Columns.Add(col); 
col.HeaderText = "Header"; 
col.Name = "Name"; 
+1

您是否嘗試過自定義單元格繪畫?您應該可以完成任何類型的自定義繪圖,包括角落中的小標籤。 [見此](http://social.msdn.microsoft.com/Forums/en/winforms/thread/07071c24-d0c4-4952-8e53-fe0d2bf20641)。 – Neolisk

+0

謝謝,這聽起來像它可能是理想的,但仍然不知道如何實際添加標籤。我用示例更新了我的問題 – CrazyHorse

回答

1

如果你正在做的風俗畫,你應該使用Graphics.DrawString而不是Label控制。您的eDataGridViewCellPaintingEventArgs類型,因此它具有Graphics屬性。以下是使用PaintEventArgs的示例,您的應該與此類似。

+0

非常感謝您的幫助,我使用了DrawString方法,並使用解決方案更新了我的問題 – CrazyHorse

1

Here是如何舉辦的交流上的MSDN教程在Winforms DataGridViewCell中進行控制。

只要按照標籤的程序

+0

感謝您的建議,但是這似乎只涉及如何覆蓋編輯控件,但我需要始終顯示標籤。我更新了我的問題,並提供了更多細節。 – CrazyHorse

1

也許這可以解決你的問題

class LabelColumn : DataGridViewColumn 
{ 
    public LabelColumn() 
     : base(new LabelCell()) 
    { 
    } 
    public override DataGridViewCell CellTemplate 
    { 
     get 
     { 
      return base.CellTemplate; 
     } 
     set 
     { 
      if (value != null && 
      !value.GetType().IsAssignableFrom(typeof(LabelCell))) 
      { 
       throw new InvalidCastException("Must be a CalendarCell"); 
      } 
      base.CellTemplate = value; 
     } 
    } 
} 
public class LabelCell : DataGridViewTextBoxCell 
{ 
    public LabelCell() 
     : base() 
    { 
    } 
    public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle) 
    { 
     base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle); 
     LabelEditingControl lb = DataGridView.EditingControl as LabelEditingControl; 
     if (this.Value == null) 
     { 
      lb.Value = this.DefaultNewRowValue; 
     } 
     else 
     { 
      lb.Value = this.Value; 
     } 
    } 
    public override Type EditType 
    { 
     get 
     { 
      // Return the type of the editing control that CalendarCell uses. 
      return typeof(LabelEditingControl); 
     } 
    } 

    public override Type ValueType 
    { 
     get 
     { 
      // Return the type of the value that CalendarCell contains. 

      return typeof(string); 
     } 
    } 
    public override object DefaultNewRowValue 
    { 
     get 
     { 
      // Use the "". 
      return ""; 
     } 
    } 
    class LabelEditingControl : Label, IDataGridViewEditingControl 
    { 
     DataGridView dataGridView; 
     private bool valueChanged = false; 
     int rowIndex; 
     public Object Value { get; set; } 
     public LabelEditingControl() 
     { 
      this.Enabled = false; 
     } 
     public object EditingControlFormattedValue 
     { 
      get 
      { 
       return this.Value.ToString(); 
      } 
      set 
      { 
       if (value is String) 
       { 
        try 
        { 
         // This will throw an exception of the string is 
         // null, empty, or not in the format of a date. 
         this.Value = value; 
        } 
        catch 
        { 
         // In the case of an exception, just use the 
         // default value so we're not left with a null 
         // value. 
         this.Value = ""; 
        } 
       } 
      } 
     } 
     public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context) 
     { 
      return EditingControlFormattedValue; 
     } 
     public void ApplyCellStyleToEditingControl(
    DataGridViewCellStyle dataGridViewCellStyle) 
     { 
      this.Font = dataGridViewCellStyle.Font; 
      this.ForeColor = dataGridViewCellStyle.ForeColor; 
      this.BackColor = dataGridViewCellStyle.BackColor; 
     } 
     public int EditingControlRowIndex 
     { 
      get 
      { 
       return rowIndex; 
      } 
      set 
      { 
       rowIndex = value; 
      } 
     } 
     public bool EditingControlWantsInputKey(
    Keys key, bool dataGridViewWantsInputKey) 
     { 
      // Let the DateTimePicker handle the keys listed. 
      switch (key & Keys.KeyCode) 
      { 
       case Keys.Left: 
       case Keys.Up: 
       case Keys.Down: 
       case Keys.Right: 
       case Keys.Home: 
       case Keys.End: 
       case Keys.PageDown: 
       case Keys.PageUp: 
        return true; 
       default: 
        return !dataGridViewWantsInputKey; 
      } 
     } 

     // Implements the IDataGridViewEditingControl.PrepareEditingControlForEdit 
     // method. 
     public void PrepareEditingControlForEdit(bool selectAll) 
     { 
      // No preparation needs to be done. 
     } 
     public bool RepositionEditingControlOnValueChange 
     { 
      get 
      { 
       return false; 
      } 
     } 

     // Implements the IDataGridViewEditingControl 
     // .EditingControlDataGridView property. 
     public DataGridView EditingControlDataGridView 
     { 
      get 
      { 
       return dataGridView; 
      } 
      set 
      { 
       dataGridView = value; 
      } 
     } 

     // Implements the IDataGridViewEditingControl 
     // .EditingControlValueChanged property. 
     public bool EditingControlValueChanged 
     { 
      get 
      { 
       return valueChanged; 
      } 
      set 
      { 
       valueChanged = value; 
      } 
     } 

     // Implements the IDataGridViewEditingControl 
     // .EditingPanelCursor property. 
     public Cursor EditingPanelCursor 
     { 
      get 
      { 
       return base.Cursor; 
      } 
     } 

    } 
} 

的形式編寫代碼

LabelColumn clm = new LabelColumn(); 
     dataGridView1.Columns.Add(clm); 
     dataGridView1.Rows.Add(5); 
     foreach (DataGridViewRow row in this.dataGridView1.Rows) 
     { 
      row.Cells[0].Value ="hello"; //or do anything desired 
     } 
相關問題