4

実行時にDataGridViewセルにラベルを挿入する方法はありますか?たとえば、各セルの上隅に小さな赤い数字が必要だったとしましょう。新しいDataGridViewColumnタイプを作成する必要がありますか、それともDataGridViewにデータを入力するときにそこにラベルを追加するだけですか?

編集私は現在、Neoliskの提案に従ってセルペインティングを使用してこれを実行しようとしていますが、実際にラベルを表示する方法がわかりません。次のコードがあります。ここで、セルTagを設定する前に、ラベルテキストをセルとして追加しValueます。

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

編集2-解決策上記のように機能させることができなかったため、DataGridViewColumnとCellをサブクラス化し、PaintそこでイベントをオーバーライドしてTag、neoliskの提案に従って、ラベルではなくDrawStringを使用して保存されているテキストを追加しました。

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"; 
4

3 に答える 3

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
        }
于 2012-12-24T13:28:26.687 に答える
1

これは、 WinformsDataGridViewCellでコントロールをホストする方法に関するMSDNチュートリアルです。

ラベルの手順に従ってください

于 2012-12-24T12:58:27.020 に答える
1

カスタムペイントを行う場合は、コントロールの代わりにGraphics.DrawStringを使用する必要があります。LabelあなたeはタイプDataGridViewCellPaintingEventArgsなので、Graphicsプロパティがあります。これはPaintEventArgsの使用例です、あなたのものは似ているはずです。

于 2012-12-26T17:51:59.590 に答える