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

后端 未结 6 994
梦毁少年i
梦毁少年i 2020-12-14 14:01

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 t

相关标签:
6条回答
  • 2020-12-14 14:33
    //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 讨论(0)
  • 2020-12-14 14:35

    In the DataGridView properties, Set

    • MultiSelect -> True
    • SelectionMode -> FullRowSelect

    0 讨论(0)
  • 2020-12-14 14:35

    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 讨论(0)
  • 2020-12-14 14:43

    You need to set datagridview's SelectionMode to FullRowMode.

    Note: In Visual Studio 2013 with .NET 4.5 the property is called FullRowSelect, see https://msdn.microsoft.com/en-us/library/3c89df86(v=vs.110).aspx

    0 讨论(0)
  • 2020-12-14 14:44

    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;
    }
    
    0 讨论(0)
  • 2020-12-14 14:45

    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);
    }
    
    0 讨论(0)
提交回复
热议问题