简体   繁体   English

从数据表(类似于like子句)中搜索值,而无需使用数据库

[英]search values from datatable (similer to like clause ) without using database

 public partial class WebForm4 : System.Web.UI.Page
   {
     protected void Page_Load(object sender, EventArgs e)
      {
        if (!(IsPostBack))
        {
            Session["List"] = null ;
        }
      }
     public static DataTable dt;
     public static int i = 1;
     // static int i = 1;
     //static List<string> lst; 
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Session["List"] = dt;
        dt = new DataTable();
        DataRow dr = null;
      //  dt.Columns.Add(new DataColumn("RowNumber", typeof(int)));
        dt.Columns.Add(new DataColumn("Column1", typeof(string)));
      //  dt.Columns.Add(new DataColumn("Column2", typeof(string)));
      if (Session["List"] == null)
      {
          dr = dt.NewRow();
          dr["Column1"] = txtAdd.Text;
          dt.Rows.Add(dr);
      //    lst.Add(txtAdd.Text);

      }
      else if  (Session["List"] != null)
      {
          dt = (DataTable)Session["List"];
          dr = dt.NewRow();
          dr["Column1"] = txtAdd.Text;
          dt.Rows.Add(dr);
          //lst = (List<string>)Session["List"];
          //lst.Insert(i,txtAdd.Text);
          //i++;

      }
    }


    protected void btnNew_Click(object sender, EventArgs e)
    {
        List<string> lst = new List<string>();
        foreach (DataRow row in dt.Rows)
        {
            foreach (DataColumn col in dt.Columns)
            {
                lst.Add(row[col].ToString());

            }
        }
        for (int j = 0; j >= dt.Rows.Count; j++)
        {              
        }
        bool isfound = lst.Contains(txtSearch.Text);
        Response.Write(Convert.ToString(isfound));
        dlshow.DataSource = dt; // i want to bind only matching values to datalist 
        dlshow.DataBind();
    }
}

i have two textboxes one to add and other is to search i want to bind only those matching values matches with search textbox ( Like ' Like ' clause ) i am not using any data base 我有两个文本框,一个要添加,另一个是搜索,我只想绑定与搜索文本框匹配的那些匹配值(例如“ Like”子句),我不使用任何数据库

   //just this will do 

   protected void btnAdd_Click(object sender, EventArgs e)
    {
      List <string > lst = (List<string >)Session["List"];          
      lst.Add(txtAdd.Text);
      // Change the list in session with new list
      Session["List"] = lst;
    }

Here the logic is wrong like you r testing if the session["list"] is null then u want to do the operation but if session is null then lst would also be null 这里的逻辑是错误的,就像您要测试session [“ list”]是否为null则要执行该操作一样,但是如果session为null则lst也将为null

 List <string > lst = (List<string >)Session["List"];
 lst.Add(txtAdd.Text);
DataTable dt2 = dt.Select("Column1 Like '%" + txtSearch.Text + "%'").CopyToDataTable();
dlshow.DataSource = dt2; // i want to bind only matching values to datalist 
dlshow.DataBind();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM