簡體   English   中英

如何在DataGridView中編輯和刪除數據?

[英]How to edit and delete data in DataGridView?

我正在VB.NET中開發一個桌面應用程序,我想在其中編輯,刪除以及在DataGridView中添加新行。 我該如何實現?

出現錯誤“從字符串“ Update dbo.Trans set BCode ='1'轉換”,鍵入'Double'無效。”

我嘗試過的代碼:

con =新建SqlConnection(“數據源= USER \\ SQLEXPRESS;初始目錄= SAP;集成安全性= True”)

    If (TbCode.Text = "" Or TbItem.Text = "" Or TbWgt.Text = "") Then
        MessageBox.Show("Please fill all the columns")
    Else
        con.Open()

        cmd1 = New SqlCommand("Update  dbo.Trans set BCode='" + TbCode.Text + "',BName='" + Label2.Text + "',ICode='" + TbItem.Text + "',IName='" + Label3.Text + "',Qty='" + TbQty.Text + "',Weight='" + TbWgt.Text + "',RWeight='" + Label4.Text + "',Rate='" + Rate + "'  where date=@date and depot=@depot and BCode=@bcode and Icode=@icode", con)
        cmd1.Parameters.AddWithValue("@depot", TbDepot.Text)
        cmd1.Parameters.AddWithValue("@icode", TbItem.Text)
        cmd1.Parameters.AddWithValue("@date", IDate.Text)
        cmd1.Parameters.AddWithValue("@bcode", TbCode.Text)
        cmd1.ExecuteNonQuery()


    End If


    con.Close()

    Call Fillgrid()
    Call emptyfun()

End Sub

這里的問題是您正在使用加號來連接字符串,並且應該使用與號。

如果您打開“嚴格選項”,這將由編譯器引起。

這是您更新的代碼。

cmd1 = New SqlCommand("Update  dbo.Trans set BCode='" & TbCode.Text & "',BName='" & Label2.Text & "',ICode='" & TbItem.Text & "',IName='" & Label3.Text & "',Qty='" & TbQty.Text & "',Weight='" & TbWgt.Text & "',RWeight='" & Label4.Text & "',Rate='" & Rate & "'  where date=@date and depot=@depot and BCode=@bcode and Icode=@icode", con)
        cmd1.Parameters.AddWithValue("@depot", TbDepot.Text)
        cmd1.Parameters.AddWithValue("@icode", TbItem.Text)
        cmd1.Parameters.AddWithValue("@date", IDate.Text)
        cmd1.Parameters.AddWithValue("@bcode", TbCode.Text)
        cmd1.ExecuteNonQuery()

但是,從技術上講,您應該對整個update語句使用參數,而不僅僅是where子句。 另一個建議是顯式定義參數的數據類型,而不是使用AddWithValue。

暫無
暫無

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

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