I'm having all manner of problems with a DataGridView

cjard

Well-known member
Joined
Apr 25, 2006
Messages
7,081
Programming Experience
10+
My overall goal is:


To have a form dedicated to editing a datatable of type NaughtyDataTable
The form has one DataGirdView on it
The datatable is a boribg table with only text box columns, except for one
The one column is a COmbobox type. the combo values will be looked up from an already existing/populated instance of a data table called StatusLookupTable in a module called Universe (Universe.StatusLookupTable)

I dont want to burden the form with the creation of a whole new MyDataSet (that holds the NaughtyDataTable) nor the binding source, table adapter etc.. its not necessary in this case
Because the designer cannot know at design time, the table structure of NaughtyDataTable, the DataGridView doesnt have any columns in
I put the columns in at runtime (i cheated on this one, i linked the datagrid to the data source, then copied the auto generated code, then removed the link again. I now have the generated code in my Form Load)
After the init code (actually before, and this is causing me another problem i know how to resolve) i push in the datatable and watch it go bang :)

The errors come when i close the form and then try to use it again. All sorts of nasties pop up, such as "BeginInit alrteady called before an EndInit called" or "Operation is not valid because it results in a re-entrant call to SetCurrentCellAddress"


Can ayone suggest a better way of doing what i'm doing? Constraints are that i do not want useless instances of datasets, bindingsources, tableadapters etc on the form. at most i'll want a table adapter to write back the changes to the database that this form will make to the datatable. i'll do this in code

Should i set up the columns colelction manually in the designer, and set the datasource, displaymember and value member of the combo box column at runtime?
Should i bang the datatable in there and let it auto generate the columns then change the type of the combobox column? hmm.. Any help would be appreciated

VB.NET:
    Private Sub frmDDPCorrection_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 
 
        CType(Me.CorrectionsGridView, System.ComponentModel.ISupportInitialize).BeginInit()
 
        Me.SuspendLayout()
        '
        'DataGridView1
        '
        CorrectionsGridView.AllowUserToAddRows = False
        CorrectionsGridView.AllowUserToDeleteRows = False
        CorrectionsGridView.AutoGenerateColumns = False
        CorrectionsGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize
        CorrectionsGridView.Columns.AddRange(New System.Windows.Forms.DataGridViewColumn() {PAYMENTREFDataGridViewTextBoxColumn,_
 BACSREFDataGridViewTextBoxColumn, AMOUNTDataGridViewTextBoxColumn, EFFECTIVEDATEDataGridViewTextBoxColumn, _
PAYSTATUSIDDataGridViewComboBoxColumn, FILEREFDataGridViewTextBoxColumn})
 
 
 
 
        '
        'PAYMENTREFDataGridViewTextBoxColumn
        '
        Me.PAYMENTREFDataGridViewTextBoxColumn.DataPropertyName = "PAYMENT_REF"
        Me.PAYMENTREFDataGridViewTextBoxColumn.HeaderText = "PAYMENT_REF"
        Me.PAYMENTREFDataGridViewTextBoxColumn.Name = "PAYMENTREFDataGridViewTextBoxColumn"
        '
        'BACSREFDataGridViewTextBoxColumn
        '
        Me.BACSREFDataGridViewTextBoxColumn.DataPropertyName = "BACS_REF"
        Me.BACSREFDataGridViewTextBoxColumn.HeaderText = "BACS_REF"
        Me.BACSREFDataGridViewTextBoxColumn.Name = "BACSREFDataGridViewTextBoxColumn"
        '
        'AMOUNTDataGridViewTextBoxColumn
        '
        Me.AMOUNTDataGridViewTextBoxColumn.DataPropertyName = "AMOUNT"
        Me.AMOUNTDataGridViewTextBoxColumn.HeaderText = "AMOUNT"
        Me.AMOUNTDataGridViewTextBoxColumn.Name = "AMOUNTDataGridViewTextBoxColumn"
        '
        'EFFECTIVEDATEDataGridViewTextBoxColumn
        '
        Me.EFFECTIVEDATEDataGridViewTextBoxColumn.DataPropertyName = "EFFECTIVE_DATE"
        Me.EFFECTIVEDATEDataGridViewTextBoxColumn.HeaderText = "EFFECTIVE_DATE"
        Me.EFFECTIVEDATEDataGridViewTextBoxColumn.Name = "EFFECTIVEDATEDataGridViewTextBoxColumn"
 
 
        'THIS IS THE LOOKUP datatable 
        'PAYSTATUSIDDataGridViewComboBoxColumn
        '
        Me.PAYSTATUSIDDataGridViewComboBoxColumn.DataPropertyName = "PAY_STATUS_ID"
        Me.PAYSTATUSIDDataGridViewComboBoxColumn.HeaderText = "PAY_STATUS_ID"
        Me.PAYSTATUSIDDataGridViewComboBoxColumn.Name = "PAYSTATUSIDDataGridViewTextBoxColumn"
        Me.PAYSTATUSIDDataGridViewComboBoxColumn.DataSource = Universe.PopulatedLookupDS.DDH_LUT_PAY_STATUS
        Me.PAYSTATUSIDDataGridViewComboBoxColumn.DisplayMember = "PAY_STATUS_DESC"
        Me.PAYSTATUSIDDataGridViewComboBoxColumn.ValueMember = "PAY_STATUS_ID"
 
 
 
 
 
        '
        'FILEREFDataGridViewTextBoxColumn
        '
        Me.FILEREFDataGridViewTextBoxColumn.DataPropertyName = "FILE_REF"
        Me.FILEREFDataGridViewTextBoxColumn.HeaderText = "FILE_REF"
        Me.FILEREFDataGridViewTextBoxColumn.Name = "FILEREFDataGridViewTextBoxColumn"
 
        CType(Me.CorrectionsGridView, System.ComponentModel.ISupportInitialize).EndInit()
 
        Me.ResumeLayout(False)
 
    End Sub
 
I solved this one, thought i'd share it...

Easiest way, in the end, was to define at design time, in the designer all the columns i wante dto use and their types. In code, I dropped a boolean isInited that starts out as false. after miking the following changes in code, i set it to true, ensuring one time init of the columns:

VB.NET:
        If Not dgviewIsInited Then
            CorrectionsGridView.AutoGenerateColumns = False

            Dim col As DataGridViewComboBoxColumn
            col = DirectCast(CorrectionsGridView.Columns("PayStatusID"), DataGridViewComboBoxColumn)
            col.DataSource = Universe.PopulatedLookupDS.DDH_LUT_PAY_STATUS
            col.DisplayMember = "PAY_STATUS_DESC"
            col.ValueMember = "PAY_STATUS_ID"
            dgviewIsInited = True
        End If

        CorrectionsGridView.DataSource = naughtyDT


i set autogen columns to false because sometimes i dont want all the columns of the dataset. if it is true and i only define 8 out of the 10 columns, the grid will generate suitable columns to show the other two blocks of data in the table.

Using this method i was able to program ahead opf time how the table would look, alignments and formats, and it has suited my purpose well. One thing to note is that if the table contains a value that is not present in the drop down list, you will get error messages often.
 
Back
Top