哈哈,謹以此文記住自己的一點笨方法
背景:winform,c#,Datagridview,checkbox 實作自由多選 保證DataGridview的MultiSelect屬性值為true
代碼中加入如下内容:
全局變量:
private int startrow = -1;
以下是Datagridview的兩個事件:
private void dataGridView1_KeyUp(object sender, KeyEventArgs e)
{
if (this.dataGridView1.SelectedCells.Count > 0 && e.KeyData == Keys.ShiftKey)
{
int endrow = this.dataGridView1.CurrentRow.Index;
if (startrow <= endrow)
{
//正序選時
for (int x = startrow; x <= endrow; x++)
{
this.dataGridView1.Rows[x].Cells["我的checkbox列"].Value = 1;
}
}
else
{
//倒序選時
for (int x = endrow; x <= startrow; x++)
{
this.dataGridView1.Rows[x].Cells["我的checkbox列"].Value = 1;
}
}
}
}
private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && !(Control.ModifierKeys == Keys.Shift ))
{
if (this.dataGridView1.Focused && this.dataGridView1.CurrentCell.OwningColumn.DataPropertyName == "checkbox" && Convert.ToBoolean(this.dataGridView1.CurrentCell.EditedFormattedValue) == false)
{
startrow = this.dataGridView1.CurrentRow.Index;
}
}
}
ok啦,希望有幫助!!!