简体   繁体   English

向数据表添加新行

[英]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. 我的表单中有一个GeidView,还有一个按钮,该按钮通过向Datatable添加一行来向GridView添加新记录,然后将该数据表作为GridContol的数据源。

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 ! 问题是当我添加一条新记录时,它显示在GridView中,但是当我添加另一个记录时,它不在GridView中显示,该GridView始终包含我添加到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(); 在您的Page_Load中,您有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,您应将其保存到会话中以备下次使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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