Data Grid View

How to fill-up a data grid view

Page 1 of 1

5 Replies - 8293 Views - Last Post: 30 March 2010 - 03:09 AM Rate Topic: -----

#1 edr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 27-July 09

Data Grid View

Posted 16 March 2010 - 02:48 PM

Hello.

I try to fill up a data grid view with records of two related tables. Beneath you can find the code I use to handle this.

The first table ‘GemeentenT’ has the following columns.

GemeenteId // Primary key
GemeentePostnummer
GemeenteNaam

The second table KlantenT’ .

KlantId
KlantNaam
KlantVoornaam
KlantAdres
GemeentePostnummerId // Foreign key
GemeenteNaam
Telefoon1



'TODO: This line of code loads data into the 'KlantenBeheertMandjeDataSet.GemeentenT' table. You can move, or remove it, as needed.
Me.GemeentenTTableAdapter.Fill(Me.KlantenBeheertMandjeDataSet.GemeentenT)
'TODO: This line of code loads data into the 'KlantenBeheertMandjeDataSet.KlantenT' table. You can move, or remove it, as needed.
Me.KlantenTTableAdapter.Fill(Me.KlantenBeheertMandjeDataSet.KlantenT)

Try
'Connection obj to database
conn.ConnectionString = BuildCnnStr("BGC-IPAUZLIX959\SQLEXPRESS", "tMandje", "True")

'Open the Connetion
conn.Open()

'Dataset that holds data in disconnected mode
Dim ds As New DataSet

'Two commands for two tables (tab1 and tab2)
Dim cmd1 As SqlClient.SqlCommand
Dim cmd2 As SqlClient.SqlCommand

'Two datapaters to fill the dataset from two tables
Dim adp1 As SqlClient.SqlDataAdapter
Dim adp2 As SqlClient.SqlDataAdapter

'This handles the relationship between the two columns
Dim datarelation As DataRelation
Dim dc1 As DataColumn
Dim dc2 As DataColumn

'It's not important but gives your code more better way to
'compare strings between tables
ds.CaseSensitive = True

'First command for first table
cmd1 = New SqlClient.SqlCommand
cmd1.Connection = conn
cmd1.CommandText = "SELECT * FROM GemeentenT"

'Second command for Second table
cmd2 = New SqlClient.SqlCommand
cmd2.Connection = conn
cmd2.CommandText = "SELECT * FROM KlantenT"

'Now , we will fill the first table and add it to the dataset
adp1 = New SqlClient.SqlDataAdapter
adp1.SelectCommand = cmd1
adp1.TableMappings.Add("Table", "GemeentenT")
adp1.Fill(ds)

'As we did in the previous step , here for the Second table
adp2 = New SqlClient.SqlDataAdapter
adp2.SelectCommand = cmd2
adp2.TableMappings.Add("Table", "KlantenT")
adp2.Fill(ds)

dc1 = ds.Tables("GemeentenT").Columns("Id")
dc2 = ds.Tables("KlantenT").Columns("id")

'Here we combined two datacolumns to the relations obj
datarelation = New DataRelation("GemeentenTandKlantenT", dc1, dc2)
ds.Relations.Add(datarelation)

'Simple one , bind the dataset after all operation to the
'Datagrid
KlantenDataGridView.DataSource = ds.DefaultViewManager
'Show the first table in the grid because it's the primary table
KlantenDataGridView.DataMember = "GemeentenT"


Catch ex As Exception
MessageBox.Show(ex.Message)
End Try.

Running the code results in the following error.

Column argument can’t be null.
Parameter name: column.

Can somebody please tell me what I do wrong.

Soory for my English.

Best regards.
Eddy.

Is This A Good Question/Topic? 0
  • +

Replies To: Data Grid View

#2 motcom  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,371
  • Joined: 16-June 08

Re: Data Grid View

Posted 17 March 2010 - 01:13 AM

Hi,

Right at the top you fill two tables in a dataset, one of them can be bound to a datagridview
(You do not need the rest of the code you have written, as this just repeats the whole thing)

'TODO: This line of code loads data into the 'KlantenBeheertMandjeDataSet.GemeentenT' table. You can move, or remove it, as needed.
Me.GemeentenTTableAdapter.Fill(Me.KlantenBeheertMandjeDataSet.GemeentenT)
'TODO: This line of code loads data into the 'KlantenBeheertMandjeDataSet.KlantenT' table. You can move, or remove it, as needed.
Me.KlantenTTableAdapter.Fill(Me.KlantenBeheertMandjeDataSet.KlantenT)



not both, so if you want to display the related records, you would have to change your querry with JOINS.

If you want a Master Detail type of Form, you would have to use 2 datagridviews and handle the click event of the "Master" to display data in the "Detail"...
Was This Post Helpful? 0
  • +
  • -

#3 muballitmitte  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 174
  • View blog
  • Posts: 470
  • Joined: 05-November 08

Re: Data Grid View

Posted 17 March 2010 - 06:36 AM

just try this (notice the use of "GemeentenT" in both the Fill function and DataMember). It should work fine if the rest of your code is correct.

Me.GemeentenTTableAdapter.Fill(Me.KlantenBeheertMandjeDataSet.GemeentenT,"GemeentenT")



KlantenDataGridView.DataSource = ds
KlantenDataGridView.DataMember = "GemeentenT"


This post has been edited by muballitmitte: 17 March 2010 - 06:38 AM

Was This Post Helpful? 0
  • +
  • -

#4 edr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 27-July 09

Re: Data Grid View

Posted 23 March 2010 - 03:33 AM

Hi,

thanks to all to assist me.

At this moment I’ve not found any solution to retrieve records, not all but a few records of each table into my Data Grid View. I tried all the suggestions you've provide me without any success.
I think i've made a mix of difference faults in my code so I'll try this night all over again to find out what's really the problem.

In attach I've put two screen shots.
The first one contains my two related tables and th good working select statement.
The second screen shot gives an idea of the structure of my data grid view

Thanks a lot.
Edr.
Was This Post Helpful? 0
  • +
  • -

#5 edr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 27-July 09

Re: Data Grid View

Posted 30 March 2010 - 02:49 AM

View Postedr, on 23 March 2010 - 02:33 AM, said:

Hi,

I wasn't be able to upload two attached screen shots.

Edr.

This post has been edited by edr: 30 March 2010 - 03:23 AM

Was This Post Helpful? 0
  • +
  • -

#6 edr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 27-July 09

Re: Data Grid View

Posted 30 March 2010 - 03:09 AM

Hi,

Finally I've found an almost working solution.
There's still one problem. The last column in my datagrid view has the same value of the first column.

Code.
Try
 'Connection obj to database
 conn.ConnectionString = BuildCnnStr("BGC-IPAUZLIX959\SQLEXPRESS", "tMandje", "True")

            'Open the Connetion 
            conn.Open()

            'SqlConnection conn = new SqlConnection(connectionString);
            Dim cmdString As String = "SELECT * FROM KlantenT"
            Dim dataAdapter As New SqlClient.SqlDataAdapter(cmdString, conn)
            Dim ds As New DataSet()
            dataAdapter.Fill(ds, "KlantenT")
            cmdString = "SELECT * FROM GemeentenT"
            dataAdapter = New SqlClient.SqlDataAdapter(cmdString, conn)
            dataAdapter.Fill(ds, "GemeentenT")
            Dim relation As New DataRelation("KlantenGemeenten", _
                        ds.Tables("GemeentenT").Columns("GemeenteId"), _
                        ds.Tables("KlantenT").Columns("GemeentePostnummerId") _
                        )
            ds.Relations.Add(relation)

            Dim dv As New DataView(ds.Tables("KlantenT"))
            KlantenDataGridView.DataSource = dv

       Catch ex As Exception
            MessageBox.Show(ex.Message)
       End Try


Result in my DataGrid View after running the code.

KlantId | KlantNaam | KlantVoornaam | KlantAdres | GemeenteNaam
17 | Goosens | Eric | Boskant 2 | 17

KlantId and GemeentNaam has the same value


I wasn't be able to upload two screen shots.
Beneath you can find the structure of my two related tables.

This is my child Table.
[KlantId]
[KlantNaam]
[KlantVoornaam]
[KlantAdres]
[GemeentePostnummerId] // Foreign key
[KlantTelefoon1]
[Klanttelefoon2]
[KlantGsm]
[KlantFax]
[KlantEmailAdres]
[KlantMailing]
[KlantBtwNummer]
[KlantBankrekening]
[KlantStartDatum]
[KlantFactuur]
[KlantInfo]

This is my master Table.
GemeenteId]
[GemeentePostnummer] / Primary key
[GemeenteNaam]
[GemeenteNaamHoofdletters]

I'll build the folowing DataGrid View with 5 columns.

KlantId | KlantNaam | KlantVoornaam | KlantAdres | GemeenteNaam.

The last problem I have is column 5 of the DataGridView, [GemeenteNaam].
I'm not be able to fill-in column 5 of the DGV with the correct value from table 'GemeentenT' column 3.

Best regards.
Edr.

This post has been edited by edr: 30 March 2010 - 06:06 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1