簡體   English   中英

插入/刪除datagridview后未顯示插入的項目

[英]After inserting/deleting datagridview not shows the inserted item

private void btndelete_Click(object sender, EventArgs e)
{
    if (txtserch.Text == "")
    {
        MessageBox.Show("Enter Customer name to delete");
    }
    else
    {
        l1.conn();
        SqlCommand cmd = new SqlCommand("delete from CUSTOMER where NAME='" + txtserch.Text + "'", l1.connection);
        int a = cmd.ExecuteNonQuery();
        if (a == 1)
        {
            MessageBox.Show("Deleted Sucessfully");
            dataGridView1.Refresh();
            dataGridView1.RefreshEdit();
            dataGridView1.Update();
            txtserch.Text = "";
        }
        else
        {
            MessageBox.Show("Customer Not Exist");
            txtserch.Text = "";
        }
    }
}

DataGridView.Refresh()是圖形刷新,而不是數據刷新。 將您的DatagridView重新綁定到源。

DataGridView dataGridView1 = new DataGridView();
dataGridView1.DataSource = source;

// Update Data in src1

dataGridView1.DataSource = null;
dataGridView1.DataSource = source;

為了刷新您的網格重新綁定它。

MyGridView.DataSource = ds;
MyGridView.DataBind();

ds是您要提供給網格的數據源。

請參閱此鏈接以獲取詳細信息

如果要在編輯/刪除/更新操作后將數據綁定到Gridview,則要在下面的代碼行中編寫

GridviewName.DataSource = ds;// retrieve the details and store in ds
GridViewName.DataBind();

暫無
暫無

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

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