0

我的表单中有一个 GeidView,我有一个按钮,它通过向 Datatable 添加一行来向 GridView 添加新记录,然后将此 DataTable 作为 GridContol 的数据源。

问题是当我添加一条新记录时,它会显示在 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;
        }
4

3 回答 3

2

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;
}
于 2012-10-13T01:14:34.170 回答
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;
    }

您还必须将数据表保存到会话

于 2012-10-13T01:16:44.303 回答
-1

the recmpensesTable is always a new DateTable, you should save it to session for next use.

于 2012-10-13T01:13:23.287 回答