57

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?

Alex Terreaux
  • 1,881
  • 5
  • 23
  • 39

7 Answers7

113

You need to set datagridview's SelectionMode to FullRowMode.

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

fdermishin
  • 3,519
  • 3
  • 24
  • 45
urlreader
  • 6,319
  • 7
  • 57
  • 91
9

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;
}
Rick H.
  • 165
  • 1
  • 10
8

In the DataGridView properties, Set

  • MultiSelect -> True
  • SelectionMode -> FullRowSelect

Shark01
  • 121
  • 2
  • 5
1

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);
}
Tom McDonough
  • 1,176
  • 15
  • 18
0
//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

0

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];
        }
    }
0

Just to give another possible answer because none of these seemed to work for my case or required extra event callbacks that aren't needed. Put this in your button click.

foreach (DataGridViewTextBoxCell item in NAMEOFDATAGRIDVIEW.SelectedCells)
        {
            item.OwningRow.Selected = true;
        }
user2455808
  • 95
  • 1
  • 1
  • 9