1

In Windows Forms, I want to add row by row into DataGridView on button click event by taking the values from other controls. I am using DataTable in this case and it is bound to DataGridView.

I am using the following code and it is working fine when the data is inserted for the first time. But my problem is when I click the button for the second time, the first row is overwritten with the second row of data.

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

In ASP .NET applications, I use the session state to check whether DataTable is null by using the following code:

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

What can I do and how can I make the changes to get the right solution in Windows Forms? Any help will be much appreciated!

1 Answer 1

1

In GetAdjustmentTable, you are recreating adjustmentTable every time. So a new row will just overwrite the existing one.

You need to modify the code such that adjustmentTable is only created just once and subsequent calls add rows to it. One way to do that would be to make it a private field and to check if it's null, and create it if it is:

private DataTable _adjustmentTable;

private DataTable GetAdjustmentTable()
{
    if (adjustmentTable == null)
    {
        adjustmentTable = new DataTable();
    }
    ....
Sign up to request clarification or add additional context in comments.

1 Comment

You are absolutely right! I declared DataTable outside the method and checked whether DataTable is null. Thanks for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.