2012-10-13 110 views
0

我的表單中有一個GeidView,我有一個按鈕,它向GridView添加新記錄,方法是向Datatable添加一行,然後將此DataTable作爲GridContol的數據源。向DataTable添加新行

問題是,當我添加一個新的記錄顯示在GridView中,但是當我添加另一個rocord時,它不顯示在GridView中,GridView始終包含我添加到DataTable的第一行!

那麼請你能幫我解決這個問題嗎?

這是源代碼:

private DataTable recompensesTable; 

private void AjoutLivre_Load(object sender, EventArgs e) 
     { 
      recompensesTable = MakeRecomponsesTable(); 
      recompenseGridControl.DataSource = recompensesTable; 
     } 
private DataTable MakeRecomponsesTable() 
     { 
      DataTable recmpensesTable = new DataTable("Recompenses"); 

      var anneeColumn = new DataColumn(); 
      anneeColumn.DataType = Type.GetType("System.Int32"); 
      anneeColumn.ColumnName = "Année"; 
      recmpensesTable.Columns.Add(anneeColumn); 

      var prixLiteraireColumn = new DataColumn(); 
      prixLiteraireColumn.DataType = Type.GetType("System.String"); 
      prixLiteraireColumn.ColumnName = "Prix Litéraire"; 
      recmpensesTable.Columns.Add(prixLiteraireColumn); 

      return recmpensesTable; 
     } 

private void nouveauRecompense_Click(object sender, EventArgs e) 
     { 
      DataRow row = recompensesTable.NewRow(); 

      row[0] = ajoutRecompense.KeyWordAnnee; 
      row[1] = ajoutRecompense.KeyWordPrixLiteraire; 
      recompensesTable.Rows.Add(row); 

      recompenseGridControl.DataSource = recompensesTable; 
     } 

回答

-1

的recmpensesTable始終是一個新的DateTable,你應該把它保存到會話下次使用。

2

在你的Page_Load中你有recompensesTable = MakeRecomponsesTable();。這將覆蓋更改並重新創建數據表值

頁面回發時,變量將恢復爲其默認值,並且需要重新創建它們。您可以使用會話保持你的價值觀

private void AjoutLivre_Load(object sender, EventArgs e) 
{ 
    if(!Page.IsPostBack) 
    { 
    DataTable recompensesTable = MakeRecomponsesTable(); 
    Session["recompensesTable"] = recompensesTable; //Save it to session the first time 
    recompenseGridControl.DataSource = recompensesTable; 
    } 
} 

和檢索會話保存的值

private void nouveauRecompense_Click(object sender, EventArgs e) 
{ 
    DataTable recompensesTable = (DataTable) Session["recompensesTable"]; //retrieve it from session 
    DataRow row = recompensesTable.NewRow(); 

    row[0] = ajoutRecompense.KeyWordAnnee; 
    row[1] = ajoutRecompense.KeyWordPrixLiteraire; 
    recompensesTable.Rows.Add(row); 

    Session["recompensesTable"] = recompensesTable; //save it back to session 

    recompenseGridControl.DataSource = recompensesTable; 
} 
+0

:: +1對於很好的解釋。 :) –

+0

我認爲是爲Asp.Net不是嗎? '因爲我正在使用WinForm應用程序 – user1726655

+1

winforms不使用gridview。它被稱爲DataGridView。你應該在你的問題中指出。在你的'_click'事件中添加相關信息 – codingbiz

0

改變你的

private void AjoutLivre_Load(object sender, EventArgs e) 
    { 
     recompensesTable = MakeRecomponsesTable(); 
     recompenseGridControl.DataSource = recompensesTable; 
    } 

private void AjoutLivre_Load(object sender, EventArgs e) 
    { 
     if(!IsPostback) 
     recompensesTable = MakeRecomponsesTable(); 
     recompenseGridControl.DataSource = recompensesTable; 
    } 

還必須保存數據表sessio n

+0

我認爲對於Asp.Net來說不是嗎? 'cuz我正在研究WinForm應用程序 – user1726655

+0

是的,但是GeidView只能在asp.net中使用而不能在winforms中使用..你的意思是DataGeidView? –

+0

如果是這樣。只需添加'recompenseGridControl.DataBind()'在指定數據源之後,如果'nouveauRecompense_Click'事件中的控件 –