简体   繁体   中英

Adding new row to a DataTable

I have a GeidView in my form and I have a button which add new records to the GridView by adding a row to a Datatable and then make this DataTable as the GridContol's Data Source.

The problem is when I add a new record it display in the GridView but when I add another rocord it doesn't display in the GridView, The GridView always contains the first row I added to the DataTable !

So please could you help me to solve this problem ?

This is the source code :

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;
        }

In your Page_Load you have recompensesTable = MakeRecomponsesTable(); . That overwrites the changes and recreate the datatable values

On page postback, variables are restored to their default values and they need to be recreated. You can use Session to maintain your values

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;
   }
}

and retrieve the session preserved value

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;
}

change your

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

To

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

You also must save the datatable to session

recmpensesTable始终是一个新的DateTable,您应将其保存到会话中以备下次使用。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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