所以我正在制作一个带有管理操作的聊天服务器,所以要做到这一点,我有一个DataGridView,当新客户端连接时,它会在客户端信息(用户名,IP地址,在线)上放置一个新行。我想要的是能够点击一行并弹出一个管理窗口。我现在拥有的是:
private void Grid_Click(object sender, MouseEventArgs e)
{
DataGridView.HitTestInfo hit = Grid.HitTest(e.X, e.Y);
if (hit.Type == DataGridViewHitTestType.Cell)
{
Grid.CurrentCell = BranchGrid[hit.ColumnIndex, hit.RowIndex];
ContextMenu.Show(Grid, e.X, e.Y);
}
}
然后我会在我的其他方法中得到特定的单元格内容:
private void Button_Click(object sender, EventArgs e)
{
var item = Grid.CurrentCell.Value; //Gives me the value of the current cell selected
}
虽然这有效,但如果您不点击已连接客户端的用户名,它将会中断。所以我想要做的就是点击任意一行并获取该行的用户名,这样我就可以打开一个单独的窗口,而不仅仅是一个上下文窗口。
答案 0 :(得分:1)
设置DataGridView.SelectionMode=FullRowSelect
您无需点击“用户名”单元格。
双击任何单元格后,您可以从当前行的单元格中获取值:
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
//get value from cell "Username" in current row
string Username = dataGridView1.CurrentRow.Cells["Username"].Value.ToString();
//here code to open dialog window etc.
}
“用户名”是列名,而不是标题文本。
您可以使用隐藏标签的其他表单 - 属性Visible1=False
并将Username
值设为label.Text
。
通过这种方式,您可以将值传递给另一种形式。