ghost1349
ghost1349

Reputation: 83

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.

Upvotes: 0

Views: 73

Answers (1)

c4pricorn
c4pricorn

Reputation: 3481

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.

Upvotes: 1

Related Questions