How to populate a form without using table adapter
I can do this easily with table adapter, but I can't seem to use variables in the table开发者_Go百科 adapters connection string, or specify the connection string to use before "filling". Is there a way to populate the form without using any sort of binding?
Currently i've used this method - a search form that populates a list box, and on the double click action i have this:
Private Sub lstResults_CellMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles lstResults.CellMouseDoubleClick
'Fills Main Form
Dim firstcellvalue As String = lstResults.Rows(e.RowIndex).Cells(0).Value.ToString()
frm_IMT.intPendingID = CInt(lstResults.Rows(e.RowIndex).Cells(0).Value.ToString())
frm_IMT.Show()
Then When the form loads:
Me.PendingTableAdapter.Fill(Me.Phone_memo_backendDataSet.Tables("Pending"), intPendingID)
Could you be a little more specific as to exactly what you are trying to acheive? When you say you can't seem to use variables to construct your connection string, do you mean, in order to connect to the back-end and retrieve data?
Since I am not certain what it is you are trying to do, I will make some guesses. It LOOKS like you are trying to populate a list of some sort (let's say a dataGridView) on one form.
You are then allowing the user to select an item in the dataGridview by double-clicking on a any cell within a row, which provides retrieves a Primary key ID for a record in a table containing pending phone memos.
This ID is then used to fill a TableAdapter with all records from the Table "Pending" which have the PendingID As either a Primary key (thereby returning a single record) or a foreign key (thus returning 0 to many records).
Since we don't know where your results are supposed to end up (Does the Table Adapter contain a single record, or potentially many?), it is difficult to answer your question.
Personally, I would consider writing a function which returns a DataTable containing the information you wish to populate your form with. In this example, the function returns a dataTable containing a record for each state, along with associated data. Assume this Function, and the next, are members of a Class Named "MyDataFactory":
'Shared Keyword makes this function Available without instantiating the containing class:
Public Shared Function StatesDataTable(ByVal StateID As Integer) As DataTable
'Set up your SQL Statement to execute against your back end:
Dim SQL As String = _
"SELECT StateID, State, StateName, HUDStateID " & _
"FROM tblState " & _
"ORDER BY State"
'Initialize a DataTable:
Dim dt As New DataTable
'The Using Keyword is elegant in that it handles bothersome Disposal tasks
'for unmanaged objects such as Database Connections:
Using cn As New OleDb.OleDbConnection(My.Settings.CreateTHisConnection)
Using cmd As New OleDb.OleDbCommand(SQL, cn)
'Open the Connection:
cn.Open()
'Retreive the Data into a DataReader:
Dim dr As OleDb.OleDbDataReader = cmd.ExecuteReader
'Load the contents of the Reader into your datatable:
dt.Load(dr)
'Clean Up:
dr.Close()
cn.Close()
End Using ' cmd
End Using ' cn
'Return the DataTable to calling code:
Return dt
End Function ' StatesDataTable
If I were to use this datatable to populate a list on my main form with the StateID (Tucked away as the Value member of a ListBox, for example) and the StateName (Displayed, as the DisplayMember of said ListBox). When the user clicked on a specific state in the listbox, one could retreive the StateID from the Valuemember property, and pass it as a parameter to another function, perhaps, which might return a dataTable of Counties related to that state:
'Shared Keyword makes this function Available without instantiating the containing class:
Public Shared Function CountiesDataTable(ByVal CountyID As Integer) As DataTable
'Set up your SQL Statement to execute against your back end:
Dim SQL As String = _
"SELECT CountyID, StateID, CountyName " & _
"FROM tblCounties " & _
"WHERE CountyID = @CountyID " & _
"ORDER BY CountyName"
'Initialize a DataTable:
Dim dt As New DataTable
'The Using Keyword is elegant in that it handles bothersome Disposal tasks
'for unmanaged objects such as Database Connections:
Using cn As New OleDb.OleDbConnection(My.Settings.CreateTHisConnection)
Using cmd As New OleDb.OleDbCommand(SQL, cn)
'Add a parameter which limits the result set to counties in the specified state:
cmd.Parameters.AddWithValue("@CountyID", CountyID)
'Open the Connection:
cn.Open()
'Retreive the Data into a DataReader:
Dim dr As OleDb.OleDbDataReader = cmd.ExecuteReader
'Load the contents of the Reader into your datatable:
dt.Load(dr)
'Clean Up:
dr.Close()
cn.Close()
End Using ' cmd
End Using ' cn
'Return the DataTable to calling code:
Return dt
End Function ' CountiesDataTable
One could then use the resulting output from this function to load into a pop-up form or some other mechanism to display the counties associated with the selected state.
Not sure if any of that is what you are after, but without a little more to go on, I did the best I could . . .
My Methodology here is a little unorthodox in ADO.NET land, but I like it because it allows me implicit control of WHAT I am pulling from the Back-end. Not that for many applications, globally available "Shared" functions like this will come in handy - In order to retreive a datatable of States (or Counties) "on demand", I simply do the following:
Dim dtStates As DataTable = MyDataFactory.StatesDataTable
Dim dtCounties As DataTable = MyDataFactory.Counties(ByVal StateID As Integer)
Anyway, hoipe that helps. Feel free to follow up if you can either clarify what you are trying to do, or find this intereesting, and want more info.
精彩评论