简体   繁体   中英

Select row and first cell

So I am making a chat server with administrative actions in it, so to do that I have a DataGridView that will put a new row with client info (Username, IP Address, Online) when a new client connects. What I want is to be able to click a row and have an admin window popup. What I have right now is this:

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);
    }
}

And then I would get the specific cell content in my other method like this:

private void Button_Click(object sender, EventArgs e)
{
    var item = Grid.CurrentCell.Value; //Gives me the value of the current cell selected
}

While this works, it will break if you don't click on the Username of the connected client. So what I want to be able to do is click on any row and get the username for that row so that I can open a separate window as opposed to just a context window.

Set DataGridView.SelectionMode=FullRowSelect

You not need to click on cell "Username".
You can get value from cell in current row after double click on any cell:

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.
    }

"Username" is column name, not header text.

You can use other form with hidden label - property Visible1=False
and set Username value to label.Text .
This way you can pass a value to another form.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM