1

我在 asp.net 中的 Window 应用程序中工作。我有一个 GUI,用户可以在其中在文本框中输入产品名称和数量。在添加按钮上单击我在 Datagridview 中添加一个新行并在 datagridview 列中设置产品名称和数量的值。我没有在数据库中插入记录,我只是在 Datatable 中保存记录以及在 Datagridview 中添加记录。

问题是,当我从 datagridview 中选择最后一行并按下键盘上的删除按钮时,它会生成错误 索引超出范围。必须是非负数且小于集合的大小。参数名称:索引

static public DataTable gdt;
    private void btnAdd_Click(object sender, EventArgs e)
    {
        try
        {
            if (txtItemCode.Text.Trim() == "")
            {
                MessageBox.Show("Enter Item Code");
                txtItemCode.Focus();
                return;
            }
            if (txtQty.Text.Trim() == "")
            {
                MessageBox.Show("Enter Qty");
                txtQty.Focus();
                return;
            }
            if (Convert.ToInt32(txtQty.Text.Trim()) <= 0)
            {
                MessageBox.Show("Qty must be greater than 0");
                txtQty.Focus();
                return;
            }

            if (btnAdd.Text == "ADD")
            {

                DataRow[] dr = gdt.Select("Item_Code = '" + txtItemCode.Text.Trim() + "'");

                if (dr.Length > 0)
                {
                    MessageBox.Show("Item Code Already Exist.");
                    txtItemCode.Text = "";
                    txtItemCode.Focus();
                    return;
                }

                tblItemMasterBLL oItem = new tblItemMasterBLL();
                int ItemID = 0;
                DataTable dt = new DataTable();
                dt = oItem.getItemDetailByItemCode(txtItemCode.Text.Trim());
                if (dt.Rows.Count > 0)
                {
                    ItemID = Convert.ToInt32(dt.Rows[0]["Item_ID"]);


                    gdt.Rows.Add();

                    gdt.Rows[gdt.Rows.Count - 1]["Item_Code"] = txtItemCode.Text.Trim();
                    gdt.Rows[gdt.Rows.Count - 1]["Item_ID"] = ItemID;
                    gdt.Rows[gdt.Rows.Count - 1]["Qty"] = txtQty.Text.Trim();

                    gdt.Rows[gdt.Rows.Count - 1]["Article_Desc"] = Convert.ToString(dt.Rows[0]["Article_Desc"]);
                    gdt.Rows[gdt.Rows.Count - 1]["Color_Desc"] = Convert.ToString(dt.Rows[0]["Color_Desc"]);
                    gdt.Rows[gdt.Rows.Count - 1]["Size_Desc"] = Convert.ToString(dt.Rows[0]["Size_Desc"]);
                    gdt.Rows[gdt.Rows.Count - 1]["MRP"] = Convert.ToString(dt.Rows[0]["MRP"]);


                    dgv_Items.DataSource = null;
                    dgv_Items.DataSource = gdt;




                }
                else
                {
                    MessageBox.Show("Invalid Item Code");
                }
                txtItemCode.Text = "";
                txtQty.Text = "";
            }
            else if (btnAdd.Text == "UPDATE")
            {
                if (gdt.Rows.Count > 0)
                {
                    gdt.Rows[Convert.ToInt32(lblhdnRowIndex.Text)]["Qty"] = txtQty.Text.Trim();
                    dgv_Items.Rows[Convert.ToInt32(lblhdnRowIndex.Text)].Cells["Qty"].Value = txtQty.Text.Trim();
                }
                txtItemCode.ReadOnly = false;
                txtItemCode.Text = "";
                txtQty.Text = "";
                lblhdnItemID.Text = "";
                lblhdnItemCode.Text = "";
                lblhdnQty.Text = "";
                btnAdd.Text = "ADD";
                lblhdnRowIndex.Text = "";
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    private void dgv_Items_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
    {
        try
        {
            if (MessageBox.Show("Do you want to delete the current row?", "Confirm deletion",
                MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                ScrollPosition = 0;
                ScrollPosition = dgv_Items.FirstDisplayedScrollingRowIndex;
                int iIndex = dgv_Items.CurrentRow.Index;

                    gdt.Rows.RemoveAt(iIndex);

            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    private void dgv_Items_UserDeletedRow(object sender, DataGridViewRowEventArgs e)
    {
        try
        {
            dgv_Items.DataSource = null;
            dgv_Items.DataSource = gdt;
            dgv_Items.Rows[dgv_Items.Rows.Count - 1].Visible = false;

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
4

2 回答 2

0

感谢你们所有人参与解决我的问题。实际上这是索引问题。我找到了在 Datagridview 的 UserDeletingRow 事件中进行更改的解决方案。我在 UserDeletingRow 中添加了一个新行,即使是粗体。现在我的代码工作正常。

private void dgv_Items_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
            {
                try
                {
                    if (MessageBox.Show("Do you want to delete the current row?", "Confirm deletion",
                        MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        ScrollPosition = 0;
                        ScrollPosition = dgv_Items.FirstDisplayedScrollingRowIndex;
                        int iIndex = dgv_Items.CurrentRow.Index;

                            DataRow dr = gdt.Rows[iIndex]; //new added code
                            gdt.Rows.RemoveAt(iIndex);
                            gdt.Rows.InsertAt(dr, iIndex); //new added code
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
于 2013-06-25T09:32:10.627 回答
0

怎么样 ..

ScrollPosition = 0;
dgv_Items.FirstDisplayedScrollingRowIndex=ScrollPosition;
int iIndex = dgv_Items.CurrentRow.Index;

gdt.Rows.RemoveAt(iIndex);
于 2013-06-25T09:21:08.740 回答