0

I have this code for a command button when pressed it should add data to the database at the same time update the datagridview and add a new row

 private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            cn.Open();
            cmd.CommandText = "INSERT INTO [Table](Code, Name, Price, units)VALUES('" + codeTextBox.Text + "','" + nameTextBox.Text + "','" + priceTextBox.Text + "','" + unitsTextBox.Text + "')";
            cmd.ExecuteNonQuery();
            cn.Close();
            string col1 = codeTextBox.Text;
            string col2 = nameTextBox.Text;
            string col3 = priceTextBox.Text;
            string col4 = unitsTextBox.Text;
            string[] row = { col1, col2, col3, col4 };
            tableDataGridView.Rows.Add(row);
            tableDataGridView.RowCount++;
        }catch(Exception bs)
        {

        }

the outcome is the data from the textboxes does go to the database and it updates when brought to a new row but the row count doesnt increment any ideas?

  • if you have a command button on the DataGridView it appears that you are not inserting the data in the correct event.. also have you stepped through the code setting break points..? also what is the significance of this line here ..`tableDataGridView.RowCount++;` totally useless based on the current code from what I am seeing . – MethodMan Nov 06 '15 at 17:00
  • Off topic for this question, but *use parameterized queries*!! – Thorsten Dittmar Nov 06 '15 at 17:10

1 Answers1

1

It seems that you are using the DataSource property of the DataGridView. When this property is used to bind to data you cannot explicitly add rows directly to the DataGridView. You must instead add rows directy to your data source.

You need to declare a datatable or a list that can be used as a bindable source and then bind this as DataSource

tableDataGridView.DataSource = myDataTable;
tableDataGridView.Refresh();

Simpliest way how to do this that you could understand:

Create your datatable with columns:

public DataTable CreateMyTableDatasource()
       {
           DataTable dt = new DataTable("dt");
           dt.Columns.Add("col1".ToString());
           dt.Columns.Add("col2".ToString());
           dt.Columns.Add("col3".ToString());
           dt.Columns.Add("col4".ToString());
           return dt;
       }

then use a datatable like this:

private void button1_Click(object sender, EventArgs e)
        {
            ....
            DataRow dr;
            DataTable myDataTable = CreateMyTableDatasource();
            dr = myDataTable.NewRow();
            dr["col1"] = codeTextBox.Text;
            dr["col2"] = nameTextBox.Text;
            dr["col3"] = priceTextBox.Text;
            dr["col4"] = unitsTextBox.Text;
            myDataTable.Rows.Add(dr);
            tableDataGridView.DataSource = myDataTable;
            tableDataGridView.Refresh();
        }

But this will create only a one row in your tableDataGridView without any previous data, so first of all, you need to fill all your previous data into a datatable from tableDataGridView and then add a new row. How can I do this?

Community
  • 1
  • 1
krtek
  • 382
  • 4
  • 10