gpt4 book ai didi

c# - 如何以编程方式将选定的单元格数据移动到 DataGrid 中的数组

转载 作者:太空宇宙 更新时间:2023-11-03 13:45:38 26 4
gpt4 key购买 nike

这是将所有数据从数据网格移动到数组的代码。但我只想将选定的数据移动到该数组。可以通过鼠标进行选择。

public List<double[]> ExtractGridData(DataGridView grid)
{
int numCols = grid.Columns.Count;
List<double[]> list = new List<double[]>();
foreach (DataGridViewRow row in grid.Rows)
{
if (row.IsNewRow) // skip the new row
continue;
double[] cellsData = new double[numCols];
foreach (DataGridViewCell cell in row.Cells)
if (cell.Value != null)
cellsData[cell.ColumnIndex] = Convert.ToDouble(cell.Value);
list.Add(cellsData);
}

return list;
}

最佳答案

使用grid.SelectedRows而不是 grid.Rows

您还可以使用 grid.SelectedCell如果SelectionMode设置为 CellSelect 并且您需要单独的单元格值:

List<double> cellsData = new List<double>();
foreach (DataGridViewCell cell in grid.SelectedCells)
{
if( cell.Value != null)
cellsData.Add(Convert.ToDouble(cell.Value));
}

关于c# - 如何以编程方式将选定的单元格数据移动到 DataGrid 中的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15447559/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com