DataGridView中的动态按钮列 [英] Dynamic button column in DataGridView

查看:30
本文介绍了DataGridView中的动态按钮列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎很简单,但对我来说却已成为极大的挫败感.我正在创建一个DataGridView并将其DataSource设置为包含两个属性的对象列表...一个int和一个按钮.第一列工作正常,但第二列为空白.经过SO搜索后,我意识到我的问题可能是我的按钮列需要设置为DataGridViewButtonColumn,只有当我尝试如此设置它时,它才会创建一个新的第三按钮列.

This seems simple but it's becoming a source of great frustration for me. I'm creating a DataGridView and setting its DataSource to a list of an object which contains two properties... an int and a button. The first column works fine but the second is blank. After an SO search I realized my problem may be that my button column needed to be set as a DataGridViewButtonColumn, only when I try and set it as such it creates a new, third, button column.

如何将自动生成的列与DataGridViewButtonColumn关联?这是我的代码的简化版本.

How do I associate my auto-generated column as a DataGridViewButtonColumn? Here is a simplified version of my code.

List<EditGrid> items = new List<EditGrid>();

foreach (int number in numbers)
{
    EditGrid eg = new EditGrid();

    eg.number = number;
    eg.edit = new Button() { Text = "Edit", Name = "Edit" };

    items.Add(eg);
}

DataGridView d = new DataGridView
{
    Dock = DockStyle.Fill,
    AutoGenerateColumns = true,
    DataSource = items
};

Controls.Add(d);

推荐答案

我认为您必须使用一些自定义的 CellPainting ,而不要设置 Text > DataGridViewButtonColumn 到任何值(默认情况下为空字符串).像这样:

I think you have to use some custom CellPainting and don't set the Text of DataGridViewButtonColumn to anything (this is by default an empty string). Like this:

private void dataGridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
        if (e.ColumnIndex > -1&&e.RowIndex > -1&&dataGridView.Columns[e.ColumnIndex] is DataGridViewButtonColumn)
        {                
            if (e.Value == null) return;
            e.Handled = true;
            e.PaintBackground(e.CellBounds, true);
            e.PaintContent(e.CellBounds);
            //prepare format for drawing string yourself.
            StringFormat sf = new StringFormat() { LineAlignment = StringAlignment.Center, Alignment = StringAlignment.Center };
            e.Graphics.DrawString(((Button)e.Value).Text, dataGridView.Font, Brushes.Black, e.CellBounds, sf);                
        }
}

在您的情况下,将 dataGridView 替换为 d ,看起来您是使用代码创建DataGridView的,如果这样,则必须注册 CellPainting 像这样自己处理事件:

In your case replace dataGridView with d, looks like that you create your DataGridView using code, if so, you have to register the CellPainting event handler yourself like this:

d.CellPainting += dataGridView_CellPainting;

更新

要使您的DataGridView首先具有一个DataGridViewButtonColumn(在设计时不添加),您必须在设置DataGridView的DataSource之前添加以下代码:

UPDATE

to make your DataGridView have a DataGridViewButtonColumn first (without adding at design time), you have to add this code before setting your DataGridView's DataSource:

DataGridViewButtonColumn col = new DataGridViewButtonColumn();
col.HeaderText = "Your header";
col.Name = "button";
col.DataPropertyName = "Your DataSource Data member";//This is very important to match the corresponding Property or DataMember name of your DataSource.
col.FlatStyle = FlatStyle.Popup;//I suggest this because it looks more elegant.
d.Columns.Add(col);         
//----
d.DataSource = ...   

我发现为Button列设置 DisplayIndex 无效,相反,您可能想在 d.DataSource =之后设置其 DisplayIndex .... ,并且有效:

I found that setting the DisplayIndex for your Button column doesn't work, instead, you may want to set its DisplayIndex after the d.DataSource=..., and it works:

d.DataSource = ...   ;
d.Columns["button"].DisplayIndex = ...;

这篇关于DataGridView中的动态按钮列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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