簡體   English   中英

我的刪除按鈕 gridview 中的行索引錯誤

[英]Row Index error in my delete button gridview

我顯示的這段代碼在單元格單擊事件中完全正常。 但是我在 gridview 中插入了一些按鈕,其中之一是“刪除”按鈕。 當我將代碼從單元格單擊事件傳輸到按鈕刪除時,我遇到了這個錯誤。 有人可以告訴我我需要在代碼中更改什么才能修復刪除功能嗎?

private void button3_Click(object sender, EventArgs e)
        {
            string maincon = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;
            SqlConnection sqlcon = new SqlConnection(maincon);
            string rfidno = dataGridView1.Rows[e.RowIndex].Cells["rfidno"].FormattedValue.ToString();

            DialogResult dialogResult = MessageBox.Show("Are you sure you want to delete this information?", "Delete function triggered", MessageBoxButtons.YesNo);
            if (dialogResult == DialogResult.Yes)
            {
                try
                {
                    SqlDataAdapter da = new SqlDataAdapter();
                    sqlcon.Open();
                    da.DeleteCommand = new SqlCommand("delete from tbl_registerStudent where rfidno = '" + rfidno + "'", sqlcon);
                    da.DeleteCommand.ExecuteNonQuery();

                    MessageBox.Show("" + rfidno);
                    MessageBox.Show("Delete Successfull");
                    sqlcon.Close();
                    bindGrid();
                }
                catch (SqlException ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else if (dialogResult == DialogResult.No)
            {
                CreateID objs = new CreateID();
                if (objs == null)
                {
                    objs.Parent = this;
                }
                objs.Show();
                this.Hide();
            }
        }

在此處輸入圖片說明

您不能在 buttonclick 事件處理程序中使用 e.RowIndex,因為傳入的e沒有該屬性。

你應該像以前一樣處理這個問題; 將處理程序附加到 datagridview 的 CellContentClick 並執行此代碼,如果單擊的列是按鈕列,即不要這樣做:

當我從單元格單擊事件傳輸代碼時

只需進行單元格單擊事件測試“如果單擊的列是按鈕列,則......”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM