How do I select a complete dataGridView Row when the user clicks a cell of that row?

C#.NetWinformsDatagridview

C# Problem Overview


I have a dataGridView and I need that when the user clicks on any cell the whole row that contains this cell is selected too. (it has multiselect disbaled) I tried getting the currentRowIndex like this

 int Index = dataGridView1.CurrentCell.RowIndex;

However, I am not sure how to use the index in order to select that row. Tried this and about other six ways with no success:

dataGridView1.Select(Index);

Do you know a way I can do this?

C# Solutions


Solution 1 - C#

You need to set datagridview's SelectionMode to FullRowMode.

Note: In Visual Studio 2013 with .NET 4.5 the property is called FullRowSelect.

Solution 2 - C#

If you want the row selected programatically, you would use the datagridview's cell click event: shown in VB.net and C#

VB.Net

Private Sub dgvGrid_CellClick(sender as System.Object, e as System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvGrid.CellClick
    If e.RowIndex < 0 Then
        Exit Sub
    End If

    intIndex = e.RowIndex
    dgvGrid.Rows(intIndex).Selected = True
Exit Sub

C#

private void dgvRptTables_CellClick(System.Object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
{
    if (e.RowIndex < 0) {
        return;
    }

    int index = e.RowIndex;
    dgvGrid.Rows[index].Selected = true;
}

Solution 3 - C#

In the DataGridView properties, Set

  • MultiSelect -> True
  • SelectionMode -> FullRowSelect

https://i.stack.imgur.com/6cAru.jpg">

Solution 4 - C#

Could do something like this

protected override void Render(HtmlTextWriter writer)
{
    foreach (GridViewRow row in Results.Rows)
    {
        if (row.RowType == DataControlRowType.DataRow)
        {
            row.Attributes["onmouseover"] = "this.style.cursor='pointer';";
            row.CssClass = "rowHover";
            row.ToolTip = "Click row to view person's history";
            row.Attributes.Add("onclick", this.ClientScript.GetPostBackClientHyperlink(this.Results,"Select$" & r.RowIndex , true));
        }
    }

    base.Render(writer);
}

Solution 5 - C#

//class to store ID (Pri. Key) value of selected row from DataGridView
public class Variables
{
   public static string StudentID;
}                                  

//This is the event call on cell click of the DataGridView
private void dataGridViewDisplay_CellClick(object sender, DataGridViewCellEventArgs e)
{
   Variables.StudentID =this.dataGridViewDisplay.CurrentRow.Cells[0].Value.ToString();
//textBoxName is my form field where I set the value of Name Column from the Selected row from my DataGridView 

   textBoxName.Text = this.dataGridViewDisplay.CurrentRow.Cells[1].Value.ToString();
            
   dateTimePickerDOB.Value = Convert.ToDateTime(this.dataGridViewDisplay.CurrentRow.Cells[2].Value.ToString());
}

Take a look at My DataGridView

Solution 6 - C#

You can Do this: May be it can help you.

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        if (e.RowIndex>0)
        {
            int rowindex = e.RowIndex;
            DataGridViewRow row= this.dataGridView1.Rows[rowindex];
        }
    }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlex TerreauxView Question on Stackoverflow
Solution 1 - C#urlreaderView Answer on Stackoverflow
Solution 2 - C#Rick H.View Answer on Stackoverflow
Solution 3 - C#Shark01View Answer on Stackoverflow
Solution 4 - C#Tom McDonoughView Answer on Stackoverflow
Solution 5 - C#Parag DevghareView Answer on Stackoverflow
Solution 6 - C#Engr Inzamam ul HaqView Answer on Stackoverflow