如何在按钮单击时更新datagridview中的记录?

问题描述 投票:0回答:2

我使用datetimepicker控件在两个日期之间绑定DataGridView中的数据。现在我想在按钮点击时更新所有记录,当我在DataGridView中编辑记录时。这是在DataGridView中显示记录的代码。

 OleDbCommand cmd = new OleDbCommand("select i.billno,i.idate,i.cname,i.address,i.mob,p.brand_name,p.category,p.price,i.quantity,p.gstper_product,i.sub_total,t.grand_total from (customer_invoice_details i inner join product p on i.pid=p.pid) inner join totalamt t on i.billno=t.billno where i.idate between @date1 and @date2", con);
            cmd.Parameters.AddWithValue("@date1", dateTime1.Value.ToString());
            cmd.Parameters.AddWithValue("@date2", datetimeto.Value.ToString());
            OleDbDataAdapter da = new OleDbDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            dataGridView1.DataSource = dt;

帮帮我怎么做...

c# winforms datagridview oledb
2个回答
0
投票

我有类似的问题。要更新datagrid,我使用了refresh方法。

dataGridView1.Refresh();

还有一点,它通过重置数据绑定到数据网格为我工作

dataGridView1.DataSource = null;
dataGridView1.DataSource = dt;

0
投票

你将如何更新提及的记录。我试着给你一个样品。

从DataGridView获取选定的行值到TextBoxes

 Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick

        ' get the index of the selected datagridview row
        index = e.RowIndex
        Dim selectedRow As DataGridViewRow

   ' show data from the selected row to textboxes
        selectedRow = DataGridView1.Rows(index)
        TextBoxID.Text = selectedRow.Cells(0).Value.ToString()
        TextBoxFN.Text = selectedRow.Cells(1).Value.ToString()
        TextBoxLN.Text = selectedRow.Cells(2).Value.ToString()
        TextBoxAGE.Text = selectedRow.Cells(3).Value.ToString()

    End Sub

使用文本框更新datagridview选定的行

 Private Sub btn_Update_Click(ByVal sender As Object, ByVal e As EventArgs)
    If txt_Name.Text <> "" AndAlso txt_State.Text <> "" Then
        cmd = New SqlCommand("update tbl_Record set Name=@name,State=@state where ID=@id", con)
        con.Open()
        cmd.Parameters.AddWithValue("@id", ID)
        cmd.Parameters.AddWithValue("@name", txt_Name.Text)
        cmd.Parameters.AddWithValue("@state", txt_State.Text)
        cmd.ExecuteNonQuery()
        MessageBox.Show("Record Updated Successfully")
        con.Close()
        DisplayData()
        ClearData()
    Else
        MessageBox.Show("Please Select Record to Update")
    End If
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.