Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# DataGrid WinForm
I searched on google but I couldn't get the right answer for my project..
I create a form in which i want to Insert,update and delete the records through the dataGridView..

My code is:-
public partial class orderListDetails : Form
    {
        Data_Conn DC;
        string DelValue = string.Empty;
        public orderListDetails()
        {
            InitializeComponent();
            DC = new Data_Conn(Application.StartupPath);                       
        }
 
<pre>
    private void orderListDetails_Load(object sender, EventArgs e)
    {
        DC.SelectSta("select proName as Name,proComp as Company,proQty as Qty,proUnit as Unit from orderList", "orderList");
        dataGridView1.DataSource = DC.dataSet;
        dataGridView1.DataMember = "orderList";
 
    }
 
    private void button5_Click(object sender, EventArgs e)
    {
        this.Dispose();
    }
 
    private void btnAddNew_Click(object sender, EventArgs e)
    {
 
        DC.CmdStatement("insert into orderList values('" + textBox1.Text + "','" + comboBox1.Text + "'," + Convert.ToInt32(textBox2.Text) + ",'" + comboBox2.Text + "')");
 
        DC.SelectSta("select proName as Name,proComp as Company,proQty as Qty,proUnit as Unit from orderList", "orderList");
        dataGridView1.DataSource = DC.dataSet;
        dataGridView1.DataMember = "orderList";
        dataGridView1.Refresh();    
    }
 
    private void btnDelete_Click(object sender, EventArgs e)
    {
        try
        {
 
            DialogResult dr = MessageBox.Show("Do You want to delete the product details?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.Yes)
            {
                dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);
                DC.CmdStatement("delete from orderList where proName='"+DelValue+"'");                    
                DC.SelectSta("select proName as Name,proComp as Company,proQty as Qty,proUnit as Unit from orderList", "orderList");
                dataGridView1.DataSource = DC.dataSet;
                dataGridView1.DataMember = "orderList";                   
            }
        }
        catch
        { }
    }
    private void btnUpdate_Click(object sender, EventArgs e)
    {
        DC.adpt.Update(DC.dataSet, "orderList");
    }
 
    private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {                                                            
        DelValue = dataGridView1[0, e.RowIndex].Value.ToString();                          
    }
}</pre>

when i click on Update button Its shows the error message
Update requires a valid UpdateCommand when passed DataRow collection with modified rows.

How to Update that record in database??

thanks in Advance....
Posted 28-Mar-13 4:51am
Comments
ThePhantomUpvoter at 28-Mar-13 10:02am
   
You write an UPDATE statement....
richcb at 28-Mar-13 10:18am
   
You get my 5, I credited you for the solution to get it off the unanswered list.
Prasad Khandekar at 28-Mar-13 10:26am
   
My 5 as well.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

As ThePhantomUpvoter so eloquently put it, you must write an Update statement in order to update a database. Ther are countless examples of them online.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You need to use the OleDbCommandBuilder as described in this example[^].
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 490
1 Sergey Alexandrovich Kryukov 405
2 Maciej Los 285
3 ProgramFOX 265
4 Peter Leow 210
0 OriginalGriff 490
1 Sergey Alexandrovich Kryukov 395
2 Maciej Los 285
3 ProgramFOX 265
4 CHill60 200


Advertise | Privacy | Mobile
Web03 | 2.8.150331.1 | Last Updated 28 Mar 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100