1

What event shall I use in a DataGridView considering that the DataGridView is full of data and when I click a row of data automatically I would like to retrieve all the data. I've tried using the event CellContentClick but it is only activated when I select a column data instead of a row

private void dtSearch_CellContentClick(object sender, DataGridViewCellEventArgs e)
{

}
Picrofo Software
  • 5,475
  • 3
  • 23
  • 37
user974015
  • 113
  • 2
  • 6
  • 14

3 Answers3

1

I have used the following to good effect. I handle the MouseDown event for the DataGridView and set the full row to be highlighted so that it is obvious it has been selected (unless of course you already have your full rows being selected).

    private void dtSearch_MouseDown(object sender, MouseEventArgs e)
    {
        // Get the cell that was clicked from the location of the mouse pointer

        DataGridView.HitTestInfo htiSelectedCell = dtSearch.HitTest(e.X, e.Y);

        if (e.Button == MouseButtons.Left)
        {
            // Make sure that a cell was clicked, and not the column or row headers
            // or the empty area outside the cells. If it is a cell,
            // then select the entire row, set the current cell (to move the arrow to
            // the current row)

            //if (htiSelectedCell.Type == DataGridViewHitTestType.Cell)
            if (htiSelectedCell.Type == DataGridViewHitTestType.RowHeader)
            {
                // do stuff here
            }
        }
    }
Sid Holland
  • 2,871
  • 3
  • 27
  • 43
  • I haven't tested this, but I would think you could test for `htiSelectedCell.Type == DataGridViewHitTestType.RowHeader` instead of, or as well as, the `DataGridViewHitTestType.Cell`. – Sid Holland Nov 20 '12 at 00:51
  • an error. it says. "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index" – user974015 Nov 20 '12 at 01:18
  • Since it would appear that you are only interested in selection by clicking a row header, I've altered my answer to reflect that. However I'd say that @Colin Pear's is probably the better one now that I've removed the code that highlights an entire row based on a cell being clicked. – Sid Holland Nov 20 '12 at 04:20
  • @SidHolland In my case The DataGridView should displays only one column i.e StudentName. But when I select the record on the DataGridView I should get all the details to display using other output controls. like name, RollNo, Class, Address etc.. but in the database table contain all the details. Help me how can I get this ? – Sanjeev4evr Apr 09 '14 at 14:11
  • @Sanjeev4evr I would suggest you start a new question post with more details. I'm not able to easily understand what the problem you are having is with only a comment. Also, more people will be available to help you out. – Sid Holland Apr 11 '14 at 04:47
  • @SidHolland I posted this as a question at http://stackoverflow.com/questions/22979149/datagridview-selectionchanged-event-firing-multiple-times – Sanjeev4evr Apr 11 '14 at 04:57
1

Hows about the RowHeaderMouseClick.

Colin Pear
  • 3,028
  • 1
  • 29
  • 33
1

Try using the CellClick event, and loop through the columns retrieving the row values you want:

        private void Form1_Load(object sender, EventArgs e)
    {
        this.dataGridView1.CellClick += new DataGridViewCellEventHandler(dataGridView1_CellClick);
    }

    public void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        List<object> values = new List<object>();

        int cols = this.dataGridView1.Columns.Count;

        for (int col = 0; col < cols; col++)
        {               

            values.Add(this.dataGridView1[col, e.RowIndex].Value);
        }
    }
Guilherme
  • 721
  • 6
  • 13