如何在按钮点击事件中向数据表添加行?

时间:2013-11-04 09:39:11

标签: c# winforms datagridview datatable databound

Windows窗体中,我希望通过从其他控件获取值,逐行添加到按钮单击事件的DataGridView中。我在这种情况下使用DataTable,它绑定到DataGridView。

我正在使用以下代码,并且在第一次插入数据时工作正常。但我的问题是当我第二次点击按钮时,第一行被第二行数据覆盖。

private void btnAddToGrid_Click(object sender, EventArgs e)
{
        LoadDataGridView();
}

private void LoadDataGridView()
{
        dgvAdjustment.DataSource = GetAdjustmentTable();
}

private DataTable GetAdjustmentTable()
{
        DataTable adjustmentTable = new DataTable();

        DataColumn dataColumn;
        dataColumn = new DataColumn("SourceOutletID", typeof(int));
        adjustmentTable.Columns.Add(dataColumn);
        dataColumn = new DataColumn("DestinationOutletID", typeof(int));
        adjustmentTable.Columns.Add(dataColumn);
        dataColumn = new DataColumn("TransactionDate", typeof(DateTime));
        adjustmentTable.Columns.Add(dataColumn);
        dataColumn = new DataColumn("MaterialName", typeof(string));
        adjustmentTable.Columns.Add(dataColumn);
        dataColumn = new DataColumn("AdjustmentType", typeof(int));
        adjustmentTable.Columns.Add(dataColumn);
        dataColumn = new DataColumn("CurrentBalance", typeof(decimal));
        adjustmentTable.Columns.Add(dataColumn);
        dataColumn = new DataColumn("AdjustmentQty", typeof(decimal));
        adjustmentTable.Columns.Add(dataColumn);
        dataColumn = new DataColumn("NewBalance", typeof(decimal));
        adjustmentTable.Columns.Add(dataColumn);

        DataRow dataRow = adjustmentTable.NewRow();
        dataRow[0] = cmbSourceOutlet.SelectedValue;
        dataRow[1] = cmbDestinationOutlet.SelectedValue;
        dataRow[2] = TransDateTimePicker.Value;
        dataRow[3] = cmbMaterialName.SelectedValue;
        dataRow[4] = cmbAdjustmentType.SelectedValue;
        dataRow[5] = Convert.ToDecimal(lblCurBalVal.Text);
        dataRow[6] = Convert.ToDecimal(lblAdjVal.Text);
        dataRow[7] = Convert.ToDecimal(lblNewQtyVal.Text);

        int insertPosition = adjustmentTable.Rows.Count;
        adjustmentTable.Rows.InsertAt(dataRow, insertPosition);

        return adjustmentTable;
}

ASP .NET 应用程序中,我使用会话状态通过使用以下代码检查DataTable是否为null:

protected void Button1_Click(object sender, EventArgs e)
{
  try
  {
     //Check if previous session is exist
     if (Session["MyTable"] == null)
     {
         dtMyTable = new DataTable("MyTable");
         dtMyTable.Columns.Add("Id", typeof(int));
         dtMyTable.Columns.Add("LName", typeof(string));

     }
     else
     {
         //If yes then get it from current session
         dtMyTable = (DataTable)Session["MyTable"];
     }

     //Add new row every time
     DataRow dt_row;
     dt_row = dtMyTable.NewRow();
     dt_row["Id"] = TextBox1.Text;
     dt_row["LName"] = TextBox2.Text;
     dtMyTable.Rows.Add(dt_row);

     //Update session table
     Session["MyTable"] = dtMyTable;
  }
  catch (Exception ex)
  {

     Response.Write(ex.Message);
  }
}

我该怎么办?如何进行更改以在 Windows窗体中获得正确的解决方案?任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

GetAdjustmentTable中,您每次都在重新创建adjustmentTable。因此,新行将覆盖现有行。

您需要修改代码,使adjustmentTable仅创建一次,后续调用添加到它。一个wya要做的就是使它成为一个私有字段并检查它是否为空,并创建它如果是:

private DataTable _adjustmentTable;

private DataTable GetAdjustmentTable()
{
    if (adjustmentTable == null)
    {
        adjustmentTable = new DataTable();
    }
    ....