locked
IDs not refreshed when new rows are added to Datagridview RRS feed

  • Question

  • Hi,

      I'm a newbie for .Net. I have a datagridview which is populated by a datatable. (Note: I fill this datatable by passing this datatable as a reference to another project that uses a dataadapter and dataset and return values in this datatable)

    The grid is populated and when the user clicks on the "Add" button then I programttically create a datarow and put the focus on the newly created cell in the datagridview. When the user either moves to the next row or hits the "Save" button then this datatable with the new values is sent (byref) to the project that fetched data to be saved back to the db (using the dataadapter.update). It works fine in saving it to the db, but the new id is not refreshed in the datatable. Any help would be appreciated.

     

    Thanks,

    SS

    Wednesday, April 25, 2007 4:26 PM

Answers

  •  

    Hi

    The Code below is just for reference .

     

    Code Snippet

            private void saveBt_Click(object sender, EventArgs e)

            {

                int rowCount = this.dataGridView1.Rows.Count;

                string value = this.dataGridView1.Rows[rowCount - 2].Cells[1].Value.ToString();

                SqlConnection cn = new SqlConnection("Data Source=localhost;Initial Catalog=test;Integrated Security=True");

                if (cn.State != ConnectionState.Open) cn.Open();

                SqlCommand cm = new SqlCommand("insert into testidentity(strings) values('" + value + "')", cn);

                try

                {

                    cm.ExecuteNonQuery();

                }

                catch (SqlException mm ) 

                {

                    MessageBox.Show("Fail to save!");

                    return;

                }

     

                cm.CommandText = "select top 1 autoId from testidentity order by autoId DESC";

                try

                {

                    string tt = cm.ExecuteScalar().ToString();

                    this.dataGridView1.Rows[rowCount - 2].Cells[0].Value = tt;

                }

                catch

                {

                    MessageBox.Show("Fail to get auto NO.!");

                }

            }

     

    There is another way to implement it . Setting the AutoIncrement property to true for the Identity column of database will make it more easier.Just coding like:

     

    Code Snippet

            DataColumn workColumn = workTable.Columns.Add("CustomerID", typeof(Int32));

            workColumn.AutoIncrement = true;

            workColumn.AutoIncrementSeed = 200;

            workColumn.AutoIncrementStep = 3;

     

    Tuesday, May 1, 2007 10:51 AM

All replies

  •  

    Hi,

     

    Is the id auto generated by the db? if so, you should read it and fill it to the datatable.

    Thursday, April 26, 2007 8:49 AM
  • How do I do that? I'm just using the dataadapter.update(datatable) to update the db? This Id is the identity column in the db.

     

    Thanks,

    SS

    Thursday, April 26, 2007 2:10 PM
  •  

    Hi

    The Code below is just for reference .

     

    Code Snippet

            private void saveBt_Click(object sender, EventArgs e)

            {

                int rowCount = this.dataGridView1.Rows.Count;

                string value = this.dataGridView1.Rows[rowCount - 2].Cells[1].Value.ToString();

                SqlConnection cn = new SqlConnection("Data Source=localhost;Initial Catalog=test;Integrated Security=True");

                if (cn.State != ConnectionState.Open) cn.Open();

                SqlCommand cm = new SqlCommand("insert into testidentity(strings) values('" + value + "')", cn);

                try

                {

                    cm.ExecuteNonQuery();

                }

                catch (SqlException mm ) 

                {

                    MessageBox.Show("Fail to save!");

                    return;

                }

     

                cm.CommandText = "select top 1 autoId from testidentity order by autoId DESC";

                try

                {

                    string tt = cm.ExecuteScalar().ToString();

                    this.dataGridView1.Rows[rowCount - 2].Cells[0].Value = tt;

                }

                catch

                {

                    MessageBox.Show("Fail to get auto NO.!");

                }

            }

     

    There is another way to implement it . Setting the AutoIncrement property to true for the Identity column of database will make it more easier.Just coding like:

     

    Code Snippet

            DataColumn workColumn = workTable.Columns.Add("CustomerID", typeof(Int32));

            workColumn.AutoIncrement = true;

            workColumn.AutoIncrementSeed = 200;

            workColumn.AutoIncrementStep = 3;

     

    Tuesday, May 1, 2007 10:51 AM