簡體   English   中英

在同一表格上插入后刷新datagridview

[英]Refresh datagridview after insert on the same form

我有一個帶有datagridview的表單和一個帶有用於插入值的按鈕的輸入框。 單擊按鈕后,我希望datagridview刷新。 我已經嘗試過datagridview.refresh()和datagridview.update選項。 僅在關閉並重新打開應用程序后,數據才會顯示。

這是我的代碼:

class cFunction
{
    public static void DoSQL(string Query)
    {
        SqlConnection Connection = new SqlConnection(@" Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Zahida\Desktop\RapidsoftSupport\RapidsoftSupport\MainData.mdf;Integrated Security=True");

        SqlCommand Command = new SqlCommand(Query, Connection);
        Command.Connection.Open();
        Command.ExecuteNonQuery();
        Command.Connection.Close();
        Connection.Close();

    }
}

private void btnAdd_Click(object sender, EventArgs e)
    {
        cFunction.DoSQL("INSERT INTO Problem(SYSTEM_ID,SUBJECT, KEYWORDS) VALUES('" + SID + "','" + txtSubject.Text + "','" + txtKeywords.Text + "')");
        this.problemTableAdapter.Fill(this.mainDataDataSet1.Problem);

    }

有什么幫助嗎?

網格控件必須綁定到活動和活動的數據源,在顯示程序加載和網格緩存主題時,您將一些記錄推送到數據集。

您可以在每次插入后使用綁定或運行select查詢,以使用新記錄更新數據集。

Zahida Kazi,

首先,您不需要強制兩個關閉:
Command.Connection.Close();
的Connection.close();

我沒有看到gridview綁定。 如果您現在還沒有設置綁定,我建議您使用DataTable創建數據並通過DataGridView.DataSource = DataTable綁定。 數據更新后,可以使用兩種方法來更新gridview的數據

  1. 您可能要更新適當的網格列值
  2. 您可以重新綁定gridview的數據源

暫無
暫無

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

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