As a person who just contact ASP.NET, DataGrid can provide us with those convenience, always I want to know early. These two days I happened to modify the previous ASP app, so I learned to learn DataGrid, I realized it, I really brought a lot of convenience, summed up, write it out to give a beginner like me. .
Part 1: Display all data in the data table with DataGrid
One purpose we use DataGrid is to use it to display data. Let's first put a DataGrid on the page, such as DataGrid1, next, we want to define a global database link on the page, you can create a "SQLConnection" from the VS.Net2003 toolbox, such as "MyConn", When the system will be regenerated into WebForm, you can initialize it, you can also initialize it in the page_load event:
Private Void Page_Load (Object Sender, System.EventArgs E)
{
IF (! ispostback)
{
THISMYCONN.CONNECTIONSTRING = "Workstation ID = TEST; packet size = 4096; integrated security = SSPI; data source = a" "PPServer; persist security info = false; initial catalog = oa";
}
// For convenience, I created a function to populate data to DataGrid
Bindgrid ();
}
Create a function bindgrid ();
Public void bindgrid ()
{
// Create an adapter
SqldataAdapter myadp = new sqldataadapter ("Select * from myTable", myconn;
// Create a dataset
DataSet DS = New Dataset ();
/ / Then populate the data in the database to the data set by the adapter
myadp.fill (DS, "MyTable"); // myTable is the name of the database table
// Use the data set as the data source of DataGrid
DataGrid1.datasource = DS.TABLES ["MyTable"]. DefaultView;
// Bind data, the data is displayed
DataGrid1.databind ();
}
OK, it is displayed!
The program is really much less than the original ASP, and it is so much fast.