C#之获取DataGridView所有选中的单元格
private void toolStripMenuItem3_Click(object sender, EventArgs e) { int selectedCellCount =dataGridView1.GetCellCount(DataGridViewElementStates.Selected); if (selectedCellCount > 0) { if (dataGridView1.AreAllCellsSelected(true)) { MessageBox.Show("All cells are selected", "Selected Cells"); } else { System.Text.StringBuilder sb = new System.Text.StringBuilder(); for (int i = 0; i < selectedCellCount; i++) { sb.Append("Row: "); sb.Append(dataGridView1.SelectedCells[i].RowIndex .ToString()); sb.Append(", Column: "); sb.Append(dataGridView1.SelectedCells[i].ColumnIndex .ToString()); sb.Append(Environment.NewLine); } sb.Append("Total: " + selectedCellCount.ToString()); MessageBox.Show(sb.ToString(), "Selected Cells"); } } }
![](http://r.qzone.qq.com/cgi-bin/cgi_get_user_pic?openid=00000000000000000000000005492D01&pic=1.jpg&key=14c00103d64a28b46e3a7211892ffbee)
探索代码的奥秘,享受技术的乐趣-_-!...