If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. I have a datagridview (bound to a DataTable) that has a combobox column (bound to bindingsource). Inserting data is not a problem. The problem occurs when I edit a row the datagridview throws an exception "datagridviewcomboboxcell value is not valid". The DGV combobox turns blank afterwards.
I posted this code to the DGV DataError Event to find the exact error:
Code:
        If (e.Context = DataGridViewDataErrorContexts.Commit) Then
            MessageBox.Show("Commit error")
        End If
        If (e.Context = DataGridViewDataErrorContexts.CurrentCellChange) Then
            MessageBox.Show("Cell change")
        End If
        If (e.Context = DataGridViewDataErrorContexts.Parsing) Then
            MessageBox.Show("parsing error")
        End If
        If (e.Context = DataGridViewDataErrorContexts.LeaveControl) Then
            MessageBox.Show("leave control error")
        End If
        If (TypeOf (e.Exception) Is ConstraintException) Then
            Dim view As DataGridView = CType(sender, DataGridView)
            view.Rows(e.RowIndex).ErrorText = "an error"
            view.Rows(e.RowIndex).Cells(e.ColumnIndex).ErrorText = "an error"
            e.ThrowException = False
        End If
I got this error "Error happened formatting, Display" and after I click ok I get this error "Error happened formatting, preferred Size".
I'm stuck. Can't figure out what I am doing wrong? Yes the user simply go to any cell and edit the values. My DGV is bound to a datatable. To save the data is use sqladapter.update(table). Inserting and deleting data is not a problem. The editing part is causing the errors.
The code I use for DGV insert, update, delete I got it from this link http://www.vbforums.com/showthread.p...ses&highlight=
my DGV has two columns: SLID (bigint), Amount (money)
BTW although I get errors the update command still executed succefully and saved the edited rows. I just can't seem to figure out what throws the exception? Sorry for the late reply. Yes I have checked the size of the fields in my tables. Again it is weird my sqladapter commands are working. Insert, delete and update commands are successfully executed. The database is ok. The problem is after the execution of the update command the datagridview throws the said error.
In my program I have an add button and save button. By default my DGV is readonly.
Add button code:
Code:
    Private Sub cmdAddDetails_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAddDetails.Click
        If dgvDisbursementDetails.RowCount = 0 Then
            isAddDetails = True
            isAddDetails = False
        End If
        If Me.txtDisbursementID.Text <> "" Then
            dgvDisbursementDetails.ReadOnly = False
            dgvDisbursementDetails.AllowUserToAddRows = True
            dgvDisbursementDetails.AllowUserToDeleteRows = True
            dgvDisbursementDetails(4, dgvDisbursementDetails.NewRowIndex).Selected = True
            cmdSaveDetails.Enabled = True
            cmdAddDetails.Enabled = False
            cmdDeleteDetails.Enabled = False
        End If
    End Sub
My Save button Code: (DGV Error occurs only and after the update)
Code:
Private Sub cmdSaveDetails_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSaveDetails.Click
                    If isAddDetails = True Then
                        'Add mode
                        AddCredit()
                        'Add details
                        sqladapter.Update(dtable)
                        dgvDisbursementDetails.AllowUserToAddRows = False
                        dgvDisbursementDetails.AllowUserToDeleteRows = False
                        dgvDisbursementDetails.ReadOnly = True
                        dgvDisbursementDetails.RefreshEdit()
                        dgvDisbursementDetails.Refresh()
                        cmdSaveDetails.Enabled = False
                        cmdAddDetails.Enabled = True
                        cmdDeleteDetails.Enabled = True
                        sqladapter.Update(dtable)
                        'Edit mode
                        'Edit details
                        sqladapter.Update(dtable)
                        dgvDisbursementDetails.AllowUserToAddRows = False
                        dgvDisbursementDetails.AllowUserToDeleteRows = False
                        dgvDisbursementDetails.ReadOnly = True
                        dgvDisbursementDetails.RefreshEdit()
                        dgvDisbursementDetails.Refresh()
                        cmdSaveDetails.Enabled = False
                        cmdAddDetails.Enabled = True
                        cmdDeleteDetails.Enabled = True
                    End If
   End Sub
My code for insert, delete and update. (No sqlexception encountered)
Code:
    Private Sub InitializeDataAdapter(ByVal mDisbursementID As Long)
        Dim con As SqlConnection = New SqlConnection(CS)
            Dim delete As New SqlCommand("sproc_AccountingBooksDeleteByDisbursementID", con)
            delete.CommandType = CommandType.StoredProcedure
            delete.Parameters.Add("@AcctID", SqlDbType.BigInt, 8, "AcctID")
            sqladapter.DeleteCommand = delete
            Dim insert As New SqlCommand("sproc_AccountingBooksDebitInsert", con)
            insert.CommandType = CommandType.StoredProcedure
            insert.Parameters.Add("@DisbursementID", SqlDbType.BigInt, 8, "DisbursementID")
            insert.Parameters.Add("@CompanyID", SqlDbType.BigInt, 8, "CompanyID")
            insert.Parameters.Add("@DatePosted", SqlDbType.DateTime, 8, "DatePosted")
            insert.Parameters.Add("@SLID", SqlDbType.BigInt, 8, "SLID")
            insert.Parameters.Add("@Debit", SqlDbType.Money, 8, "Debit")
            insert.Parameters.Add("@AccountingPeriodID", SqlDbType.BigInt, 8, "AccountingPeriodID")
            insert.Parameters.Add("@UserID", SqlDbType.BigInt, 8, "UserID")
            sqladapter.InsertCommand = insert
            Dim update As New SqlCommand("sproc_AccountingBooksDebitUpdate", con)
            update.CommandType = CommandType.StoredProcedure
            update.Parameters.Add("@DisbursementID", SqlDbType.BigInt, 8, "DisbursementID")
            update.Parameters.Add("@CompanyID", SqlDbType.BigInt, 8, "CompanyID")
            update.Parameters.Add("@DatePosted", SqlDbType.DateTime, 8, "DatePosted")
            update.Parameters.Add("@SLID", SqlDbType.BigInt, 8, "SLID")
            update.Parameters.Add("@Debit", SqlDbType.Money, 8, "Debit")
            update.Parameters.Add("@AccountingPeriodID", SqlDbType.BigInt, 8, "AccountingPeriodID")
            update.Parameters.Add("@UserID", SqlDbType.BigInt, 8, "UserID")
            update.Parameters.Add("@Original_AcctID", SqlDbType.BigInt, 8, "AcctID")
            update.Parameters.Add("@Original_DisbursementID", SqlDbType.BigInt, 8, "DisbursementID")
            sqladapter.UpdateCommand = update
            sqladapter.MissingSchemaAction = MissingSchemaAction.AddWithKey
        Catch ex As Exception
            Throw ex
        Finally
            con.Close()
        End Try
    End Sub
I got it. Stupid of me. I found the error. It's in my update storedproc. I failed to put the original ID parameter.
Original SP:
Code:
USE [SimpleAcctg]
/****** Object:  StoredProcedure [dbo].[sproc_AccountingBooksDebitUpdate]    Script Date: 11/16/2013 23:09:05 ******/
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
ALTER PROCEDURE [dbo].[sproc_AccountingBooksDebitUpdate]
	@DisbursementID bigint,
	@CompanyID bigint,
	@DatePosted datetime,
	@SLID bigint,
	@Debit money,
    @AccountingPeriodID bigint,
	@UserID bigint,
	@Original_AcctID bigint,
	@Original_DisbursementID bigint
SET NOCOUNT OFF;
UPDATE [dbo].[tblAccountingBooks] 
[DisbursementID] = @DisbursementID, 
[CompanyID] = @CompanyID, 
[DatePosted] = @DatePosted, 
[SLID] = @SLID, 
[Debit] = @Debit,
[AccountingPeriodID] = @AccountingPeriodID, 
[UserID] = @UserID 
WHERE (([AcctID] = @Original_AcctID) 
AND ([DisbursementID] = @Original_DisbursementID));
SELECT AcctID, DisbursementID, CompanyID, DatePosted, SLID, Debit, AccountingPeriodID, UserID FROM tblAccountingBooks WHERE (DisbursementID = @DisbursementID)
Updated SP:
Code:
USE [SimpleAcctg]
/****** Object:  StoredProcedure [dbo].[sproc_AccountingBooksDebitUpdate]    Script Date: 11/16/2013 23:09:05 ******/
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
ALTER PROCEDURE [dbo].[sproc_AccountingBooksDebitUpdate]
	@DisbursementID bigint,
	@CompanyID bigint,
	@DatePosted datetime,
	@SLID bigint,
	@Debit money,
    @AccountingPeriodID bigint,
	@UserID bigint,
	@Original_AcctID bigint,
	@Original_DisbursementID bigint
SET NOCOUNT OFF;
UPDATE [dbo].[tblAccountingBooks] 
[DisbursementID] = @DisbursementID, 
[CompanyID] = @CompanyID, 
[DatePosted] = @DatePosted, 
[SLID] = @SLID, 
[Debit] = @Debit,
[AccountingPeriodID] = @AccountingPeriodID, 
[UserID] = @UserID 
WHERE (([AcctID] = @Original_AcctID) 
AND ([DisbursementID] = @Original_DisbursementID));
SELECT AcctID, DisbursementID, CompanyID, DatePosted, SLID, Debit, AccountingPeriodID, UserID FROM tblAccountingBooks WHERE (AcctID = @Original_AcctID) AND (DisbursementID = @DisbursementID)
Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.