如何将一个标签添加到一个DataGridView细胞 [英] How to add a Label to a DataGridView cell

查看:564
本文介绍了如何将一个标签添加到一个DataGridView细胞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在运行时插入一个标签DataGridView的细胞 - 例如说我在每个单元的右上角想稍微红润一些?我需要创建一个新的DataGridViewColumn类型或我可以只添加一个Label那里当我填充DataGridView的?



修改我现在试图做这个使用Cell画为每Neolisk的建议,但我不确定如何真正得到要显示的标签。我有下面的代码,在那里我现在加上标签文本的单元格的标签设置前的

 私人无效dgvMonthView_CellPainting(对象发件人,DataGridViewCellPaintingEventArgs E)
{
的DataGridView DGV = this.dgvMonthView;
细胞的DataGridViewCell DGV = [e.ColumnIndex,e.RowIndex]

标签标签=新的Label();
label.Text = cell.Tag.ToString();
label.Font =新的字体(宋体,5);
label.ForeColor = System.Drawing.Color.Red;
}



任何人都可以解释:我现在就可以附加标签细胞



编辑2 - ?解决方案我不能完全得到它的工作上面的方式,因此已经结束了子类的DataGridViewColumn和细胞并重写油漆事件有没有添加任何文本存储在标签使用的DrawString,而不是一个标签按neolisk的建议:

 类DataGridViewLabelCell: DataGridViewTextBoxCell 
{
保护覆盖无效油漆(图形显卡,
矩形clipBounds,
矩形cellBounds,
INT的rowIndex,
DataGridViewElementStates cellState,
对象的值,
对象FormattedValue的,
串ERRORTEXT,
的DataGridViewCellStyle cellStyle,
DataGridViewAdvancedBorderStyle advancedBorderStyle,
DataGridViewPaintParts paintParts)
{
//调用基类方法绘制的默认单元格的外观。
base.Paint(图形,clipBounds,cellBounds和rowIndex,cellState,
值,FormattedValue的,ERRORTEXT,cellStyle,
advancedBorderStyle,paintParts);

如果(base.Tag!= NULL)
{
字符串标记= base.Tag.ToString();
点积分=新的点(base.ContentBounds.Location.X,base.ContentBounds.Location.Y);
graphics.DrawString(标签,新的字体(宋体,7.0F),新SolidBrush(Color.Red),cellBounds.X + cellBounds.Width - 15,cellBounds.Y);
}
}
}

公共类DataGridViewLabelCellColumn:的DataGridViewColumn
{
公共DataGridViewLabelCellColumn()
{
this.CellTemplate =新DataGridViewLabelCell();
}
}



实现为:

  DataGridViewLabelCellColumn山坳=新DataGridViewLabelCellColumn(); 
dgv.Columns.Add(COL);
col.HeaderText =头;
col.Name =姓名;


解决方案

如果你正在做的风俗画,你应该使用 Graphics.DrawString 而不是标签控制。你的电子的类型为 DataGridViewCellPaintingEventArgs ,因此具有图形属性。下面是使用 PaintEventArgs的的,你应该是相似的例子。


Is there any way to insert a Label to DataGridView cell at runtime - say for example I wanted a little red number in the top corner of each cell? Do I need to create a new DataGridViewColumn type or can I just add a Label there when I'm populating the DataGridView?

EDIT I am now attempting to do this using Cell painting as per Neolisk's suggestion, but am unsure how to actually get the label to be displayed. I have the following code, where I now add the label text as the cell's Tag before setting its 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;
}

Can anyone explain how I can now "attach" label to cell?

EDIT 2 - Solution I couldn't quite get it to work the above way, so have ended up subclassing the DataGridViewColumn and Cell and overriding the Paint event there to add whatever text is stored in Tag using DrawString rather than a Label as per neolisk's suggestion:

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();
    }
}

Implemented as:

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

解决方案

If you are doing custom painting, you should be using Graphics.DrawString instead of Label control. Your e is of type DataGridViewCellPaintingEventArgs, so it has Graphics property. Here is an example of using PaintEventArgs, yours should be similar.

这篇关于如何将一个标签添加到一个DataGridView细胞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆