0

我想将选定的数据网格视图的 rpws 发送到数据表我应该说我的数据网格视图的列之一是复选框,这是我想要的图像:

http://www.uploadmb.com/dw.php?id=1378997861 “怎么样??”

4

2 回答 2

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;
}
于 2013-09-12T18:50:55.340 回答
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);
于 2013-09-12T15:06:11.300 回答