Generic Selection Form

Source: Internet
Author: User

To do a program down, in order to serve the good customer, the father, basically the loss of the hand is replaced by choice. Therefore, it is necessary to write a generic selection form.

As below, make a simple form.

There is no difficulty in the code below. Basic notes are also available.

    Public partial class Selectform:form {public string returnstr = "", Columnvalue = "", sql = "";        int columnindex;        DataTable dt = null;        <summary>/////</summary>/<param name= "SQL" >sql statements </param>             <param name= "Columnvalue" > the column name of the return value </param> public selectform (String sql, string columnvalue) { This.            Columnvalue = Columnvalue;            This.sql = SQL;        InitializeComponent (); }///<summary>//Page Load Event///</summary>//<param name= "Sender" ></pa        ram>//<param name= "E" ></param> private void Selectform_load (object sender, EventArgs e)        {Datagrid.celldoubleclick + = datagrid_celldoubleclick;//cells cell Double-click event DataBind (); }///<summary>//Data binding//</summary> private void DataBind () {           DT = getdatatable ();//bind database columnindex = DT According to SQL statement.            Columns.indexof (Columnvalue);        Datagrid.datasource = DT; }///<summary>///Based on SQL statements, return to DataTable///</summary>//<returns></ret urns> private DataTable getdatatable () {SqlConnectionStringBuilder sb = new Sqlconnectionstri            Ngbuilder (); Sb.            DataSource = "LIURT-PC"; Sb.            InitialCatalog = "MyDB"; Sb.            UserID = "sa"; Sb.            Password = "qaz123"; using (SqlConnection con = new SqlConnection (sb.)                ToString ())) {SqlDataAdapter da = new SqlDataAdapter (sql, con);                DataTable dt = new DataTable (); Da.                Fill (DT);            return DT;  }} private void Datagrid_celldoubleclick (object sender, DataGridViewCellEventArgs e) {if            (E.rowindex >-1) {returnstr = DatagriD.rows[e.rowindex]. Cells[columnindex].                Value.tostring (); This.            Close (); }} private void Txtvalue_textchanged (object sender, EventArgs e) {datarow[] drs = dt.            Select (Columnvalue + "like '%" + txtvalue.text + "% '"); if (Drs.            Length = = 0) {Datagrid.datasource = null; } else {Datagrid.datasource = Drs.            CopyToDataTable ();            }} private void Datagrid_cellformatting (object sender, Datagridviewcellformattingeventargs e) {             if (e.rowindex% 2 = 0) {E.cellstyle.backcolor = Color.greenyellow;}        else {e.cellstyle.backcolor = Color.mintcream;} }    }

The basic idea is this.

Generic Selection Form

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.