【问题标题】:Datagridview SelectionChanged event based on row selection基于行选择的 Datagridview SelectionChanged 事件
【发布时间】:2014-01-14 21:55:47
【问题描述】:

我在 TabPage 上有一个 DataGridView。当用户单击一行时,会出现第二个 DGV。每一行都与它自己的填充数据的 DGV 相关联。我想要的是当用户从一行转到另一行时,DataGridView 也会发生变化。到目前为止,我已经尝试了 SelectionChanged 事件,但我不希望 DGV 在用户单击同一行中的单独单元格时重新加载。任何帮助将不胜感激。

void dgv1_CellClick(object sender, DataGridViewCellEventArgs e)           
{    
    int rowIndex = dgv1.Rows[e.RowIndex].Index;

    if (dgv1 == null)
        return;
    if (dgv1.Rows[e.RowIndex].Cells[rowIndex].Selected == true);
    {
        dgv2.Size = new Size(dgv2.Width + 800, dgv2.Height);
        dgv2.Location = new Point(0, 500);   

        tp.Controls.Add(dgv2);

        Console.WriteLine("Row clicked");
    }
}

-塔蒂亚娜

【问题讨论】:

  • 能否创建一个代表当前选中行的属性,然后检查新选中的行是否相同?
  • 当前选择的行实际上有一个属性,即 dgv.CurrentRow 问题是如何检查新选择的行。其次,CurrentRow 是 DataGridViewRow 类型,它不能转换为 int 类型,因此在尝试将其与任何 int 进行比较时出现错误。
  • DataGridViewRow 有一个 Index 属性,所以它是 dgv.CurrentRow.Index。 MSDN:msdn.microsoft.com/en-us/library/…
  • 在 rowenter 和 rowleave 中使用 e.rowIndex。当您向上或向下导航到一行时它们会触发(不需要您进入该单元格/行的编辑模式)gotcha 如果您使用 dgv.currentRow 它会给您上一行它可能会混淆你。请务必使用来自事件的 e.rowIndex。

标签: c# .net visual-studio datagridview datagridviewrow


【解决方案1】:
    int curRow = -1;

    private void dgv1_SelectionChanged(object sender, EventArgs e)
    {
        if (dgv1.CurrentRow.Index != curRow)
        {
            curRow = dgvPatientDetail.CurrentRow.Index;        
        }

【讨论】:

  • 或设置 dgv1.SelectMode = FullRowSelect;当点击在同一行内时,不应触发 SelectionChanged
【解决方案2】:

我知道这篇文章很旧,但其他人正在阅读: 设置..

DataGridView1.FullRowSelect = true

应该可以解决这个问题。

【讨论】:

  • 你的意思是dgv1.SelectionMode = FullRowSelect,上面已经提到过?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-08
  • 2013-06-16
  • 2011-07-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多