Question Issue with run-time control binding - bindingNavigator, gridControl

jchoponis

Member
Joined
May 15, 2011
Messages
8
Programming Experience
1-3
Using a devExpress gridControl & gridView (don't think devExpress is to blame here, but i just mention it for accuracy), combined with a BindingNavigator and BindingSource.

I have portion of a WinForms app that has several "admin" forms that each show a table - i.e. stuff that users can fill out so their listboxes and such in other parts of the app have proper values.

I wanted to do away with about 10 or 12 forms, so I thought I would just create a class of "AdminFormTemplate" or similar and bind a dataAdapter at runtime using different queries for differennt instances of the class. This way, I get to manage 1 form, but use several separate class instances during run-time.

Only issue I see right now is related to creating the first record in a blank table. It seems that when I create the first record, a "ghost" of the intial add is left over, producing an output of 2 records of the same characteristics (one with an ID of "0", the other with the proper ID). Only the correct record gets written to the DB, so I believe the database if fine, but it seems my bindingSource/dataAdapater is goofed up after the first add in a blank table. Subsequent add seem fine.

Here's my code from the class - feel free to comment.

Public Class frmAdmin_Template
Private strColumnThatHasDefVal As String 'if set to something, column name will have default value of 0 (false) - only need to do for bit fields, expecting 1 at most per table
Private objDa As New SqlDataAdapter()
Private objDt As New DataTable
Private objBindSource As New BindingSource() 'used for record manipulation and writing back to data source
Public Sub New(ByVal strQueryToRun As String, ByVal strColThatHasDefVal As String, ByVal strFormTag As String)
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Me.Tag = strFormTag
strColumnThatHasDefVal = strColThatHasDefVal
Dim sqlCmd As New SqlCommand
sqlCmd.Connection = mod_m_sqlConnIDB
sqlCmd.CommandText = strQueryToRun
objDa.SelectCommand = sqlCmd
Dim objCmdBuilder As SqlCommandBuilder = New SqlCommandBuilder(objDa)
End Sub
Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Fill_DataSet()
End Sub
Private Sub Fill_DataSet()
Try
objBindSource.DataSource = Nothing
objDa.FillSchema(objDt, SchemaType.Source) 'get schema info - doesn't seem to grab default values though!
objDa.Fill(objDt) 'fill the data table with our data adapter
If strColumnThatHasDefVal IsNot Nothing Then
'if set to something, make the default value be false - signifies we have a bit field in this admin table form
objDt.Columns(strColumnThatHasDefVal).DefaultValue = CType(False, Boolean)
End If
objBindSource.DataSource = objDt 'match up the binding source to the data table
TblTemplateBindingNavigator.BindingSource = objBindSource 'match up the navigator with binding source
gridControl_Template.DataSource = objBindSource 'bind the gridcontrol to data source
Catch ex As Exception
mod_Show_Error_Message(ex, Reflection.MethodBase.GetCurrentMethod.Name)
End Try
End Sub
Private Sub Update_Db()
Try
Me.Validate()
Me.objBindSource.EndEdit()
objDa.Update(objDt) 'send stuff back to actual db!
Catch ex As Exception
mod_Show_Error_Message(ex, Reflection.MethodBase.GetCurrentMethod.Name)
Cancel_Db_Edit()
End Try
End Sub
Private Sub Cancel_Db_Edit()
objBindSource.CancelEdit()
End Sub
Private Sub BindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorSaveItem.Click
Update_Db()
End Sub
Private Sub BindingNavigatorRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorRefresh.Click
Fill_DataSet()
End Sub
Private Sub BindingNavigatorCancelEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorCancelEdit.Click
Cancel_Db_Edit()
Fill_DataSet()
End Sub
Private Sub BindingNavigatorAddNewItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorAddNewItem.Click
objBindSource.AddNew()
End Sub
End Class
 
BTW - this "objBindSource.DataSource = Nothing" line was an attempt at resolving the mentioned issue.

I have also tried going back and forth between using my own .AddNew() event and the built-in one - happens either way.
 
got it!

i found i needed to clear the data table before refilling it.

i added the following at the beginning of the Fill_DataSet() method
objDt.Clear()



 

Latest posts

Back
Top