using System;
using System.Windows.Forms;
namespace WindowsFormsApplication8
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private int getCheckRows(){
int result = 0;
for (int rowCounter = this.dataGridView1.Rows.Count -1; rowCounter >=0; rowCounter--)
{
if (Convert.ToBoolean(this.dataGridView1.Rows[rowCounter].Cells[1].Value) == true)
{
result++;
}
}
return result;
}
private void Form1_Load(object sender, EventArgs e)
{
this.dataGridView1.Rows.Add("Siavash", true);
this.dataGridView1.Rows.Add("Soroush", false);
this.dataGridView1.Rows.Add("Amin", false);
this.dataGridView1.Rows.Add("siavash", true);
this.dataGridView1.Rows.Add("Siavash", true);
this.dataGridView1.Rows.Add("Soroush", false);
this.dataGridView1.Rows.Add("Amin", false);
this.dataGridView1.Rows.Add("siavash", true);
this.label1.Text = getCheckRows().ToString();
}
private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
int count = this.getCheckRows();
if (count > 4 && Convert.ToBoolean(this.dataGridView1.CurrentCell.Value) == false)
{
e.Cancel = true;
}
}
private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (dataGridView1.IsCurrentCellDirty)
{
this.dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
int countRowChecked = getCheckRows();
this.label1.Text = countRowChecked.ToString();
}
}
}