开发者

C# DataGridView CellClick Returning Previous Index on Click

I have the following code:

private void dgv_Checks_CellClick(object sender, DataGridViewCellEventArgs e)
{
  DoThis();

  if (e.RowIndex < 0)
  return;

  if (dgv_Checks.CurrentCell.OwningColumn.Name == "CopyBalance")
    dgv_Checks.Rows[e.RowIndex].Cells["CheckAmountColumn"].Value = dgv_Checks.Rows[e.RowIndex].Cells["AccountBalanceColumn"].Value;


  // ****** THIS IS WHERE I AM HAVING ISSUES ******
  if (e.RowIndex > -1 && dgv_Checks.Columns[e.ColumnIndex].Name == "SearchColumn")
  {
    var acctno = dgv_Checks.Rows[e.RowIndex].Cells["AccountNumberColumn"].Value.ToString().Trim();
    if (acctno.Length == 7)
    {
      var acctname = GetAccountName(acctno);
      if (acctname.Trim().Length > 0)
      {
        dgv_Checks.Rows[e.RowIndex].Cells["NameColumn"].Value = acctname;
        dgv_Checks.Rows[e.RowIndex].Cells["Accou开发者_JS百科ntBalanceColumn"].Value = GetAccountBalance(acctno);
      }
      else
      {
        AccountSearchScreen(dgv_Checks.CurrentRow);
      }
    }
    else
    {
      AccountSearchScreen(dgv_Checks.CurrentRow);
    }

  }
    dgv_Checks.Rows[e.RowIndex].Cells["CheckAmountColumn"].Value = dgv_Checks.Rows[e.RowIndex].Cells["AccountBalanceColumn"].Value;

}

When it reaches this line:

if (e.RowIndex > -1 && dgv_Checks.Columns[e.ColumnIndex].Name == "SearchColumn")

I am getting back the previous cell's index so I am always 1 click behind. Am I not using the right event? I just want to capture a button click and only that one in the "SearchColumn".

Thanks as usual.


If I recall correctly, it has nothing to do with the handler code, and something to do with other page events, master pages, validation, etc. Something is hijacking your request somewhere in the .net framework that causes it to get delayed, but I can't remember where...

Take your gridview and datasource, and put them in a separate file with no master pages or other confounding factors and see if the problem goes away.

If it is now working normally, either add in components from your original page until you can replicate the problem again, or go back to your original page and step through everything it does. This has happened to me at least twice, and all I can remember is that it's something that seems completely unrelated.

Not only have I provided a fairly useless answer, but I've demonstrated once again why I should really keep a (b)log of this crap...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜