1. 全选/取消:
Code
bool _SelectAll = false;
private void btnSelectAll_Click(object sender, EventArgs e)
{
if (this.dataGridView.Rows.Count > 0)
{
if (!this._SelectAll)
{
for (int i = 0; i < this.dataGridView.Rows.Count; i++)
{
this.dataGridView.Rows[i].Selected = true;
this._SelectAll = true;
}
}
else
{
for (int i = 0; i < this.dataGridView.Rows.Count; i++)
{
this.dataGridView.Rows[i].Selected = false;
this._SelectAll = false;
}
}
}
}
bool _SelectAll = false;
private void btnSelectAll_Click(object sender, EventArgs e)
{
if (this.dataGridView.Rows.Count > 0)
{
if (!this._SelectAll)
{
for (int i = 0; i < this.dataGridView.Rows.Count; i++)
{
this.dataGridView.Rows[i].Selected = true;
this._SelectAll = true;
}
}
else
{
for (int i = 0; i < this.dataGridView.Rows.Count; i++)
{
this.dataGridView.Rows[i].Selected = false;
this._SelectAll = false;
}
}
}
}
2. 反选
Code
private void btnSelect_Click(object sender, EventArgs e)
{
if (this.dataGridView.Rows.Count > 0)
{
for (int i = 0; i < this.dataGridView.Rows.Count; i++)
{
if (this.dataGridView.Rows[i].Selected)
{
this.dataGridView.Rows[i].Selected = false;
}
else
{
this.dataGridView.Rows[i].Selected = true;
}
}
}
}
private void btnSelect_Click(object sender, EventArgs e)
{
if (this.dataGridView.Rows.Count > 0)
{
for (int i = 0; i < this.dataGridView.Rows.Count; i++)
{
if (this.dataGridView.Rows[i].Selected)
{
this.dataGridView.Rows[i].Selected = false;
}
else
{
this.dataGridView.Rows[i].Selected = true;
}
}
}
}