darklup
darklup

Reputation: 63

Get value of data grid view first cell with context menu

I have a data grid view populated with values. What I want to do is to right click item in data grid view, get a context menu to pop up, there I should select a menu item to get a first cell of selected row.
I managed to make context menu and its item, so I'm good there. What I need to do is to somehow get the value of first row while selecting said item. I tried to use cell click method to get it and it works, but I have no clue how to implement it to menu item click.

string value = ((DataGridView)sender).Rows[e.RowIndex].Cells[0].Value.ToString();

Upvotes: 4

Views: 1027

Answers (1)

darklup
darklup

Reputation: 63

Okay so I managed to play around the problem and solve it like that: I got the value I needed with mouse down method and stored it in global variable like this:

private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
    {
        DataGridView.HitTestInfo hit = dataGridView1.HitTest(e.X, e.Y);
        if(hit.RowIndex >= 0)
        {
            finalValue = ((DataGridView)sender).Rows[hit.RowIndex].Cells[0].Value.ToString();
        }
    }

After I click the menu item i can manage the variable as I want. This is not the exact solution for the problem I described, but HEY , as long as it works right? Hope it will help someone.

Upvotes: 2

Related Questions