简体   繁体   English

如何使用数据适配器或vb.net中的其他方法通过datagridview将记录插入到sql中以进行更新

[英]how to update, insert records through datagridview to sql using data adapter or other method in vb.net

hello best programmers in the world i need your expert advise and assistance with regards to my project. 你好,世界上最好的程序员,我需要我的项目方面的专家建议和帮助。 I am trying to insert and update my table through datagridview by clicking a command button, i have my datagridview properties set to editmode : editprogrammatically, 我试图通过单击命令按钮通过datagridview插入和更新我的表,我将datagridview属性设置为editmode:以编程方式进行编辑,

here's the code, of where i pulled up my datagridview content: 这是我提取datagridview内容的代码:

Public Sub loaddgvfrm3()
    cmdconn = New SqlConnection
    cmd = New SqlCommand
    cmdconn.ConnectionString = sqlstr
    cmdconn.Open()
    cmd.Connection = cmdconn
    cmd.CommandText = "select  period, VOUCH_AMT, INDIVIDUAL_AMT, check_no, D_MAILED,           DIR_NO from tobee.EBD_BILLHISTORY where CLAIM_NO like '" + txtClaimno.Text + "'"


    Dim dt As New DataTable
    da = New SqlDataAdapter
    da.SelectCommand = cmd
    da.Fill(dt)
    Me.DataGridView1.DataSource = dt
    Me.DataGridView2.DataSource = dt
    cmdconn.Close()

End Sub

now i have my command buttons here here's the add button: (im prefering to add a row within the selected table) 现在我有我的命令按钮,这里是添加按钮:(我更喜欢在所选表中添加一行)

Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click

End Sub

here's the Edit button: 这是“编辑”按钮:

Private Sub btnEditmain_Click(sender As Object, e As EventArgs) Handles btnEditmain.Click

    ''Form1.ShowDialog()
    'DataGridView2.AllowUserToAddRows = True
    ''DataGridView2.BeginEdit(True)
    'btnSave.Enabled = True

End Sub and here's the save button that should save all changes that i have done, End Sub,这是保存按钮,应保存我所做的所有更改,

Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click

    Dim connstr As String = "server=midtelephone\sqlexpress; database=testdb; user= sa; password=sa;"

End Sub

i left command button contents empty because i need to create a new method of inserting, updating the row. 我将命令按钮的内容留空了,因为我需要创建一种新的插入,更新行的方法。 because the one that i have earlier was a fail, although it inserts the data in the sql, but not in its appropriate row, take a look at here: Data inserted from datagridview not updated in SQL Server , instead it creates another row which is not connected with '" + txtClaimno.Text + "'" (stated from above) so what happens is that it stacks a new row with no connected owner from another table(claim_no < this claim_no is connected from another table as a fk in the database but a primary key in (billhistory table)) can you pls help me get rid of this problem as i am having a hard time moving to the next phase of our project? im just a high school student, so pls bear with me :) i'll appreciate if u give comments / answers regarding my question :) thanks in adv. 因为我之前的操作失败了,尽管它在sql中插入了数据,但没有在其适当的行中插入数据,所以请看这里: 从datagridview插入的数据未在SQL Server中更新 ,而是创建了另一行没有与'“ + txtClaimno.Text +”'“(从上面陈述)连接,所以发生的是它在没有连接所有者的情况下从另一个表中堆叠了一个新行(claim_no <此claim_no从另一个表中作为fk连接到数据库,但(票据历史表)中的主键)您能否帮助我摆脱这个问题,因为我很难进入项目的下一阶段?我只是一名高中生,所以请耐心等待: )如果您对我的问题发表评论/回答,我将不胜感激。

my mentor advised me to use data adapter accept changes stuff, but i don't know how to implement such stuffs. 我的导师建议我使用数据适配器接受更改内容,但我不知道如何实现此类内容。 pls help me thank you :) 请帮我谢谢:)

Use Event to get data from gridview on cellConttent Click and these values to a query or Stored procedure. 使用事件从cellConttent Click上的gridview获取数据,并将这些值存储到查询或存储过程中。

 private void grdCampaignStats_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

                //I want to get value of SomeColumn of grid view having datatype String 
                string SomeVariabel = grd["CoulmnNameinGRID", e.RowIndex].Value.ToString();

           // Make it according to Vb it is C# code 
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM