locked
Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound. RRS feed

  • Question

  • I am trying to add data from one table to DataGridView binded to another table.

     

    string sql = "select a.itemcd from cnitem as a,itemmaster as b,comaster as c where a.itemcd=b.itemcd and b.cocd=c.cocd ";

    ds = MainModule.gQuery.PopulateData(sql);

    for(int i=0;i<ds.Tables[0].Rows.Count; i++)

    {

    this.coDnItemBindingSource.AllowNew = true;

    dgvCoDnItems.Rows.Add();

    dgvCoDnItems.Rows[i ].Cells[srNoDataGridViewTextBoxColumn.Index].Value = i + 1;

    dgvCoDnItems.Rows[i ].Cells[transNoDataGridViewTextBoxColumn.Index].Value = lblTransNo.Text;

    dgvCoDnItems.Rows[i ].Cells[itemCdDataGridViewTextBoxColumn.Index].Value = ds.Tables[0].RowsIdea["ItemCd"];

    }

     

    Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound.

     

     

    System.InvalidOperationException was unhandled
      Message="Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound."
      Source="System.Windows.Forms"
      StackTrace:
           at System.Windows.Forms.DataGridViewRowCollection.Add()
           at FA.frmCoDrNote.GenerateDebitNoteButton_Click(Object sender, EventArgs e)
           at System.Windows.Forms.Control.OnClick(EventArgs e)
           at System.Windows.Forms.Button.OnClick(EventArgs e)
           at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
           at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
           at System.Windows.Forms.Control.WndProc(Message& m)
           at System.Windows.Forms.ButtonBase.WndProc(Message& m)
           at System.Windows.Forms.Button.WndProc(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
           at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
           at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
           at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
           at System.Windows.Forms.Application.Run(Form mainForm)
           at FA.StartUp.Main()
           at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
           at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
           at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           at System.Threading.ThreadHelper.ThreadStart()

    Monday, July 9, 2007 12:31 PM

Answers

  • Hi rajthegreat

     

    The following code is an exmaple which explains how to add a new row to the DataGridView binds to a DataTable.

     

    Code Snippet

               

                // DataGridView will generate a new row automatically

                SampleDataSet.CustomersRow newCustomersRow = sampleDataSet.Customers.NewCustomersRow();

                newCustomersRow.CustomerID = "1234-123";

                newCustomersRow.CompanyName = "Sample Works";

                sampleDataSet.Customers.Rows.Add(newCustomersRow);

     

    Regards

    Wei Zhou

    Wednesday, July 11, 2007 5:37 AM

All replies

  • If the grid is connected with datatable you can add in datatable. that will be displayed in grid. It works this way.

     

    venp--

    Monday, July 9, 2007 12:54 PM
  • Actually I think, I am not clear!

     

    I am using Typed DataSet.

     

    For retrive records I am using

    this.coDnItemTableAdapter.FillByTransNo(this.pharmaFADataSet.CoDnItem, lblTransNo.Text);

     

    For Updating records

    this.coDnItemTableAdapter.Update(pharmaFADataSet.CoDnItem);

     

     

    Other refrence:

     

    this.dgvCoDnItems.DataSource = this.coDnItemBindingSource;

    //

    // coDnItemBindingSource

    //

    this.coDnItemBindingSource.DataMember = "CoDnItem";

    this.coDnItemBindingSource.DataSource = this.pharmaFADataSet;

    //

    // pharmaFADataSet

    //

    this.pharmaFADataSet.DataSetName = "PharmaFADataSet";

    this.pharmaFADataSet.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema;

     

    So in which DataTable I must add rows.

    Tuesday, July 10, 2007 11:26 AM
  • Hi rajthegreat

     

    The following code is an exmaple which explains how to add a new row to the DataGridView binds to a DataTable.

     

    Code Snippet

               

                // DataGridView will generate a new row automatically

                SampleDataSet.CustomersRow newCustomersRow = sampleDataSet.Customers.NewCustomersRow();

                newCustomersRow.CustomerID = "1234-123";

                newCustomersRow.CompanyName = "Sample Works";

                sampleDataSet.Customers.Rows.Add(newCustomersRow);

     

    Regards

    Wei Zhou

    Wednesday, July 11, 2007 5:37 AM
  •  

    Thank you Wei Zhou

     

    Your code is exactly what i want

     

    Once again thank you

    Wednesday, July 11, 2007 10:21 AM
  •  

    Hello Wei Zhou......

     

    your post was extremely helpfull...!!! thanks a MILLION !!!!

     

    Pradeep

    Monday, October 13, 2008 7:43 AM
  • Has anyone tried this?  It does not work or is not correctly syntaxed.

    Who okayed this as an answer by the way?
    Thursday, December 24, 2009 5:20 AM
  • Do you have a specific problem? Post some code so someone can help.
    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Friday, December 25, 2009 5:35 PM
  •       // DataGridView will generate a new row automatically

                SampleDataSet.CustomersRow newCustomersRow = sampleDataSet.Customers.NewCustomersRow();

                newCustomersRow.CustomerID = "1234-123";

                newCustomersRow.CompanyName = "Sample Works";

                sampleDataSet.Customers.Rows.Add(newCustomersRow);



    Sorry I just realize the above code was in C and not VB which generated an error combined with the fact that everyone was saying it works great.  Using the parameters in the DataSet I came up with this for VB

            Dim newCustomersRow As SampleDataSet.CustomersRow
            newCustomersRow = SampleDataSet.Customers.NewCustomersRow
            newCustomersRow.CustomerID = "1234"
            newCustomerRow.CompanyName = "Sample Works"
            SampeDataSet.Customers.Rows.Add(newCustomersRow)



        


    Saturday, December 26, 2009 4:29 PM
  • So, are you saying the error you were getting  before when you posted was because you were trying to use C# code in VB? Or are you saying that the new VB code you came up with is still not working? Sorry, it's just not clear from the way you stated it. =0(
    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Monday, December 28, 2009 9:19 PM
  • While searching for a VB solution to add rows to a data-bound datagridview, the above solution came up.  Since it didn't work I was forced to come up with a VB version.  I've posted the Vb version should anyone need it.

    Thanks
     
    Wednesday, December 30, 2009 3:59 AM
  • I am having the same problem. following code you have given seems to be a solution to add rows programatically to a bounded datagridview eventhoug it's little bit difficult to understand. Could you please explain the VB.CODE more undertandable way.

     // DataGridView will generate a new row automatically

                SampleDataSet.CustomersRow newCustomersRow = sampleDataSet.Customers.NewCustomersRow();

                newCustomersRow.CustomerID = "1234-123";

                newCustomersRow.CompanyName = "Sample Works";

                sampleDataSet.Customers.Rows.Add(newCustomersRow);



    Sorry I just realize the above code was in C and not VB which generated an error combined with the fact that everyone was saying it works great.  Using the parameters in the DataSet I came up with this for VB

            Dim newCustomersRow As SampleDataSet.CustomersRow
            newCustomersRow = SampleDataSet.Customers.NewCustomersRow
            newCustomersRow.CustomerID = "1234"
            newCustomerRow.CompanyName = "Sample Works"
            SampeDataSet.Customers.Rows.Add(newCustomersRow)

    Monday, July 30, 2012 10:15 AM
  • Does the VB code work for you or are you just having problems understanding the code? All it is doing is creating a new CustomersRow, setting the values of a few columns in that row, then adding the new row to the Customers DataTable in the SampleDataSet. If your datagridview is bound to the DataTable (the grid's DataSource equals either the DataTable or a BindingSource whose DataSource is the DataTable), then the added row should show up in your grid.

    The only thing I can think of that might be causing you confusion is if you're not using a Typed DataSet. The syntax given in both the C# and VB versions of the code assume a Typed DataSet and so the syntax for access the Customers DataTable is different than if you're not using a Typed DataSet. Without a Typed DataSet, the VB code would be as follows:

    Dim newCustomersRow As DataRow
    newCustomersRow = SampleDataSet.Tables("Customers").NewRow
    newCustomersRow.Items("CustomerID") = "1234"
    newCustomerRow.Items("CompanyName") = "Sample Works"
    SampeDataSet.Tables("Customers").Rows.Add(newCustomersRow)


    ~~Bonnie Berent DeWitt [C# MVP]

    geek-goddess-bonnie.blogspot.com

    Monday, July 30, 2012 3:28 PM
  • Thanks for your readable code. Any way It does not seem to be working. It seems need something like refresh the DGV but does not work for me. Any other solution or improvements to the code if any.

    Thanks

    Monday, July 30, 2012 5:24 PM
  • Hi BonnieB

    I tried the code and noted it works (Add to the table).Only thing it does not display in the grid. Grid DataSource is a Binding Source.

    Any suggestion please.

    Monday, July 30, 2012 6:11 PM
  • Hi BonnieB

    The problem was sorted out I had a foreign key in my table and after i gave value programatically the records were reflected.

    Thank you very much for your readable code.

    wasantha

    Tuesday, July 31, 2012 3:20 AM
  • Hi Wasantha,

    I'm happy to help ... and I'm glad you got it figured out!  =0)


    ~~Bonnie Berent DeWitt [C# MVP]

    geek-goddess-bonnie.blogspot.com

    Thursday, August 2, 2012 4:11 PM
  • Hello Wei Zhou,

    Your solution doesn't work for c#, any new suggestions ?

    Thanks in advance

    Hamoudy

    Tuesday, July 23, 2019 10:36 AM
  • Hi Hamoudy,

    "You solution doesn't work" doesn't really explain what problem you're having with it. Can you make it a little clearer what problem you're having, or, better yet, start a new thread with your question/problem? If you start a new thread, please post a link to it on this thread.


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Sunday, August 18, 2019 2:08 PM