DataGridView_CellClick事件args正在使用奇怪的列索引 [英] DataGridView_CellClick event args are using strange column indexes

查看:125
本文介绍了DataGridView_CellClick事件args正在使用奇怪的列索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的WinForms应用程序中有一个DataGridView,我使用数据绑定填充:

  myGridView.DataSource = myDataList; 

一旦列表被绑定,我在DataGridView的最后(右侧)添加一个DataGridViewButtonColumn 。我在DataBindingComplete事件中处理这个:

  private void myGridView_DataBindingComplete(object sender,DataGridViewBindingCompleteEventArgs e)
{
if(!myGridView.Columns.Contains(Remove))
{
DataGridViewButtonColumn removeColumn = new DataGridViewButtonColumn();
removeColumn.Name =Remove;
removeColumn.HeaderText =删除;
removeColumn.DataPropertyName =删除;
removeColumn.Text =删除;
removeColumn.UseColumnTextForButtonValue = true;
_removeColumnIndex = myGridView.Columns.Add(removeColumn);
}
}

最后,我的DataGridView有以下列:

 日期|数量|累积数量|被拒绝|累积拒绝|删除| 

根据文档 添加方法将返回添加列的索引。由于索引从0开始,并且从左到右依次删除列的索引应为5.使用调试器,我已经验证 _removeColumnIndex 实际上等于5



奇怪的行为进入我的 CellClick 事件:

  private void myGridView_CellClick(object sender,DataGridViewCellEventArgs e)
{
if(e.ColumnIndex == _removeColumnIndex)
{
/ /删除行
}
}

当我点击我的删除按钮,行没有被删除。使用调试器,我发现当我点击删除按钮 e.ColumnIndex 的值为0.因为它不等于5,删除不会发生。我在所有其他列使用了调试器;从左到右的顺序是:1,2,3,4,5,0。



为什么 myGridView.Columns。 Add()方法返回5的正确索引,但DataGridViewCellEventArgs将该列视为索引0,即使它位于网格列的右侧?

解决方案

列的索引已更改,因为 DataGridView 的DataSource在之前更新CellClick 事件被触发



我假定其他列是由 DataGridView 生成的(你有 DataGridView.AutoGenerateColumns = true



所以绑定完成后,列生成就添加了一个删除

DataSource 已重置生成的列已被删除并再次生成时,

,但您的手动添加列保留并获取索引= 0(第一列)



使用列名来检查 CellCl中的列ick 事件处理程序

  private void myGridView_CellClick(object sender,DataGridViewCellEventArgs e)
{
DataGridView dgv =(DataGridView)sender;
if(dgv.Columns(e.ColumnIndex).Name.Equals(Remove)== true)
{
//删除行
}
}


I have a DataGridView in my WinForms application that I populate using data binding:

myGridView.DataSource = myDataList;

Once the list has been bound, I add a DataGridViewButtonColumn at the end (right side) of the DataGridView. I handle this in the DataBindingComplete event:

private void myGridView_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
    if (!myGridView.Columns.Contains("Remove"))
    {
        DataGridViewButtonColumn removeColumn = new DataGridViewButtonColumn();
        removeColumn.Name = "Remove";
        removeColumn.HeaderText = "Remove";
        removeColumn.DataPropertyName = "Remove";
        removeColumn.Text = "Remove";
        removeColumn.UseColumnTextForButtonValue = true;
        _removeColumnIndex = myGridView.Columns.Add(removeColumn);
    }
}

In the end, my DataGridView has the following columns:

| Date | Quantity | Cumulative Quantity | Rejected | Cumulative Rejected | Remove |

According to the documentation the Add method will return the index of the added column. Since indexing starts at 0, and following from left to right the index of the remove column should be 5. Using the debugger, I have verified that _removeColumnIndex is in fact equal to 5.

The strange behavior comes inside my CellClick event:

private void myGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
   if(e.ColumnIndex == _removeColumnIndex)
   {
      // Remove row
   }
}

When I click my remove button, the row is not being removed. Using the debugger, I found out that when I click on the remove button e.ColumnIndex has a value of 0. Because it's not equal to 5, the remove doesn't happen. I have used the debugger on all other columns; The order from left to right is: 1, 2, 3, 4, 5, 0.

Why is it that the myGridView.Columns.Add() method returns the proper index of 5, but the DataGridViewCellEventArgs considers that column as index 0, even though it is at the right side of the grid columns?

解决方案

Column's index changed because you DataSource of the DataGridView was updated(reset) before CellClick event was fired

I assume other columns was generated by DataGridView (you have DataGridView.AutoGenerateColumns = true

So after binding was complete and columns generate you added a Remove column
When DataSource was resetted generated columns was removed and generated again,
but your manually added column remains and get index = 0(first column)

Use column name for checking a column inside CellClick event handler

private void myGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
    DataGridView dgv = (DataGridView)sender;   
    if(dgv.Columns(e.ColumnIndex).Name.Equals("Remove") == true)
    {
       // Remove row
    }
}    

这篇关于DataGridView_CellClick事件args正在使用奇怪的列索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆