我最近开始使用Windows窗体应用程序。在一个数据网格视图DatagridView1
中,我正在显示一些来自数据表dt1
的数据。
DatagridView1.DataSource=dt1;
我在DatagridView1
中添加了一个名为selectRows
的列,类型为DataGridViewCheckBoxColumn
。
我想要将DatagridView1
中选中的行添加到另一个数据表中。
我有一个名为btn_Select
的按钮,在按钮单击时,我使用以下代码以字符串格式获取选定的行:
private void btn_Select_Click(object sender, EventArgs e)
{
string data = string.Empty;
foreach (DataGridViewRow row in DatagridView1.Rows)
{
if (row.Cells[0].Value != null &&
Convert.ToBoolean(row.Cells[0].Value) == true)
{
foreach (DataGridViewCell cell in row.Cells)
{
if (cell.OwningColumn.Index != 0)
{
data += (cell.Value + " "); // do some thing
}
}
data += "\n";
}
}
}
但是我想选择整行,而不仅仅是文本。