开发者

How can I get the value of all the selected cells in a DataGridView?

I have a DataGridView that has MultiSelect = true. After the user selects differe开发者_如何学Pythonnt cells from different rows how can I get the value of all the selected cells?


You can iterate over SelectedCells.

foreach (DataGridViewCell cell in dataGridView1.SelectedCells)
{
    MessageBox.Show(cell.Value.ToString());
}

You asked only for the value, but you probably also want to know the row and the column of the cell otherwise the value could be meaningless. You can access these also on the cell object.


foreach -

DataGrid.SelectedCells

More info on the SelectedCells Property can be found at http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.selectedcells.aspx

The SelectedCells collection is inefficient with large selections in DataGridView. There is a method you can use to get the count of the selected cells. iterate based on that and it'll be faster.

for (int i = 0; i < grid.GetCellCount(System.Windows.Forms.DataGridViewElementStates.Selected); i++)
{
    string val = grid.SelectedCells[i].Value;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜