(C #) a project configuration and additions and deletions

Source: Internet
Author: User
Tags rtrim

I. Windows Forms project Environment configuration steps

1. New project, file,->windows-> modify file name/path, OK

2. Right-click to add Sqlhelper.cs and add Reference (. net): System.configuration3. Add New Item->app.config (Modify the database name, file name) 4. Add using in code System.Data.SqlClient Second, add the DataGridView, need add BindingSource (binding source)//read data from the database  string sql = "Select  patient_id , Patient_no,name,type,birth,id_no,dept,grade,major,glass,work,job,mobile,cornet,remark from patient ";                CommandType cmd = commandtype.text;          &NB Sp     String con = sqlhelper.connstring;                BindingSource1.Dat Asource = Sqlhelper.executereader (con, cmd, sql);                Dgvpatient.dataso Urce = BindingSource1; declares a variable to be the length of the study number/work number, which is used to determine if it conforms to the requirement int count1 = Txtpatient_no. Text.length;else if (count1 = = 5 | | count1==9)             {  MessageBox.Show ("Insert Success"); sh Ow_message ();    }else                   {                        MessageBox.Show ("School number/work number already exists, add failed ! "," Prompt ", MessageBoxButtons.OK, MessageBoxIcon.Information);                    Four, the Prompt box normative MessageBox.Show ("Update failed! "," hint ", MessageBoxButtons.OK, MessageBoxIcon.Information); Five, DataGridView get the data, note that the column name to bind the data source, That is, in DataGridView Edit column Settings datapropertyname the corresponding database field name private void CreateColumns ()         {                            dgvPatient.Columns.Clear (); &nbsp ;                           Int32 collenth = 10;                          ,         &NB Sp        //number                 datagridviewtextboxcolumn[] Column s = new Datagridviewtextboxcolumn[collenth];                 for (int j = 0; J < Collenth; J + +) &nbsp ;                   COLUMNS[J] = new Datagridviewtextboxcolumn ();   & nbsp             Columns[0]. HeaderText = "id";                Columns[0]. DataPropertyName = "patient_id";                 COLUMNS[1]. HeaderText = "study number/work number";              //Column name                 COLUMNS[1]. DataPropertyName = "Patient_no";//corresponding database field                  COLUMNS[2]. HeaderText = "name";                COLUMNS[2]. DataPropertyName = "name";                 COLUMNS[3]. HeaderText = "type";            &NBSP;   COLUMNS[3]. DataPropertyName = "type";                 COLUMNS[4]. HeaderText = "Date of birth";                COLUMNS[4]. DataPropertyName = "Birth";                 COLUMNS[5]. HeaderText = "Social Security Number";                COLUMNS[5]. DataPropertyName = "Id_no";                 COLUMNS[6]. HeaderText = "department";                COLUMNS[6]. DataPropertyName = "Dept";                 COLUMNS[7]. HeaderText = "Grade";                COLUMNS[7]. DataPropertyName = "Grade";                 COLUMNS[8]. HeaderText = "Professional";                COLUMNS[8]. DataPropertyName = "Major";                 COLUMNS[9]. HeaderText = "Class";                COLUMNS[9]. DataPropertyName = "Glass";                 for (int i = 0; i < collenth; i++)                     DGVPATIENT.COLUMNS.ADD (Columns[i]);             } Six, click on one line, data jumps to the above edit, that is DataGridView data jump to text box edit         Private Voi d Dgvpatient_cellcontentclick (object sender, DataGridViewCellEventArgs e)         {    & nbsp       try            {                if (Dgvpatient.rows[e.rowindex]. Cells! = null)                 {              &N Bsp     Txtpatient_no. Text = Convert.ToString (Dgvpatient.rows[e.rowindex]. CELLS[1]. Value);  &NBsp                 txtName.Text = convert.tostring (Dgvpatient.rows[e.rowindex]. CELLS[2]. Value);                    Txttype.text = convert.tostring (dgvpatient.ro Ws[e.rowindex]. CELLS[3]. Value);                    Txtbirth.text = convert.tostring (DGVPATIENT.R Ows[e.rowindex]. CELLS[4]. Value);                    Txtid_no. Text = Convert.ToString (Dgvpatient.rows[e.rowindex]. CELLS[5]. Value);                    Txtdept.text = convert.tostring (dgvpatient.ro Ws[e.rowindex]. CELLS[6]. Value);                    Txtgrade.text = convert.tostring (DGVPATIENT.R Ows[e.rowindex]. CELLS[7]. Value);                    Txtmajor.text = convert.tostring (DGVPATIENT.R Ows[e.rowindex]. CELLS[8]. Value);                    Txtglass.text = convert.tostring (DGVPATIENT.R Ows[e.rowindex]. CELLS[9]. Value);                   //txtpatient_no. Enabled = false;//Cannot modify this text box                }          &NB Sp }            catch (Exception ee)             {    &N Bsp           MessageBox.Show ("Operation failed:" + ee);           }       }        Seven, Judge text box cannot be empty if (txtpatient_no. Text = = "" | | Txtpatient_no. Text = = String. Empty)             {                MessageBox.Show (" Please enter the study number/work number! "," Prompt ", MessageBoxButtons.OK, MessageBoxIcon.Information);                Txtpat Ient_no. FOCUs ();           }            else if (txtName.Text = = "" | | txtName.Text = = String. Empty)             {                MessageBox.Show (" Please enter a name! "," Prompt ", MessageBoxButtons.OK, MessageBoxIcon.Information);                Txtnam E.focus ();           }  VIII, the call to the stored procedure, which is a lookup store private void Search_click (object sender, EventArgs e)         {            try          & nbsp {                String strSQL = "Patient_get";                CommandType cmdtype = commandtype.storedprocedure;              &N Bsp String connstring = sqlhelper.connstring;                sqlparameter[] Paras = {//Parameters                 New SqlParameter ("@patient_no", SqlDbType.VarChar, ,                New SqlParameter ("@name", SqlDbType.VarChar,,                New SqlParameter ("@type", sqldbtype.varchar,20),        &NB Sp       New SqlParameter ("@dept", sqldbtype.varchar,30),              &NBS P New SqlParameter ("@grade", sqldbtype.varchar,30),                New SqlParameter ("@major", sqldbtype.varchar,30),                New SqlParameter ("@glass", SqlDb type.varchar,30                               and nbsp    };                Paras[0]. Value = Txtpatient_no. text;//Get parameters       &nbsp         PARAS[1]. Value = txtname.text;                PARAS[2]. Value = txttype.text;                PARAS[3]. Value = txtdept.text;                PARAS[4]. Value = txtgrade.text;                PARAS[5]. Value = txtmajor.text;                PARAS[6]. Value = txtglass.text;                 SqlDataReader reader = Sqlhelper.execu Tereader (connstring, Cmdtype, strSQL, paras);                Bindingsource1.datas Ource = reader;                Dgvpatient.datasource = bindingsource1;  &nbsp ;                                 creat Ecolumns ();      &NBSP                       //CREATEBTN ();    &nbsp ;             int obj3 = Sqlhelper.executenonquery (connstring, Cmdtype, strSQL, paras);  & nbsp               if (obj3! = 1)                 {&N bsp;                    MessageBox.Show ("Find success!");                }                ELSE&NBSP ;               {                   &NB Sp MessageBox.Show ("Does not exist, find failed!", "hint", MessageBoxButtons.OK, MessageBoxIcon.Information);                }           }            C Atch (Exception ee)       &nbsp     {                MessageBox.Show ("Operation failed:" + ee);           }                   } ix. set default private void Form 1_load (object sender, EventArgs e)         {            show_message () &n Bsp           Txtheight.text = "0";//default is 0            Txtweight.text = " 0.0 "///default = 0.0       } Ten, empty button-trigger event         private void Clear_click (object sender, E Ventargs e)         {            Txtpatient_no. Text = "";            txtName.Text = "";            Txttype.te XT = ";            Txtbirth.text =" ";            Txtid_no. Text = "";       } xi., database: Fuzzy Lookup stored procedure alter PROCEDURE [dbo]. [Patient_get] (@patient_no varchar (), @name varchar (), @type varchar, @dept varchar, @grade varchar (30), @major varchar , @glass varchar (asbeginset)) NOCOUNT on; SELECT Patient_id,patient_no,name,type,birth,id_no,dept,grade,major,glass,work,job,mobile,cornet,remark from Patient where patient_no like '% ' + rtrim (@patient_no) + '% ' and name like '% ' + rtrim (@name) + '% ' and type like '% ' + RTrim (@type) + '% ' and dept like '% ' + rtrim (@dept) + '% ' and grade like '% ' + rtrim (@grade) + '% ' and major like '% ' + RTrim (@major) + '% '   and glass like '% ' + rtrim (@glass) + '% '   end 12, 1.DataGridView attribute anchor settings up or down, At this point, the maximum adaptive size of 2.selectionMode is set to Fullrowselect to select the full line 3.RowTemplate set to DataGridViewRow {index=-1}, Setting Cellclick to the current DataGridViewRow trigger event in an event indicates that the message will be displayed in the text box at any point in a row 4. Setting the dock to fill in the SplitContainer property is adaptive when you maximize it

(C #) a project configuration and additions and deletions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.