簡體   English   中英

如何在選擇行並更改文本框值並單擊更新后更新 datagridview? 它連接到訪問數據庫

[英]how to update datagridview after selecting row and changed in text box value and clicking update? which is connected to access db

我想在點擊按鈕更新后更新 datagridview 列表,這里是圖像

當我選擇原始標題時,它會在文本框上顯示,並且在更改值並單擊“更新”后,其顯示的消息記錄已成功更新。 這是圖片

但它沒有在 datagridview 中更新..

private void UpdateButton_Click(object sender, EventArgs e)
        {
            
            
            if (ValueTextBox.Text != "" && TypeTextBox.Text != "")
            {

                cmd = new OleDbCommand("update Sflorotype set Sflorovalues=@value,Sflorotypes=@type where ID=@id", con);
                con.Open();
                cmd.Parameters.AddWithValue("@id", ID);
                cmd.Parameters.AddWithValue("@value", ValueTextBox.Text);
                cmd.Parameters.AddWithValue("@type", TypeTextBox.Text);
                var returnValue =
                cmd.ExecuteNonQuery();
                MessageBox.Show("Record Updated Successfully");
                con.Close();
                DisplayData();
                ClearData();
            }
            else
            {
                MessageBox.Show("Please Select Record to Update");
            }
        }
        OleDbDataAdapter adapt;
        private void DisplayData()
        {
            con.Open();
            DataTable dt = new DataTable();
            adapt = new OleDbDataAdapter("select * from Sflorotype", con);
            adapt.Fill(dt);
            dataGridViewList.DataSource = dt;
            con.Close();
        }
        private void ClearData()
        {
            ValueTextBox.Text = "";
            TypeTextBox.Text = "";
            ID = 0;
        }

 private void dataGridViewList_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            ID = Convert.ToInt32(dataGridViewList.Rows[e.RowIndex].Cells[0].Value.ToString());
            ValueTextBox.Text = dataGridViewList.Rows[e.RowIndex].Cells[1].Value.ToString();
            TypeTextBox.Text = dataGridViewList.Rows[e.RowIndex].Cells[2].Value.ToString();
        }

抱歉無法添加評論 - 我是新來的。

cmd = new OleDbCommand("update Sflorotype set Sflorovalues=@value,Sflorotypes=@type where ID=@id", con);

你確定ID是94嗎? (在您的圖像中,有 ID 94)因為我看不到您從哪里獲得這個值。

我只在這里找到 ID,你把它設置為 0:

    private void ClearData()
    {
        ValueTextBox.Text = "";
        TypeTextBox.Text = "";
        ID = 0;
    }

我只在這里看到一些 ID 值。 如果您不在其他地方更改它,它將使用值 0 更新 ID。

您可以輕松獲取正在執行的信息:

string command = "update Sflorotype set Sflorovalues=@value,Sflorotypes=@type where ID=@id";
MessageBox.Show(command);
cmd = new OleDbCommand(command, con);
            

暫無
暫無

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

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