我想将datagridview的选定rpws发送到datatable 我应该说我的datagridview列之一是复选框 这是我想要的图像:
答案 0 :(得分:1)
也许试试这个?
public DataTable GetDataTableFromDataGridView(DataGridView dataGridView)
{
DataTable dataTable = new DataTable();
foreach (DataGridViewColumn column in dataGridView.Columns)
{
//// I assume you need all your columns.
dataTable.Columns.Add(column.Name, column.CellType);
}
foreach (DataGridViewRow row in dataGridView.Rows)
{
//// If the value of the column with the checkbox is true at this row, we add it
if (row.Cells["checkbox column name"].Value == true)
{
object[] values = new object[dataGridView.Columns.Count];
for (int i = 0; i < row.Cells.Count; i++)
{
values[i] = row.Cells[i].Value;
}
dataTable.Rows.Add(values);
}
}
return dataTable;
}
答案 1 :(得分:0)
假设您知道如何发送DataGridViewRow
个对象的集合,以下是您应该只检查那些被检查的对象:
var rows = yourDataGridView.Rows.Cast<DataGridViewRow>().Where(row => (bool)
(row.Cells["name of checkbox column!"] as DataGridViewCheckBoxCell).Value);
如果您的DataGridView
是数据绑定的,当然您应该直接对数据进行操作,而不是在DataGridViewCells
周围进行杂乱和转换。
与没有LINQ的上述相同:
var rows = new List<DataGridViewRow>();
foreach(DataGridViewRow row in yourDataGridView.Rows)
if((bool)(row.Cells["name of checkbox column!"] as DataGridViewCheckBoxCell).Value)
rows.Add(row);