在winfrom 的DataGridView中添加一列,ColumnType属性设置为DataGridViewCheckBoxColumn。
假设设置Name属性为SelIndex
1、选择全部列
private void btnSelALl_Click(object sender, EventArgs e)
{
DataTable dt = (dataGridView1.DataSource as DataTable);
for (int i = 0; i < dt.Rows.Count; i++)
{
dt.Rows[i]["SelIndex"] = 1;
}
}
2、取得某一选择行的值
private void btnGetRows_Click(object sender, EventArgs e)
{
//刷新
if (dataGridView1.IsCurrentCellDirty)
{
dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
//取得选中的行
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (dataGridView1.Rows[i].Cells["SelIndex"].Value.ToString() == "1")
{
//相应的操作
MessageBox.Show( dataGridView1.Rows[i].Cells["Test"].Value.ToString());
}
}
}
其他:添加 一行(比如说合计行)
index = dataGridView1.Rows.Add();
DataGridViewRow newrow = dataGridView1.Rows[index];
newrow.Cells[0].Value = ...;
newrow.Cells[1].Value = ...;
newrow.Cells[2].Value = ...;