Results 1 to 8 of 8

Thread: Rows cannot be programmatically added to the DataGridView's rows collection

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2008
    Posts
    103

    Rows cannot be programmatically added to the DataGridView's rows collection

    how to add Rows programmatically to the DataGridView's rows collection when the control is data-bound?


    here is my code but i got error as "Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound? "


    dtagrdScaleoffinance.Rows.Add(New Object() {Me.txtAccountNumber.Text, Me.txtAccountnumberScaleOfFinance.Text, Me.cmbxCropCode.SelectedIndex, (Me.mskdeffectivedate.Text)

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: Rows cannot be programmatically added to the DataGridView's rows collection

    add a row to your datasource + it'll appear in your dgv

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Nov 2008
    Posts
    103

    Re: Rows cannot be programmatically added to the DataGridView's rows collection

    sorry to ask u again how to add in datasource....


    please give me some sample code

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: Rows cannot be programmatically added to the DataGridView's rows collection

    can you post the code you use to bind your DataGridView?

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Nov 2008
    Posts
    103

    Re: Rows cannot be programmatically added to the DataGridView's rows collection

    Private Sub scaleoffinanceview()

    Dim dt As New DataTable

    dtagrdScaleoffinance.Visible = True

    PacsKccDetailsdatagridView.Visible = True

    Dim da As OdbcDataAdapter = New OdbcDataAdapter("select * from scaleoffinancedetails_mig where oldaccountnumber = '" & txtAccountNumber.Text & "' ", con)

    Dim ds As DataSet = New DataSet

    Try

    da.Fill(ds, "scaleoffinancedetails_mig")

    dt = ds.Tables(0)

    dtagrdScaleoffinance.DataSource = dt

    Catch ex As Exception

    MessageBox.Show(ex.Message)

    End Try

    End Sub


    thi is my code for databinding

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: Rows cannot be programmatically added to the DataGridView's rows collection

    ok. declare dt at form level:

    vb Code:
    1. Dim dt As New DataTable

    to add a new row:

    vb Code:
    1. dim dr as datarow = dt.newrow
    2. dr.itemarray = New Object() {Me.txtAccountNumber.Text, Me.txtAccountnumberScaleOfFinance.Text, Me.cmbxCropCode.SelectedIndex, Me.mskdeffectivedate.Text}
    3. dt.rows.add(dr)

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Nov 2008
    Posts
    103

    Re: Rows cannot be programmatically added to the DataGridView's rows collection

    thank you for ur reply.


    i tried your code but i am getting error as input array is longer than number of column in this table

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: Rows cannot be programmatically added to the DataGridView's rows collection

    how many columns does your datatable have?
    this is the line you need to change:

    vb Code:
    1. dr.itemarray = New Object() {Me.txtAccountNumber.Text, Me.txtAccountnumberScaleOfFinance.Text, Me.cmbxCropCode.SelectedIndex, Me.mskdeffectivedate.Text}

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width