こんにちはプログラマ、 チェックボックスとラベルを同じセルに追加する必要があります。これを行う方法はわかります。私はdatagridviewcolumnをcheckboxにすることができますが、チェックボックスだけを表示し、ラベルを表示する場所は表示しません。誰もこの悪夢から私を目覚めさせることができますか? ありがとうございます。DataGridviewの同じセルにチェックボックスとラベルを追加するには?
答えて
これは、デフォルトのチェックボックスセルでは可能ではないと思います。
ここでは、チェックボックス列をサブクラス化し、ラベルを描画するためのカスタムペイントを実行する実装を示します。ラベルテキストのバインディングを提供する(現在、ラベルテキストを直接設定する必要があります)など、さまざまな方法でこれを拡張することができます。私のコードは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";
}
}
}
お世話になりました。私の時間を節約します。 –
質問したいのは、DataGridViewCheckBoxのcheckStateを変更してイベントを作成する方法です。 –
@PrakashKunwarこのためにcelldirtystatechangedイベントを使用することができます(これ以外の方法もありますが、これがベストとなることがよくあります)。この質問を見てくださいhttp://stackoverflow.com/questions/6468263/datagridviewcheckboxcolumn-how-to-update -bound-datasource-on-property-i-6469559#6469559 –
- 1. C#では、IntellisenseをDataGridViewセルに追加する方法は?
- 2. アンドロイドの同じリストビューにチェックボックスとラジオボタンの両方を追加するコード?
- 3. ウィンドウフォームのDataGridViewにチェックボックス列を追加する手助け
- 4. DataGridViewのセルを編集するときにDateTimepickerを追加する方法
- 5. チェックボックスのラベルから値を追加
- 6. UITableViewセルでラベルとイメージビューの数を追加する方法
- 7. Erlangを同じリストに追加する
- 8. ラベルにマーキーを追加するには
- 9. ラベルにデータを追加するには
- 10. slickgridにチェックボックスを追加するには?
- 11. datagridview内の特定のセルに行の注釈を追加する
- 12. ruby fastercsv、ファイル内のすべてのセルに同じテキストを追加して追加します
- 13. ビューを回転すると、動的に追加されたチェックボックスのテキストが同じになる
- 14. 同じ行にチェックボックスとテキストを強制するには?
- 15. MVCの親と同じフォームに子アイテムを追加する
- 16. DataGridViewヘッダーにComboboxを追加する
- 17. DataGridViewにSubTableを追加する
- 18. DataGridView列にデータを追加する
- 19. C#DataGridViewに行を追加する
- 20. コンボボックスをdatagridviewに追加するC#Winforms
- 21. 別のフォームにあるdatagridviewに行を追加するには
- 22. Python:同じリストにリストを追加
- 23. 同じ表のセルで選択した後にラベルを追加するにはどうすればよいですか?
- 24. JUNGに同じラベル(しかし異なるエンドポイント)を持つ2つのエッジを追加する方法は?
- 25. ラベルに選択された値を追加するためのチェックボックス付きグリッドビュー
- 26. DataGridviewセルから値を追加します。
- 27. DataGridViewのCheckboxColumnヘッダーのチェックボックスは、行と同期しますか?
- 28. yuiのタブラベルにチェックボックスを追加する
- 29. 別のテーブルビューにセルを追加する
- 30. UITextFieldにラベルを追加
あなたはテンプレート列を使用することができます。両方のコントロールを入れてください。 –
私はwinformsでこれが必要です。それはそこで利用可能ですか? –