DataGridView Update Error

Hi guys. I'm new at your forum but I really need your help!

I'm working with DataGridView and put Update() method of my TableAdapter in the RowLeave event of my DGV. So when I add a new row it works perfectly but when i try to update some other row created before it gives me an error like this (translated from italian):

Update requires a valid UpdateCommand when a DataRow collection is passed through this method

I just use this construction:

this.ticketsTableAdapter.Update(this.customersDBDataSet.Tickets);

Can someone help me Maybe somebody knows another WORKING method to update DataGridView content. Thanks a lot. Looking forward to your suggestions :)



Answer this question

DataGridView Update Error

  • Go4More

    Hi,

    DataAdapter.Update method calls its Updatecommand, so you must implement the DataAdapter.UpdateCommand before using Update method.

    Assuming you meant sqldataadapter, see: http://msdn2.microsoft.com/en-us/library/system.data.sqlclient.sqldataadapter.aspx

    Thanks



  • ahmedilyas

    Hi and thanks again! I just solved my problem. At last i decided to try to add the Primary Key and now it works perfectly! But now i have another LITTLE problem. I have to change BackgroundColor of some cells of DGV CheckBoxColumns (depending on its state - Checked or not). Firstly i had to change the color only to the cells of the only one column using this procedure:

    if (e.ColumnIndex > -1)//i added this because sometimes it gave me ColumnIndex=-1 and i still don't know why
    {
    if (TicketsDataGridView.Columns[e.ColumnIndex].Name == statoTicketDataGridViewCheckBoxColumn.Name)
    {
    if (e.Value != null)
    {
    if (e.RowIndex < TicketsDataGridView.Rows.Count - 1)//except the last row - the new one
    {
    if ((bool)e.Value == false)
    {
    e.CellStyle.BackColor = Color.Red;
    }
    }
    }
    }

    And till here it worked pretty good but then i had to add the same functionality also to another column and so i have just duplicated my code changing only column name:

    if (TicketsDataGridView.Columns[e.ColumnIndex].Name == esitoDataGridViewCheckBoxColumn.Name)
    {
    if (e.Value != null)
    {
    if (e.RowIndex < TicketsDataGridView.Rows.Count - 1)
    {
    if ((bool)e.Value == false)//CAST ERROR HERE - e.Value seems to be null
    {
    e.CellStyle.BackColor = Color.PapayaWhip;
    }
    else
    {
    e.CellStyle.BackColor = Color.LightGreen;
    }
    }
    }
    }
    else if (e.ColumnIndex > -1)
    {
    if (TicketsDataGridView.Columns[e.ColumnIndex].Name == statoTicketDataGridViewCheckBoxColumn.Name)
    {
    if (e.Value != null)
    {
    if (e.RowIndex < TicketsDataGridView.Rows.Count - 1)
    {
    if ((bool)e.Value == false)
    {
    e.CellStyle.BackColor = Color.Red;
    }
    }
    }
    }
    }

    And now when i add a new row(so my new row becomes in edit mode and it's no more the last one) it gives me an error (in red) and i don't know what to do. It's strange because the other fit of code still works. Some suggestions Please!:)Greetings from Italy!


  • sofakng

    Figo Fei - MSFT wrote:

    What type of e what event you use

    Thanks

    CellFormatting event of my DataGridView. Thnx.


  • Veo

    Hi,

    Sorry for the delay replying.

    Some code seem to be redundant.

    If you want to change the color of the checked cell.

    Here is sample code:

    private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
            {
                if (dataGridView1.Columns[e.ColumnIndex].Name != "CheckBoxColumn")
                    return;
                if (e.Value != null)
                    if ((bool)e.Value)
                        e.CellStyle.BackColor = Color.Red;
            }

    And for more information about cell formatting event, see: http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagridview.cellformatting.aspx

    Thank you



  • JSR2005

    CellFormatting event of my DataGridView. Thnx.
  • Jiajia

    Hi. first of all thnx for your attention. I found the "why" of my problem. The table of DB that i use has no PrimaryKey! It's really stupid,isn't it I just have no important relation between the two tables i use so i put only one PrimaryKey in the first table and not in both of them. But the "paradox" is that when i insert a new record it works perfectly, only after some time i saw that my TableAdapter hadn't generated any autocode with all commands(update,delete etc.) My question is the following one: am i obliged to add a PrimaryKey if yes then i must REDO ALL the things because VS2005 express doesn't update automatically changes made in the source DB (in my case is Access 2003 db).Some suggestions Please!Thnx.
  • Woody_In_Sheffield

    What type of e what event you use

    Thanks



  • Hearty81

    CellFormatting event of my DataGridView. Thnx
  • Yuvaraj

    Hi,

    Since you miss the PK in the database, if the connection didnt change and the catalog didnt change, there are not "much work" to do.

    What is your problem

    Thanks.



  • DataGridView Update Error