![](/img/trans.png)
[英]C# DataGridView get values from another column if cells is selected
[英]Get First Column Values of Selected Rows of DataGridView in C#
我正在使用以下代码来通过单击按钮获取所选数据网格视图结果的ID(第一)列
`DataGridViewSelectedCellCollection DGV = this.dgvSearch.SelectedCells;
for (int i = 0; i <= DGV.Count - 1; i++)
{
string ID = Convert.ToString(dgvSearch.CurrentRow.Cells[0].Value);
MessageBox.Show(ID);
}`
我在消息框中获取ID,但与列次数相同,我只希望每个选择行一次。
使用DataGridViewSelectedRowCollection
而不是DataGridViewSelectedCellCollection
并遍历选定的行数。 在循环内只是给你你给的一样。 用this.dgvSearch.SelectedCells
替换this.dgvSearch.SelectedRows
。
更新:
DataGridViewSelectedRowCollection DGV =this.dgvSearch.SelectedRows;
foreach (DataGridViewRow row in DGV)
{
DataRow myRow = (row.DataBoundItem as DataRowView).Row;
string ID = Convert.ToString(myRow.Cells[0].Value);
MessageBox.Show(ID);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.