首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >添加用户控件

添加用户控件
EN

Stack Overflow用户
提问于 2013-04-01 02:36:54
回答 1查看 311关注 0票数 3

我搜索将用户控件添加到datagridview单元格的技巧,发现了下面的代码。我只是复制粘贴代码,但是当我尝试添加CustomColumn时,我得到了错误。

通过这种方式,我尝试将CustomColumn列添加到网格。

代码语言:javascript
复制
private void button1_Click(object sender, EventArgs e)
{
    CustomColumn cc=new CustomColumn();
    dataGridView1.Columns.Add(cc);
    dataGridView1.Rows.Add("");
}

public class CustomColumn : DataGridViewColumn {
    public CustomColumn() : base(new CustomeCell()) { }
    public override DataGridViewCell CellTemplate
    {
        get
        {
            return base.CellTemplate;
        }
        set
        {
            // Ensure that the cell used for the template is a CalendarCell.
            if (value != null &&
                !value.GetType().IsAssignableFrom(typeof(CustomeCell)))
            {
                throw new InvalidCastException("It should be a custom Cell");
            }
            base.CellTemplate = value;
        }
    }        
}
public class CustomeCell : DataGridViewCell
{
    public CustomeCell() : base() { }
    public override Type ValueType
    {
        get
        {
            return typeof(CustomUserControl);
        }
    }
    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)
    {
        CustomUserControl ctrl = (CustomUserControl)value;
        Bitmap img = new Bitmap(cellBounds.Width, cellBounds.Height);
        ctrl.DrawToBitmap(img, new Rectangle(0, 0, ctrl.Width, ctrl.Height));
        graphics.DrawImage(img, cellBounds.Location);
    }
    protected override void OnClick(DataGridViewCellEventArgs e)
    {
        List<InfoObject> objs = this.DataGridView.DataSource as List<InfoObject>;
        if (objs != null) 
        {
            if (e.RowIndex >= 0 && e.RowIndex < objs.Count) {
                CustomUserControl ctrl = objs[e.RowIndex].Ctrl;
                // Take any action - I will just change the color for now.
                ctrl.BackColor = Color.Red;
                ctrl.Refresh();
                this.DataGridView.InvalidateCell(e.ColumnIndex, e.RowIndex);
            }    
        }
    }
} 

请告诉我如何使用上面的代码来添加自定义列。我创建了一个用户控件,名称是CustomUserControl,但没有成功。谢谢

EN

回答 1

Stack Overflow用户

发布于 2013-04-01 15:11:25

发布了一个名为ColVis的DataTables新插件。ColVis将在表格旁边设置一个按钮,当激活该按钮时,将显示表中列的列表,并为最终用户提供显示或隐藏列的选项。

试试datatables.net/release-datatables/extras/ColVis/

下载(将解压缩的文件夹放到DataTables发行版的"extras“文件夹中):datatables.net/release/ColVis-1.0.1.zip

http://yellowpages.sulekha.com

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15733014

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档