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