C# 当用户单击该行的单元格时,如何选择完整的 dataGridView 行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13672693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:23:36  来源:igfitidea点击:

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

c#.netwinformsdatagridview

提问by Alex Terreaux

I have a dataGridViewand 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 currentRowIndexlike this

我有一个dataGridView,我需要当用户单击任何单元格时,也会选择包含该单元格的整行。(它已取消多选)我尝试过currentRowIndex这样的

 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?

你知道我可以这样做的方法吗?

采纳答案by urlreader

You need to set datagridview's SelectionModeto FullRowMode.

您需要将 datagridview 设置SelectionModeFullRowMode.

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

注意:在带有 .NET 4.5 的 Visual Studio 2013 中,该属性称为FullRowSelect,请参阅https://msdn.microsoft.com/en-us/library/3c89df86(v=vs.110).aspx

回答by Rick H.

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

如果您希望以编程方式选择行,您将使用 datagridview 的单元格单击事件:显示在 VB.net 和 C#

VB.Net

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#

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

回答by Tom McDonough

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);
}

回答by Shark01

In the DataGridView properties, Set

在 DataGridView 属性中,设置

  • MultiSelect -> True
  • SelectionMode -> FullRowSelect
  • 多选 -> 真
  • 选择模式 -> 全行选择

回答by Parag555

//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

看看我的 DataGridView

回答by Engr Inzamam ul Haq

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