Reputation:
Good ol' Ellie here with a Visual Studio 2010 question.
I've got a column in a Data Grid View that's just a check box, and I would like it to appear checked unless the user specifically un-checks it. The only things I've found are how to make it checked if it's just a stand alone checkbox.
Thanks in advance for help!
Ellie
Upvotes: 1
Views: 7739
Reputation: 91
I had the same problem right now.
My solution is, to use an event on my dataGridView.
You simply get the current row and replace the null value of your checkbox-column with true or false.
private void myDataGridView_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
{
// Get current row.
DataGridViewRow obj = myDataGridView.CurrentRow;
// Get the cell with the checkbox.
DataGridViewCheckBoxCell oCell = obj.Cells[theIndexOfTheCheckboxColumn] as DataGridViewCheckBoxCell;
// Check the value for null.
if (oCell.Value.ToString().Equals(string.Empty))
oCell.Value = true;
}
Upvotes: 0
Reputation: 368
If you are binding the DataGridView to a collection, then you can set the default value of the boolean property to true on the object, add the object to the BindingList collection, and set the collection to the DataGridView's data source.
For example, the collection to bind to the DataGridView would contain the properties needed (each property representing a column), including a boolean property to represent the checkbox column. Here is an example of what the class would look like:
public class Product : INotifyPropertyChanged
{
private bool _selected;
private string _product;
public event PropertyChangedEventHandler PropertyChanged;
public Product(string product)
{
_selected = true;
_product = product;
}
public bool Selected
{
get { return _selected; }
set
{
_selected = value;
this.NotifyPropertyChanged("Selected");
}
}
public string ProductName
{
get { return _product; }
set
{
_product = value;
this.NotifyPropertyChanged("Product");
}
}
private void NotifyPropertyChanged(string name)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
Within the form that contains the DataGridView, you can add your items to a BindingList and bind that collection to the DataGridView:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
InitGrid();
}
private void InitGrid()
{
dgProducts.AutoGenerateColumns = true;
BindingList<Product> products = new BindingList<Product>();
products.Add(new Product("Eggs"));
products.Add(new Product("Milk"));
products.Add(new Product("Bread"));
products.Add(new Product("Butter"));
dgProducts.DataSource = products;
}
}
This is just a quick and dirty example, but shows how you can set default values to an object, add that object to a BindingList, then add it to the DataGridView.
To add items after the list has been bound you can always access the DataSource collection and add to it (the sample code below assumes a button is added to the form and wired to the click event shown below, as well as a textbox named, newItemName):
private void addItemButton_Click(object sender, EventArgs e)
{
BindingList<Product> products = dgProducts.DataSource as BindingList<Product>;
products.Add(new Product(newItemName.Text));
}
Upvotes: 0
Reputation:
Loop through each row and check its respective box so that it appears checked (by default).
Should be something like this:
foreach (DataGridViewRow row in dataGridView.Rows)
{
row.Cells[CheckBoxColumn.Name].Value = true;
}
Upvotes: 2