1

Here is my code:

class SelectionTableEntry
{
    public CheckBox cbIsChecked { get; set; }

    public Table Table { get; set; }

    public string TableName { get; set; }

    public Button btnEditFilter { get; set; }

    public SelectionTableEntry(Table table, bool IsChecked)
    {
        this.Table = table;
        this.TableName = table.Name;
        this.cbIsChecked = new CheckBox();
        this.cbIsChecked.Checked = IsChecked;
        this.btnEditFilter = new Button();
        this.btnEditFilter.Text = "Open";
    }
}

List<SelectionTableEntry> myList = new List<SelectionTableEntry>();

// after filling the list with items
myDataGridView.DataSource = myList;

Now I wanted to use a List with the type of SelectionTableEntry as a DataSource for my DataGridView. The problem is, that the CheckBox and the Button are not displayed, so the field is empty.

How can I solve the problem? Thanks in advance.

Regards, Chris

4

2 に答える 2

1

The DataGridView has out of the box column types for checkboxes and for buttons, the DataGridViewCheckBoxColumn and the DataGridViewButtonColumn.

You will automatically get a check box column for each boolean property on your DataSource object if you have AutoGenerateColumns set to true.

So your class would look like:

class SelectionTableEntry
{
    public bool cbIsChecked { get; set; }    
    public Table Table { get; set; }    
    public string TableName { get; set; }    
    public string btnEditFilter { get; set; }

    public SelectionTableEntry(Table table, bool IsChecked)
    {
        this.Table = table;
        this.TableName = table.Name;
        this.cbIsChecked = IsChecked;
    }
}

You cannot auto generate button columns, you need to add them yourself like so:

// Add a button column. 
DataGridViewButtonColumn buttonColumn = 
    new DataGridViewButtonColumn();
buttonColumn.HeaderText = "";
buttonColumn.Name = "Open";
buttonColumn.Text = "Open";
buttonColumn.UseColumnTextForButtonValue = true;

dataGridView1.Columns.Add(buttonColumn);

You need to do a little bit more to react to the button clicks, but that is all explained in the MSDN article.

于 2012-05-11T15:31:08.140 に答える
0

Here's a simple tutorial on How to: Host Controls in Windows Forms DataGridView Cells. It's a little dated, but I believe provides an excellent starting point when working with the DataGridView. It may appear to be a little intimidating though - you will be required to implement your own DataGridViewColumn and DataGridViewCell.

于 2012-05-11T15:04:45.977 に答える