Private voidButton1_Click (Objectsender, EventArgs e)//here is the case without a generic collection, using a DataTable{SqlConnection conn=NewSqlConnection ("server=.; Database=cml;user=sa;pwd=123"); SqlCommand cmd=Conn. CreateCommand (); Cmd.commandtext="SELECT * FROM Student"; Conn. Open (); SqlDataReader Dr=cmd. ExecuteReader (); DataTable DT=NewDataTable (); if(Dr. HasRows) {//Start adding Columns for(inti =0; I < Dr. FieldCount; i++)//Dr. Number of columns in the FieldCount table{dt. Columns.Add (Dr. GetName (i)); //a column-by-column addition, for Loop loop is determined after a row for each column } //Start adding Rows while(Dr. Read ())//The while loop is the data for each row, a line of loops{DataRow DTW= dt. NewRow ();//Create a new line//start adding content to new lines below for(inti =0; I < Dr. FieldCount; i++) {Dtw[i]= Dr[i];//start assigning values to each column that determines a row, and the For loop once assigns a column value at a time } //Add the contents of each row after the while loop is complete to DTdt. Rows.Add (DTW); } } //now there is content in the DataTable, and then the DATAGRIDVIEW1 data source is DTDatagridview1.datasource =DT; Cmd. Dispose (); Conn. Close (); Copy the Code
--Drag the form
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Data.SqlClient;namespace_ June 11, 2015 wwz{ Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); } Private BOOLisOK//to distinguish the mouse point down or release//Initial coordinates Private intChushix;//Initial Horizontal Private intChushiy;//Initial ordinate//the coordinates when the mouse points down Private intShubiaodianx;//Mouse point down the horizontal axis Private intShubiaodiany;//mouse dot down ordinate//coordinates in the move process Private intYIDONGX;//the horizontal axis when the mouse point moves down Private intYidongy;//the vertical axis when the mouse point moves down Private voidForm1_mousedown (Objectsender, MouseEventArgs e)//Mouse dot down{isOK=true; Shubiaodianx= cursor.position.x;//give the mouse a point down the horizontal axis assignmentShubiaodiany = CURSOR.POSITION.Y;//give the mouse point down ordinate assignment//assigning values to the initial coordinatesChushix = This. location.x; Chushiy= This. LOCATION.Y; } Private voidForm1_mouseup (Objectsender, MouseEventArgs e) {isOK=false; } Private voidForm1_mousemove (Objectsender, MouseEventArgs e) { if(isOK) {//assign a value to the coordinates when movingYIDONGX =cursor.position.x; Yidongy=CURSOR.POSITION.Y; //re-assign new coordinates to the form This. Location =NewPoint (chushix+yidongx-shubiaodianx,chushiy+yidongy-Shubiaodiany); } }
Winfrom (1) Form transfer value connection (2) mouse Move window