C# 更新后重新填充网格时,单击将数据移动到下一个单元格

C# 更新后重新填充网格时,单击将数据移动到下一个单元格,c#,winforms,events,gridview,C#,Winforms,Events,Gridview,填充网格视图,然后在单元格上单击我填充单元格上的文本框单击事件 private void dgvCompany_CellClick(object sender, DataGridViewCellEventArgs e) { int i = dgvCompany.SelectedCells[0].RowIndex; ID = dgvCompany.Rows[i].Cells[0].Value.ToString();

填充网格视图,然后在单元格上单击我填充单元格上的文本框单击事件

   private void dgvCompany_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int i = dgvCompany.SelectedCells[0].RowIndex;
            ID = dgvCompany.Rows[i].Cells[0].Value.ToString();
            txtCompanyName.Text = dgvCompany.Rows[i].Cells[1].Value.ToString();
            txtContactName.Text = dgvCompany.Rows[i].Cells[2].Value.ToString();
            txtContactPersonPhone.Text = dgvCompany.Rows[i].Cells[3].Value.ToString();
            txtAddress.Text = dgvCompany.Rows[i].Cells[4].Value.ToString();
            txtCity.Text = dgvCompany.Rows[i].Cells[5].Value.ToString();
            txtPhone.Text = dgvCompany.Rows[i].Cells[6].Value.ToString();
            cbIsActive.Checked = Convert.ToBoolean(dgvCompany.Rows[i].Cells[7].Value);
        }
这里ID是填充文本框后的字符串变量,我编辑并保存更新,然后像前面的方法一样重新填充网格

public void FillCompanyInfo()
        {
            DataTable dtCompanyInfo = objFunctions.GetCompanyInfo();
            if(dtCompanyInfo.Rows.Count>0)
            {

                dgvCompany.DataSource = dtCompanyInfo;
                if (this.dgvCompany.Columns.Count == 8)
                {
                    DataGridViewCheckBoxColumn checkColumn = new DataGridViewCheckBoxColumn();
                    checkColumn.Name = "";
                    checkColumn.HeaderText = "Select";
                    checkColumn.Width = 50;
                    checkColumn.ReadOnly = false;
                    checkColumn.FillWeight = 10; //if the datagridview is resized (on form resize) the checkbox won't take up too much; value is relative to the other columns' fill values\\
                    dgvCompany.Columns.Add(checkColumn);
                }
            }
        }
这里我使用if条件counting column为8,因为每次它添加column Select,并在我单击grid和cell click event fire时再次填充grid之后,都会导致错误

在网上,

 ID = dgvCompany.Rows[i].Cells[0].Value.ToString();
找不到对象引用,因为它在单元格0上变为null,而在单元格单击更新之前它工作正常,当我使用断点时,我在单元格[1]上变为ID,而单元格[0]为null

我不理解这个错误

希望你的建议


谢谢

请确保在Page.Load之前调用
FillCompanyInfo()
。检查gridview在CellClick上是否有行。另外,当抛出错误时,
i
的值是多少?@NikitaSilverstruk这里是
winforms
,而不是
ASP.NET
。哦,对不起。。。链接后的所有内容仍然适用…@Nikita它显示的i值与更新索引记录之前的i值相同value@SaluSala,行数呢?