我有一个datagridview,显示客户详细信息的列表,例如客户ID,姓名等等。当我点击特定客户行然后点击按钮时,我希望该客户的客户ID存储在一个字符串,然后用于使用MySQL命令检索客户的菜单选项。例子select * from menu where customerid =(从datagridview获取的字符串)
答案 0 :(得分:1)
试试这个
private void button1_Click(object sender, EventArgs e)
{
if (dataGridView1.SelectedCells.Count > 0)
{
int selectedrowindex = dataGridView1.SelectedCells[0].RowIndex;
DataGridViewRow selectedRow = datagridview1.Rows[selectedrowindex];
string CustumerID = Convert.ToString(selectedRow.Cells["column name"].Value);
}
}
或者
private void button1_Click(object sender, EventArgs e)
{
int SelectedRowndex = dataGridView1.CurrentCell.RowIndex;
string CustumerID= dataGridView1.Rows[SelectedRowndex].Cells["column name"].Value.ToString();
}