none
How to Update a Database using DatarowView?

    Question

  • Hello,
    I use Typed Dataset and Select data bu using view to my dataset. There is no problem getting data properly. On the other hand for example when I need to update a record, I double click on the row in my datagridview and open a new tab for updating data. But as you know, I cannot update view. So, first I have tried to fill a new datatable when I selecting the records. And then, I just update this datatable for updating the data. But it is not so magical to fill one more datatable in the dataset just in case update only one record. Therefore, could you give me effective examples about the issues below?

    When double clicking a row on the datagridview, I just want to select only active(double clicked) record data from database and to bind it to a datarow. And then to bind this datarow values to my textboxes. After changing data from textboxes, I want to update this datarow and then update dataset.
    Thanks in advance..

    Tuesday, January 19, 2010 2:27 PM

Answers

  • //My Table Column
    //FiniName, SemiID, SemiSpec, SemiName ,AddID
    
    //Update Database
    
    public SqlConnection getcon()
    {
        string sqlcon = "Data Source=(local);Database=YKTGOODS;user Id=sa;PWD=";
        SqlConnection myCon = new SqlConnection(sqlcon); 
        return myCon;
    }
    
    public void getcom(string M_str_sqlstr)
    {
        SqlConnection sqlcon = this.getcon();
        sqlcon.Open();
        SqlCommand sqlcom = new SqlCommand(M_str_sqlstr, sqlcon);
        sqlcom.ExecuteNonQuery();
        sqlcom.Dispose();
        sqlcon.Close();
        sqlcon.Dispose();
    }
    
    private void btnUpdate_Click(object sender, EventArgs e)
    {
        try
        {   
            //dgvInputGoods is data grid view
            string FiniName = dgvInputGoods[0, dgvInputGoods.CurrentCell.RowIndex].Value.ToString();
            string SemiID = dgvInputGoods[1, dgvInputGoods.CurrentCell.RowIndex].Value.ToString();
            string SemiSpec = dgvInputGoods[2, dgvInputGoods.CurrentCell.RowIndex].Value.ToString();
            string SemiName = dgvInputGoods[3, dgvInputGoods.CurrentCell.RowIndex].Value.ToString();
    
           this.getcom("update tb_InputGoods set FiniName = '" + FiniName.Trim() + "', SemiID = '" + SemiID.Trim() + "', SemiSpec = '" + SemiSpec.Trim() + "', SemiName = '" + SemiName.Trim() + "' WHERE AddID = " + Convert.ToInt32(dgvInputGoods[4, dgvInputGoods.CurrentCell.RowIndex].Value) + "");
    
            MessageBox.Show("Update Success", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
    
    
            frmMain_Load(sender, e);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Alert", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
    }
    
    
    //bind data values to textboxes
    private void dgvInputGoods_CellContentClick(object sender, DataGridViewCellEventArgs e) 
    {
        txtFiniProducts.Text = Convert.ToString(dgvInputGoods[0, dgvInputGoods.CurrentCell.RowIndex].Value).Trim();
    
        txtSemiID.Text = Convert.ToString(dgvInputGoods[1, dgvInputGoods.CurrentCell.RowIndex].Value).Trim();
    
        txtSemiSpec.Text = Convert.ToString(dgvInputGoods[2, dgvInputGoods.CurrentCell.RowIndex].Value).Trim();
    
        txtSemiName.Text = Convert.ToString(dgvInputGoods[3, dgvInputGoods.CurrentCell.RowIndex].Value).Trim();
    
    }

    If (My Answer) Please mark the replies as answer. Thanks; Else Thank you all the same;
    • Marked as answer by Jing0Moderator Thursday, January 28, 2010 2:57 AM
    Thursday, January 21, 2010 1:32 AM
  • Hi,

     

    Based on my understanding, you bind the datagridview to dataview, then you want to binding the textbox to the current row and update the changed value to the database.

     

    This can be done by databinding.

     

    If you bind the textbox to a datasource as the same as dataGridView datasource, the textbox will auto-update its value according to datagridview current selected row.

     

    When you update the dataview, the datatable will also update its value.

     

    In the view of the above analysis, you just need bind the textbox datasource as the same as the datagridview datasource, and update the datatable to database.

     

    If you want to only update one row, it’s the same as the ordinary updating. The updatecommand would be like this

     “update datatable set ************** where id = ******”

     

            private void Form3_Load(object sender, EventArgs e)

            {

                #region MyTable

                DataTable dt01 = new DataTable();

                dt01.Columns.Add("col01");

                dt01.Columns.Add("col02");

                dt01.Columns.Add("col03");

     

                dt01.Rows.Add("aaaaaaaaaaaa", "11", "a1");

                dt01.Rows.Add("bb", "22", "b2");

                dt01.Rows.Add("cc", "33", "c3");

                dt01.Rows.Add("dd", "44", "d4");

                #endregion

     

                // try change the datagridview current cell in different row and see the textbox text.

                dataGridView1.DataSource = dt01.DefaultView;

     

                textBox1.DataBindings.Add("text", dt01.DefaultView, "col01", true);

     

                // check the datatable

                dataGridView2.DataSource = dt01;

     

            }

     

    Best regards,

    Ling Wang


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by Jing0Moderator Thursday, January 28, 2010 2:57 AM
    Tuesday, January 26, 2010 8:04 AM

All replies

  • //My Table Column
    //FiniName, SemiID, SemiSpec, SemiName ,AddID
    
    //Update Database
    
    public SqlConnection getcon()
    {
        string sqlcon = "Data Source=(local);Database=YKTGOODS;user Id=sa;PWD=";
        SqlConnection myCon = new SqlConnection(sqlcon); 
        return myCon;
    }
    
    public void getcom(string M_str_sqlstr)
    {
        SqlConnection sqlcon = this.getcon();
        sqlcon.Open();
        SqlCommand sqlcom = new SqlCommand(M_str_sqlstr, sqlcon);
        sqlcom.ExecuteNonQuery();
        sqlcom.Dispose();
        sqlcon.Close();
        sqlcon.Dispose();
    }
    
    private void btnUpdate_Click(object sender, EventArgs e)
    {
        try
        {   
            //dgvInputGoods is data grid view
            string FiniName = dgvInputGoods[0, dgvInputGoods.CurrentCell.RowIndex].Value.ToString();
            string SemiID = dgvInputGoods[1, dgvInputGoods.CurrentCell.RowIndex].Value.ToString();
            string SemiSpec = dgvInputGoods[2, dgvInputGoods.CurrentCell.RowIndex].Value.ToString();
            string SemiName = dgvInputGoods[3, dgvInputGoods.CurrentCell.RowIndex].Value.ToString();
    
           this.getcom("update tb_InputGoods set FiniName = '" + FiniName.Trim() + "', SemiID = '" + SemiID.Trim() + "', SemiSpec = '" + SemiSpec.Trim() + "', SemiName = '" + SemiName.Trim() + "' WHERE AddID = " + Convert.ToInt32(dgvInputGoods[4, dgvInputGoods.CurrentCell.RowIndex].Value) + "");
    
            MessageBox.Show("Update Success", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
    
    
            frmMain_Load(sender, e);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Alert", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
    }
    
    
    //bind data values to textboxes
    private void dgvInputGoods_CellContentClick(object sender, DataGridViewCellEventArgs e) 
    {
        txtFiniProducts.Text = Convert.ToString(dgvInputGoods[0, dgvInputGoods.CurrentCell.RowIndex].Value).Trim();
    
        txtSemiID.Text = Convert.ToString(dgvInputGoods[1, dgvInputGoods.CurrentCell.RowIndex].Value).Trim();
    
        txtSemiSpec.Text = Convert.ToString(dgvInputGoods[2, dgvInputGoods.CurrentCell.RowIndex].Value).Trim();
    
        txtSemiName.Text = Convert.ToString(dgvInputGoods[3, dgvInputGoods.CurrentCell.RowIndex].Value).Trim();
    
    }

    If (My Answer) Please mark the replies as answer. Thanks; Else Thank you all the same;
    • Marked as answer by Jing0Moderator Thursday, January 28, 2010 2:57 AM
    Thursday, January 21, 2010 1:32 AM
  • Hi Huan,
    thanks for reply. But I meant "DatarowView", not "DatagridView".
    Monday, January 25, 2010 3:29 PM
  • Hi,

     

    Based on my understanding, you bind the datagridview to dataview, then you want to binding the textbox to the current row and update the changed value to the database.

     

    This can be done by databinding.

     

    If you bind the textbox to a datasource as the same as dataGridView datasource, the textbox will auto-update its value according to datagridview current selected row.

     

    When you update the dataview, the datatable will also update its value.

     

    In the view of the above analysis, you just need bind the textbox datasource as the same as the datagridview datasource, and update the datatable to database.

     

    If you want to only update one row, it’s the same as the ordinary updating. The updatecommand would be like this

     “update datatable set ************** where id = ******”

     

            private void Form3_Load(object sender, EventArgs e)

            {

                #region MyTable

                DataTable dt01 = new DataTable();

                dt01.Columns.Add("col01");

                dt01.Columns.Add("col02");

                dt01.Columns.Add("col03");

     

                dt01.Rows.Add("aaaaaaaaaaaa", "11", "a1");

                dt01.Rows.Add("bb", "22", "b2");

                dt01.Rows.Add("cc", "33", "c3");

                dt01.Rows.Add("dd", "44", "d4");

                #endregion

     

                // try change the datagridview current cell in different row and see the textbox text.

                dataGridView1.DataSource = dt01.DefaultView;

     

                textBox1.DataBindings.Add("text", dt01.DefaultView, "col01", true);

     

                // check the datatable

                dataGridView2.DataSource = dt01;

     

            }

     

    Best regards,

    Ling Wang


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by Jing0Moderator Thursday, January 28, 2010 2:57 AM
    Tuesday, January 26, 2010 8:04 AM