• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 404
  • Last Modified:

fill a datagrid that is displayed on a modal form,from dataset

good day i have a frmMain.vb and a
frmList.vb. The latter is a form with a datagrid called gdDetails.

now i have a button on frmMain with the follwing code:

 Dim frmListNow As New frmList 'instantiate  frmList

        frmListNow.Show() 'show it

        MyOLEDBConnection.Open() 'open my connection

        Dim cmdFillCustomers As New System.Data.OleDb.OleDbCommand("SELECT Customers.* FROM Customers",

MyOLEDBConnection)'fill my oledbcommand

        Dim rdFillCustomers As System.Data.OleDb.OleDbDataReader = cmdFillCustomers.ExecuteReader
'set the oledbdatareader


my plan is to press the button,
and display frmList
then fill formlist with all the data of the oledbcommand cmdFillCustomers.

1. how can i get the grid on frmList to be populated with the values of the Oledbdatareader: rdfillcustomers
should i make a dataadapter now, fill the dataset, then load the frmList
and how do i populate it from there?
maybe export the dataset to a xml file, then display the frmList , then gather the data from the xml file?
should i put all the code in frmList and just call FRMLIST from frmMain and then somehow call frmList.Fill from there?

2. how can i get frmlist to display modal?

thank you!

  • 3
  • 2
1 Solution
Hello.  This can be accomplished very easily:

1) Fill your DataSet in your frmMain
2) Create a Public property in your frmList that will accept your DataSet
3) Instantiate your frmList object
4) Pass the DataSet to the List form
5) Show the form modally by calling the ShowDialog method

see the code fragments below.

In your frmList, create a new Public property & corresponding private variable to accept your dataset object like this:

Private m_dstMyDataSet As DataSet     'Private member variable

Public Property MyDataSet As DataSet
       Return m_dstMyDataSet
   End Get
   Set(Value As DataSet)
       m_dstMyDataSet = Value
       ' Here you could bind the dataset to the grid:
       objGrid.DataSource = Value
       objGrid.DataBind()    ' <<<  You may or may not need this line depending on the grid you are using
   End Set
End Property

Now in frmMAIN's button event handler:

    MyDataSet = MyDataAdapter.Fill("SELECT * FROM MyTable", MyConnection)
   Dim frm As New frmList()     'Instantiate a List Form object
   frm.MyDataSet = MyDataSet

Argg...hit tab and enter and the form posted.   (Too used to code editors...)   These comments should be appended to my previous post.

Anyway, you use ShowDialog to get the form to appear modally.

On your List form, if you create a couple buttons, say OK and Cancel and set their DialogResult property to the same (OK and Cancel) then when the user clicks those buttons the form is automatically closed and the ShowDialog method returns the DialogResult you put in the button.  You can then use this to determine what the user wants to do:

If frm.ShowDialog = DialogResult.OK Then   ' The user clicked the OK button
    'Update the data from the grid
End If

Finally dispose of the list form:

jxhardingAuthor Commented:
hi ptakja, thanks the answer is awesome and i learned a lot,could you please just help me on 1 small section still
in my fmMain i only have the following code
Dim daCustomers as system.data.oledb.oledbdataadapter
dim dsCustomers as Dataset

dsCustomers = daCustomers.fill ("SELECT * FROM CUSTOMERS",myconnection)
now i get the message overload resolution failed because no accessible 'fill' can be called with these argument
Public function fill(dataset ........

obviously im trying here on my own, but hopefully you can help me
jxhardingAuthor Commented:
i sorted that one myself thanks!
only one question left now
the user now selects a row on the frmList and in this frmList row, i need to take the row , the user chose and put it on a control on my frmMain.
Customer Name
Customer Address
Customer Telephone Number
what control should i use and how difficult would it , or rather what method should i use  to pass the values from the chosen row into the control?
thank you!
Probably the easiest way to do what you want is to create several public properties (ie, CustomerName, CustomerAddress, CustomerPhone) in your frmList.  When the user clicks a row, copy the data from the row object to the properties.  Then when they close the modal form, you can query the properties prior to destroying the form:

IF frmList.ShowDialog = DialogResult.OK Then
    txtName.Text = frmList.CustomerName
    txtAddress.Text = frmList.CustomerAddress
    txtPhone.Text = frmList.CustomerPhone
End If


Thank help?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now