-
Nov 22nd, 2022, 07:02 PM
#1
Thread Starter
Hyperactive Member
DataSource
I am currently tyring to implement the DatagridViewAutoFilter, and have been successful in getting it working when the data is read directly form the database in the same piece of code. However prior to this on a different form (which shows this form) the Data is read into a dataset and table, which can then be edited. As such when I display the DataGridView with Auto filter, I want it to use the data from the dataset / datatable that was previously populated (and possibly edited, but not saved). I am using typed datasets
The inital code that works(but reads the data from the database) is :-
Code:
Dim con As String = "Data Source=xxx\SQLEXPRESS;Initial Catalog=TestDatabase;Integrated Security=True"
Dim dbConnection As New SqlConnection(con)
Dim strSQL As String = "SELECT * FROM TestTable"
Dim cmd As New SqlCommand(strSQL, dbConnection)
Dim da As New SqlDataAdapter(cmd)
Dim TestDatabaseDataset As New DataSet()
Try
da.Fill(TestDatabaseDataset, "TestTable")
Catch sqlExc As Exception
MessageBox.Show(sqlExc.ToString, "SQL Exception Error!",
MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End Try
BindingSource1.DataSource = TestDatabaseDataset.Tables("TestTable")
Me.DataGridViewCutPaste1.DataSource = BindingSource1
Me.DataGridViewCutPaste1.BindingSource = BindingSource1
#Region "Columns defined"
Me.DataGridViewCutPaste1.Columns.Clear()
Me.SurnameTextBoxColumn = New DataGridViewAutoFilter.DataGridViewAutoFilterTextBoxColumn()
'crImportRecNoTextBoxColumn = New DataGridViewAutoFilter.DataGridViewAutoFilterTextBoxColumn()
Me.DataGridViewCutPaste1.Columns.AddRange(New System.Windows.Forms.DataGridViewColumn() {Me.SurnameTextBoxColumn})
Me.SurnameTextBoxColumn.DataPropertyName = "Surname"
Me.SurnameTextBoxColumn.HeaderText = "Surname"
Me.SurnameTextBoxColumn.Name = "SurnameDataGridViewTextBoxColumn"
Me.SurnameTextBoxColumn.ReadOnly = True
Me.SurnameTextBoxColumn.Resizable = System.Windows.Forms.DataGridViewTriState.[True]
Me.SurnameTextBoxColumn.Width = 100
'Me.crImportRecNoTextBoxColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
Me.SurnameTextBoxColumn.FilteringEnabled = True
etc for next cloumn....
Now when I convert this to read the previously poulated dataset and datatable (and remove the SQL read code previously used), it generates an error.
Code:
BindingSource1.DataSource = TestDatabaseDataset.Tables("TestTable") <---------ERROR
Me.DataGridViewCutPaste1.DataSource = BindingSource1
Me.DataGridViewCutPaste1.BindingSource = BindingSource1
#Region "Columns defined"
Me.DataGridViewCutPaste1.Columns.Clear()
Me.SurnameTextBoxColumn = New DataGridViewAutoFilter.DataGridViewAutoFilterTextBoxColumn()
'crImportRecNoTextBoxColumn = New DataGridViewAutoFilter.DataGridViewAutoFilterTextBoxColumn()
Me.DataGridViewCutPaste1.Columns.AddRange(New System.Windows.Forms.DataGridViewColumn() {Me.SurnameTextBoxColumn})
Me.SurnameTextBoxColumn.DataPropertyName = "Surname"
Me.SurnameTextBoxColumn.HeaderText = "Surname"
Me.SurnameTextBoxColumn.Name = "SurnameDataGridViewTextBoxColumn"
Me.SurnameTextBoxColumn.ReadOnly = True
Me.SurnameTextBoxColumn.Resizable = System.Windows.Forms.DataGridViewTriState.[True]
Me.SurnameTextBoxColumn.Width = 100
'Me.crImportRecNoTextBoxColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
Me.SurnameTextBoxColumn.FilteringEnabled = True
The error being that reference to a none shared member requires an object reference or
Public read only property tables as datatable collection has no parameters and its return type can not be indexed.
To me this looks like its because its a typed dataset (but am not sure).
So i have changed the offending lines to :-
Code:
BindingSource1.DataSource = TestDatabaseDataSet.TestTableDataTable <----ERROR
Me.DataGridViewCutPaste1.DataSource = BindingSource1
Me.DataGridViewCutPaste1.BindingSource = BindingSource1
This however generates an error :- TestDatabaseDataSet.TestTableDataTable is a class type and can not be used as an expression.
Now this seems strange, as I thought TestDatabaseDataSet.TestTableDataTable actually referred to the datatable that i had populated earlier.
Any help in correcting this would be appreciated.
-
Nov 22nd, 2022, 08:20 PM
#2
Re: DataSource
Originally Posted by Signalman
I am using typed datasets
So you say, but there's no typed DataSets in that first code snippet.
Do you know the difference between a type and an object? It's a fairly important distinction in OOP. In the first code snippet, you are using the DataSet type and you are creating an object of that type here:
vb.net Code:
Dim TestDatabaseDataset As New DataSet()
Here in the first code snippet:
vb.net Code:
BindingSource1.DataSource = TestDatabaseDataset.Tables("TestTable")
TestDatabaseDataset is a variable of type DataSet and you are getting the object assigned to that variable.
Where are you doing the equivalent in the other code snippets? In those two cases, TestDatabaseDataset is a type. Nowhere do you declare a variable of that type, create an object of that type and assign it to the variable. You can't use a type where an object is required. If you went to the supermarket, would you bring your shopping home in bag or would you bring it home in A bag? You know what an object is in the real world and you know that you have to create objects in order to user them. OOP is the same, given that it was designed to mimic the way real-world objects work.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|