Link to home
Start Free TrialLog in
Avatar of jsctechy
jsctechyFlag for United States of America

asked on

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

hi there,
i got this error when i execute this code:
 Private Sub frmErrorAcct_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'ErrorAcct._ErrorAcct' table. You can move, or remove it, as needed.
        Dim str As String() = {Nothing, "Hello", "Test"}

        Me.ErrorAcctDataGridView.Columns.Add("REP_P", "P/L")
        Me.ErrorAcctDataGridView.Rows.Add(str)
        Me.ErrorAcctTableAdapter.Fill(Me.ErrorAcct._ErrorAcct)

    End Sub

Any ideas how to fix this
Avatar of tward
tward

You would need to add the row to the Recordset via the .AddNew Method.

recordset.Addnew FieldList, Values

That should inturn add it to the DataGrid....
Avatar of jsctechy

ASKER

can you be a little be more specific?

recordset.addNew willnot work by itself? can you show me how with the code i have provide on my original question?
Thanks
Fill your DataGridView first:

    Me.ErrorAcctDataGridView.Columns.Add("REP_P", "P/L")
    Me.ErrorAcctTableAdapter.Fill(Me.ErrorAcct._ErrorAcct)
    ErrorAcctDataGridView.DataSourece = ErrorAcct._ErrorAcct
    Dim str As String() = {Nothing, "Hello", "Test"}
    ErrorAcct._ErrorAcct.Add(str)
    ErrorAcct._ErrorAcct.EndEdit
how do i add a total at the bottom of the column?

lets said i have 4 columns i need a total on column 3 how do i do it without getting this error?
1. Read data from database and fill System.Data.DataTable
2. Add one boolean column to the newly created DataTable (say colIsSummary)   that column might be set to true for the last, summary, row
3. Programmatically add one extra row to datatable that contains suitable summary data
4. Set BindingSource.DataSource propert to this DataTable, set sorting by last column to have summary row always on bottom.
4. Do the data binding to DataGridView control (DataGridView.DataSourse=YourBindingSource)
5. Using appropriate event (Button click, Row/CellValidate etc) , bold or otherwise graphically distinct summary row (row that have extra column value set to true)

PS Dont forget remove extra column and row from DataTable before saving
those 5 steps looks really simple but i only have this amount of code on my form:

    Private Sub ErrorAcctBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ErrorAcctBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.ErrorAcctBindingSource.EndEdit()
        Me.ErrorAcctTableAdapter.Update(Me.ErrorAcct._ErrorAcct)

    End Sub

    Private Sub frmErrorAcct_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Me.Validate()
        Me.ErrorAcctBindingSource.EndEdit()
        Me.ErrorAcctTableAdapter.Update(Me.ErrorAcct._ErrorAcct)

        ModMain.DisplayfrmBackGround()
    End Sub

    Private Sub frmErrorAcct_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'code to store values of the clipboard
        Me.ErrorAcctDataGridView.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithoutHeaderText

        'TODO: This line of code loads data into the 'ErrorAcct._ErrorAcct' table. You can move, or remove it, as needed.
        Me.ErrorAcctTableAdapter.Fill(Me.ErrorAcct._ErrorAcct)

    End Sub

how do i do all that that you have just tell me??

=/
ASKER CERTIFIED SOLUTION
Avatar of Ark
Ark
Flag of Russian Federation image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
got an error here  Me.ErrorAcctBindingSource.Sorting
 sorting is not a menber of system.windows.forms.bindingsource
and if i do comment that line out i dont get any summary anyways =/
anything on this or its abandoned?