I am using vb.net. In my homework, I need to have:
1. a combo box with a list of predefined queries, with a button when clicked to retrieve data defined by that query
2. A text box where user can put in his own query, add this query to the combo box list, and be able to retrieve data defined by user's query
I got most part done, but there are some problems:
1. when I run the predefined query, if it has keys linked to other tables, it shows those columns from other table in the data grid with null values for each field. How do I just make it show only the table columns, not the linked table columns?
2. when I write a new query in the text box, and add it to the combo box list, it is not saved to the list, it is only good for that run session. If I close the app, and reopen it, the new query is not in the combo box list. How do save it to the combo list?
Here is what I have so far:
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
'clear dataset
BooksDataSet1.Clear()
End Sub
Private Sub btnAddQuery_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddQuery.Click
'add new query to combo box list and clear the text box
cboQuery.Items.Add(txtAddQuery.Text)
txtAddQuery.Text = ""
End Sub
Private Sub btnQuery_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQuery.Click
BooksDataSet1.Clear()
'run each predefined query
Select Case cboQuery.SelectedIndex
Case Is = 0
BooksDataSet1.Clear()
OleDbDataAdapter1.Fill(BooksDataSet1, "Authors")
dgdResults.SetDataBinding(BooksDataSet1, "Authors")
Case Is = 1
BooksDataSet1.Clear()
OleDbDataAdapter2.Fill(BooksDataSet1, "Publishers")
dgdResults.SetDataBinding(BooksDataSet1, "Publishers")
Case Is = 2
BooksDataSet1.Clear()
OleDbDataAdapter3.Fill(BooksDataSet1, "Authors")
dgdResults.SetDataBinding(BooksDataSet1, "Authors")
Case Is = 3
BooksDataSet1.Clear()
OleDbDataAdapter4.Fill(BooksDataSet1, "Publishers")
dgdResults.SetDataBinding(BooksDataSet1, "Publishers")
Case Else
'run user defined query
OleDbDataAdapter5.SelectCommand.CommandText = txtAddQuery.Text
BooksDataSet1.Clear()
OleDbDataAdapter5.Fill(BooksDataSet1, txtAddQuery.Text)
dgdResults.SetDataBinding(BooksDataSet1, txtAddQuery.Text)
End Select
End Sub
Thank you everyone! I have attached a picture of the form for question 1.
First Question:
Two ways:
1-If you have set your datagrid datasource to the desired table that sometimes is linked to other tables and dont want to add custom DatagridTablestyles and .... , just try to hide those columns by setting their width to Zero.
2-Define custom DatagridTableStyles and so on
Second Question:
Where the queries come from? A file, an XML file, a database,..? Save the new query to that place. Unless you say that you have hard coded the combobx items.
'Heading for the automatic overload'
Marillion, Brave, The Great Escape, 1994
'How will WE stand the FIRE TOMORROW?'
Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979
The predefined queries are just:
Select * from authors
Select * from publisher
I am not sure how to customize using datagrid.tablestyles, since the number of columns of tables (authors and publishers) are different, and user defined query could be different too. Somehow in the dataset schema, it has those linking columns from other tables, when I deleted them, the data returned did not show the linking columns. I am not sure if that was the problem.
Combo box question:
A user is supposed to be able to add/run his own query, eg. select * from authors where lastname ="smith". The user is querying an Access database. or is this not supposed to happen with a combo box? I want the combo box has a list of predefined query as well as used added query.