Gridview
-
retrive data from sql database to a gridview, then how to use search option in a gridview ?
-
retrive data from sql database to a gridview, then how to use search option in a gridview ?
use this code it will help u private DataTable GetRecords() { conn.ConnectionString = "User ID=sa;Data Source=(local);Database=inventry"; //SqlConnection conn = new SqlConnection(strConnection); conn.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandType = CommandType.Text; cmd.CommandText = "Select ptype,pqty from total_stock"; SqlDataAdapter dAdapter = new SqlDataAdapter(); dAdapter.SelectCommand = cmd; DataSet objDs = new DataSet(); dAdapter.Fill(objDs); return objDs.Tables[0]; } private void BindGrid() { DataTable dt = GetRecords(); if (dt.Rows.Count > 0) { grdSearch.DataSource = dt; grdSearch.DataBind(); } } private void SearchText() { DataTable dt = GetRecords(); DataView dv = new DataView(dt); string SearchExpression = null; if (!String.IsNullOrEmpty(txtSearch.Text)) { SearchExpression = string.Format("{0} '%{1}%'", grdSearch.SortExpression, txtSearch.Text); } dv.RowFilter = "ptype like" + SearchExpression; grdSearch.DataSource = dv; grdSearch.DataBind(); } public string Highlight(string InputTxt) { string Search_Str = txtSearch.Text.ToString(); // Setup the regular expression and add the Or operator. Regex RegExp = new Regex(Search_Str.Replace(" ", "|").Trim(), RegexOptions.IgnoreCase); // Highlight keywords by calling the //delegate each time a keyword is found return RegExp.Replace(InputTxt, new MatchEvaluator(ReplaceKeyWords)); // Set the RegExp to null. RegExp = null; } public string ReplaceKeyWords(Match m) { return "<span class=highlight>" + m.Value + "</span>"; } protected void txtSearch_TextChanged(object sender, EventArgs e) { SearchText(); }
-
retrive data from sql database to a gridview, then how to use search option in a gridview ?
-
retrive data from sql database to a gridview, then how to use search option in a gridview ?