[ Pobierz całość w formacie PDF ]
.Don t confuse clicking the button on the form with populating the dataset for use with a Windows form.Filling the Categories data table in theDsCategories data set with data values from a SQL Server instance and displayingthe values on a Windows form requires two m ore steps.First you need to invokethe Fill m ethod for the SqlDataAdapter object.Second you need to bind formcontrols, such as text boxes, to colum ns in the local Categories data table.Filling a Data Set and Binding Controls to I tA logical place to fill a data set for use with a form is the form Load eventprocedure.A single line of code in the tem plate will fill the data set.Run the lineof code from the form Load event to m ake the contents for the data set availableas soon as the form opens.The following code segm ent illustrates the syntax forinvoking the SqlDataAdapter Fill m ethod to populate a data set.The eventprocedure is for Form 1, which is the default startup object for a Windowsapplication.The Fill m ethod takes two argum ents in this situation.First youspecify the data set nam e.Second you designate the DataTable nam e within thedata set.You m ust nam e a DataTable object because one data set can holdm ultiple DataTable obj ects.Leaving out the DataTable nam e will cause an error.Private Sub Form1_Load(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles MyBase.LoadSqlDataAdapter1.Fill(DsCategories1,  Categories )End SubAfter you fill the data set, you can bind it to controls on a form.For exam ple, Iadded two text boxes to Form 1 for the starter ADO.NET application.You can dothis with the Toolbox j ust as in prior Visual Basic versions.What s new is thatthere is now a DataBindings property.You can graphically bind the Text propertyfor a text box control to a colum n in the Categories data table.Figure 1-7 showshow to bind the Text property for TextBox1 to the CategoryI D colum n in theCategories data table.The Form 1.vb Design tab shows TextBox1 selected onForm 1.The Properties window reveals the assignm ent of the CategoryI D colum nto TextBox1.Selecting a colum n from the Categories data table com pletes thetask.I followed the sam e process for TextBox2, but I selected CategoryNam einstead.I f you run Form 1 by pressing the F5 key, you see the form with two text boxesshowing the CategoryI D and CategoryNam e colum n values for the first row fromthe Categories data table.While it is nice to see data in the text boxes,applications typically seek to allow users to at least browse through data.To enable browsing, you need controls that let a user navigate through the rows ofthe Categories data table.Figure 1 - 7.Use the DataBindings property to bind the Text property of atext box control to a colum n in a DataTable object.Navigating Through Row sA row of button controls can provide the basis for a navigation bar.All we needare Text property settings indicating the navigation each button provides andevent procedures for the Click event of each button that navigates through therows in the Categories data table.I added four button controls to Form 1 withevent procedures to control navigation in response to click events.For exam ple,Figure 1-8 shows the text boxes after the button control on the far right has beenclicked.Notice that the last row (for CategoryI D 8 in the Categories data table)shows in the top text box.Figure 1 - 8.Form 1 in the starter ADO.NET sam ple after the last- rowbutton ( > | ) has been clicked displays colum n values from thecorresponding row in its text box controls. The following set of Click event procedures for Button1 through Button4 showshow easy it is to control navigation.The buttons from left to right navigate to thefirst row, the previous row, the next row, and the last row.The proceduresupdate the Position property of the BindingContext property on the form for theCategories DataTable in the DsCategories1 data set.This m anipulation, in turn,affects all text box controls bound to the Categories data table.Chapter 10 drillsdown m ore deeply into the object m odel supporting these m anipulations.Theim portant point to notice here is that the code doesn t have to handle m ovingpast the beginning or ending row because ADO.NET is sm art about recognizingeither end of a rowset, such as the Categories data table.Private Sub Button1_Click(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles Button1.Click Move to the first row.Me.BindingContext(DsCategories1,  Categories ).Position _= Me.BindingContext(DsCategories1,  Categories )._Position.MinValueEnd SubPrivate Sub Button2_Click(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles Button2.Click Move to the previous row.Me.BindingContext(DsCategories1,  Categories ).Position _-= 1End SubPrivate Sub Button3_Click(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles Button3 [ Pobierz całość w formacie PDF ]
  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • agnieszka90.opx.pl