2012-03-22 40 views
3

こんにちはプログラマ、 チェックボックスとラベルを同じセルに追加する必要があります。これを行う方法はわかります。私はdatagridviewcolumnをcheckboxにすることができますが、チェックボックスだけを表示し、ラベルを表示する場所は表示しません。誰もこの悪夢から私を目覚めさせることができますか? ありがとうございます。DataGridviewの同じセルにチェックボックスとラベルを追加するには?

+0

あなたはテンプレート列を使用することができます。両方のコントロールを入れてください。 –

+0

私はwinformsでこれが必要です。それはそこで利用可能ですか? –

答えて

4

これは、デフォルトのチェックボックスセルでは可能ではないと思います。

ここでは、チェックボックス列をサブクラス化し、ラベルを描画するためのカスタムペイントを実行する実装を示します。ラベルテキストのバインディングを提供する(現在、ラベルテキストを直接設定する必要があります)など、さまざまな方法でこれを拡張することができます。私のコードはthis forum postから大きく借りています。ここで

using System; 
using System.Windows.Forms; 
using System.Drawing; 

public class MyDGVCheckBoxColumn : DataGridViewCheckBoxColumn 
{ 
    private string label; 

    public string Label 
    { 
     get 
     { 
      return label; 
     } 
     set 
     { 
      label = value; 
     } 
    } 

    public override DataGridViewCell CellTemplate 
    { 
     get 
     { 
      return new MyDGVCheckBoxCell(); 
     } 
    } 
} 

public class MyDGVCheckBoxCell : DataGridViewCheckBoxCell 
{ 
    private string label; 

    public string Label 
    { 
     get 
     { 
      return label; 
     } 
     set 
     { 
      label = value; 
     } 

    } 

    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 base Paint implementation paints the check box 
     base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts); 

     // Get the check box bounds: they are the content bounds 
     Rectangle contentBounds = this.GetContentBounds(rowIndex); 

     // Compute the location where we want to paint the string. 
     Point stringLocation = new Point(); 

     // Compute the Y. 
     // NOTE: the current logic does not take into account padding. 
     stringLocation.Y = cellBounds.Y + 2; 


     // Compute the X. 
     // Content bounds are computed relative to the cell bounds 
     // - not relative to the DataGridView control. 
     stringLocation.X = cellBounds.X + contentBounds.Right + 2; 


     // Paint the string. 
     if (this.Label == null) 
     { 
      MyDGVCheckBoxColumn col = (MyDGVCheckBoxColumn)this.OwningColumn; 
      this.label = col.Label; 
     } 

     graphics.DrawString(
     this.Label, Control.DefaultFont, System.Drawing.Brushes.Red, stringLocation); 

    } 

} 

は、使用方法を示すコードです:

private void Form1_Load(object sender, EventArgs e) 
{ 
    MyDGVCheckBoxColumn col = new MyDGVCheckBoxColumn(); 
    col.Label = "hippo"; 
    col.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft; 
    this.dataGridView1.Columns.Add(col); 
    this.dataGridView1.RowCount = 5; 

    foreach (DataGridViewRow row in this.dataGridView1.Rows) 
    { 
     if (row.Index % 2 == 0) 
     { 
      ((MyDGVCheckBoxCell)row.Cells[0]).Label = "kitten"; 
     } 
    } 
} 
+0

お世話になりました。私の時間を節約します。 –

+0

質問したいのは、DataGridViewCheckBoxのcheckStateを変更してイベントを作成する方法です。 –

+1

@PrakashKunwarこのためにcelldirtystatechangedイベントを使用することができます(これ以外の方法もありますが、これがベストとなることがよくあります)。この質問を見てくださいhttp://stackoverflow.com/questions/6468263/datagridviewcheckboxcolumn-how-to-update -bound-datasource-on-property-i-6469559#6469559 –

関連する問題