【问题标题】:How do I select a complete dataGridView Row when the user clicks a cell of that row?当用户单击该行的单元格时,如何选择完整的 dataGridView 行?
【发布时间】:2012-11-20 07:05:42
【问题描述】:

我有一个dataGridView,我需要当用户单击任何单元格时,包含该单元格的整行也会被选中。 (它已禁用多选) 我试着像这样得到currentRowIndex

 int Index = dataGridView1.CurrentCell.RowIndex;

但是,我不确定如何使用索引来选择该行。 尝试了此方法以及其他六种方法均未成功:

dataGridView1.Select(Index);

你知道我可以用什么方法吗?

【问题讨论】:

    标签: c# .net winforms datagridview


    【解决方案1】:

    你需要将datagridview的SelectionMode设置为FullRowMode

    注意:在带有 .NET 4.5 的 Visual Studio 2013 中,该属性称为 FullRowSelect

    【讨论】:

    • 哈哈比我想象的要简单得多。谢谢。
    • 使用:dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
    【解决方案2】:

    如果您希望以编程方式选择行,您将使用 datagridview 的单元格单击事件:在 VB.net 和 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;
    }
    

    【讨论】:

    • 如何将事件与 datagridview 绑定?
    【解决方案3】:

    可以做这样的事情

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

    【讨论】:

      【解决方案4】:

      在 DataGridView 属性中, 设置

      • 多选 -> 真
      • SelectionMode -> FullRowSelect

      【讨论】:

        【解决方案5】:
        //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

        【讨论】:

          【解决方案6】:

          你可以这样做:也许它可以帮助你。

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2018-02-28
            • 1970-01-01
            • 1970-01-01
            • 2011-06-23
            • 2019-07-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多