0

I have a winforms application that I am developing, I have hit a dead end. What I am trying to do is on each "click", add a new row to my DataTable with the values input in the form. This Datatable is the DataSource for my DataGridView. Can someone point me in the right direction on how this can be achieved.

Articles I looked at:

How to add new row to datatable gridview

My code:

private void btnAdd_Click(object sender, EventArgs e)
        {
        //inserting into order table
        DataTable dt = new DataTable();

        string articleId = cmbArticle.Text;
        string productDescription = txtDesc.Text;
        string type = txtType.Text;
        string materialType = txtMaterial.Text;
        string size = cmbSizes.Text;
        string quantity = txtQuantity.Text;

        try
        {
            dt.Columns.Add("Article");
            dt.Columns.Add("Description");
            dt.Columns.Add("Type");
            dt.Columns.Add("Material");
            dt.Columns.Add("Size");
            dt.Columns.Add("Quantity");
            dt.Columns.Add("DateTime");

            DataRow dr = dt.NewRow();
                //addrows
                dr["Article"] = articleId;
                dr["Description"] = productDescription;
                dr["type"] = type;
                dr["Material"] = materialType;
                dr["Size"] = size;
                dr["Quantity"] = quantity;

            dt.Rows.Add(dr);

            dgvView.DataSource = dt;
        }
        catch (Exception ex)
        {

        }
    }
Community
  • 1
  • 1
Harry
  • 3,031
  • 7
  • 42
  • 67
  • Can you explain how you code isn't working? What *exactly* is going wrong? – rory.ap Dec 18 '14 at 19:25
  • The problem is, when I click on add it adds a row to the datatable which is fine. But if i click on add again to add different information it just overwrite the existing row? I want to be able to add new rows... – Harry Dec 18 '14 at 19:29
  • 1
    The code you posted add a single row to a new DataTable. I mean that you are allocating a new datatable each time the click event is raised. – LPs Dec 18 '14 at 19:29

1 Answers1

4

On each click you are creating a new DataTable which would be with just one row, You need to create DataTable once and then just keep adding rows to in the click. Define your DataTable at class level and then in your event just add a new row to it.

DataTable dt = new DataTable(); //at class level
private void Form1_Load(object sender, EventArgs e)
{
    CreateDataTableColumns();
    //.... your code
}

Then have a method to create table structure, call that method once from your From_Load event.

private void CreateDataTableColumns()
{
    dt.Columns.Add("Article");
    dt.Columns.Add("Description");
    dt.Columns.Add("Type");
    dt.Columns.Add("Material");
    dt.Columns.Add("Size");
    dt.Columns.Add("Quantity");
    dt.Columns.Add("DateTime");
}

Later add rows to your class level DataTable in Add event.

private void btnAdd_Click(object sender, EventArgs e)
{
    string articleId = cmbArticle.Text;
    string productDescription = txtDesc.Text;
    string type = txtType.Text;
    string materialType = txtMaterial.Text;
    string size = cmbSizes.Text;
    string quantity = txtQuantity.Text;

    try
    {
        DataRow dr = dt.NewRow();
        //addrows
        dr["Article"] = articleId;
        dr["Description"] = productDescription;
        dr["type"] = type;
        dr["Material"] = materialType;
        dr["Size"] = size;
        dr["Quantity"] = quantity;
        dt.Rows.Add(dr);
        dgvView.DataSource = dt;
    }
    catch (Exception ex)
    {

    }
}

(I believe you are doing something with the exception object in your catch block, like logging, showing message to user etc)

Habib
  • 219,104
  • 29
  • 407
  • 436
  • Thank you @habib, I realise my two mistakes. Firstly datatable was being created each time like you pointed out, and secondly to create the columns separately. – Harry Dec 18 '14 at 19:39