Reputation: 550
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
Upvotes: 1
Views: 5551
Reputation: 33183
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.
Upvotes: 1
Reputation: 9030
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
.
Upvotes: 0