.net学习总结(5)之Gridview控件,智能搜索

来源:互联网 发布:筑龙投标软件 编辑:程序博客网 时间:2024/05/01 09:15
 

17. 报错:“将截断字符串或二进制数据。语句已终止。”
    原因:(1)可能数据库的类型与程序的数据类型不一至。(2)可能数据表中数据长度过短。
18. girdview 分页PageIndexChanging事件问题--激发了未处理的事件"PageIndexChanging"
    修改方法:1) 添加OnPageIndexChanging事件,OnPageIndexChangin="GridView1_PageIndexChanging
          2)要cs页面中添加

  1. protected  void  GridView1_PageIndexChanging(object  sender,  GridViewPageEventArgs  e)
  2.             {
  3.                     GridView1.PageIndex  =  e.NewPageIndex;
  4.                     DataBind();
  5.             }

19. 如何在 girdview分页中得到总的记录数
    方法一、使用datatable取得数据源,然后用ds.Rows.Count便可以取得总的记录数。

  1. {  
  2.          DataTable ds = sqlcontrol.Table_jl(sql1);
  3.          //或者使用DataSet,
  4.          //DateSet ds=new DataSet();
  5.          //int count=ds.Table[0].Rows.Count;
  6.             int cout = ds.Rows.Count;
  7.             Label1.Text = "共有留言:" + cout.ToString() + "条";
  8.             han.DataSource = ds;
  9.             han.DataBind();
  10.         }

    方法二、网上的,不过没用过,先写上。在sqldatasource的selected事件中,取e.affectrow 值(select所影响的行数)。


20.智能搜索;

  1. private string jshan(string han, string ziduan)//第一个参数是搜索的关键字,第二个字段是数据表中的字段。
  2.     {
  3.         int lngLenKey = han.Length;//取得关键字的长度
  4.         string strNew1 = "";
  5.         string stRSTubKey;
  6.         const int lngSubKey = 3;
  7.         if (lngLenKey == 0)
  8.         {
  9.             strNew1 = "";
  10.         }
  11.         else
  12.         {
  13.             int i = 1;
  14.             while (i <= (lngLenKey - (lngSubKey - 1)))
  15.             {//把关键字分解成每两个字组成的词,分别按分解成的词搜索与之相匹配的。
  16.                 stRSTubKey = han.ToString().Substring(i, 2);
  17.                 strNew1 = strNew1 + " or " + ziduan + " like '%" + stRSTubKey + "%'";
  18.                 i = i + 1;
  19.             }
  20.         }
  21.         return strNew1;
  22.  
  23.  }
  24.  string sql1 = "Select * from [News] where ntitle like '%" + scontent + "%'" + jshan(scontent, "ntitle");
  25.  // 从News表中ntitle字段中搜素与关键字匹配的。

附加贴一个组合查询的方法:

  1.  //查询语句
  2.     public void sql()//
  3.     {
  4.         string lbjs = "全部数据";
  5.         sqlCedar = "select * from HCDetaile where 1=1 ";
  6.         if (SZH.Checked == true)//如果是组合查询
  7.         {
  8.             lbjs = "组合查询结果";
  9.             string Stime = cr.formatDate(Convert.ToDateTime(SBirthDayTime.Text)).ToString();
  10.             string Etime = cr.formatDate(Convert.ToDateTime(EBirthDayTime.Text)).ToString();
  11.             sqlCedar += " and (BrithTime between  '" + Stime + "' and '" + Etime + "') ";
  12.             if (Rsex.Text != "0")
  13.             {
  14.                 sqlCedar += " and UserSex='" + Rsex.Text + "'";
  15.                 lbjs += "--性别:" + Rsex.Text;
  16.             }
  17.             if (Marry.Text != "0")
  18.             {
  19.                 sqlCedar += " and Marry='" + Marry.Text + "'";
  20.                 lbjs += "--婚否:" + Marry.Text;
  21.             }
  22.             if (Distinction.Text != "0")
  23.                 sqlCedar += " and Distinction='" + Distinction.Text + "'";
  24.             if (Degree.Text != "0")
  25.                 sqlCedar += " and Degree='" + Degree.Text + "'";
  26.             if (userName2.Text != "")
  27.             {
  28.                 sqlCedar += " and ( UserName like '%" + userName2.Text + "%') ";
  29.                 lbjs += "--关键字:" + userName2.Text;
  30.             }
  31.         }
  32.         else
  33.         {
  34.             if (userName.Text.Trim() != "")
  35.             {
  36.                 sqlCedar += " and UserName like '%" + userName.Text + "%'";
  37.                 lbjs = "搜索的关键字:" + userName.Text;
  38.             }
  39.         }
  40.         lbjg.Text = lbjs;
  41.         lbjg.Visible = true;
  42.     }
  43.     //执行查询并绑定数据
  44.     public void databin()
  45.     {
  46.         sql();
  47.         DataSet ds = dc.Query(sqlCedar, 0, 0);
  48.         DataTable dt = ds.Tables[0];
  49.         if (dt.Rows.Count != 0)
  50.         {
  51.             //增加新的一列
  52.             DataColumn dl = dt.Columns.Add("number", System.Type.GetType("System.String"));
  53.             for (int i = 0; i < dt.Rows.Count; i++)
  54.             {
  55.                 dt.Rows[i]["number"] = (i + 1).ToString();
  56.             }
  57.             //绑定数据,分页显示
  58.             PagedDataSource pds = new PagedDataSource();
  59.             Pager1.RecordCount = dt.Rows.Count;
  60.             pds.DataSource = ds.Tables[0].DefaultView;
  61.             pds.AllowPaging = true;
  62.             pds.CurrentPageIndex = Pager1.CurrentPageIndex - 1;
  63.             pds.PageSize = Pager1.PageSize;
  64.             Custom.DataSource = pds;
  65.             Custom.DataBind();
  66.             chkAll.Visible = true;
  67.             LinkButton11.Visible = true;
  68.             Linkmove.Visible = true;
  69.             
  70.         }
  71.         else
  72.         {
  73.             Custom.DataBind();
  74.             chkAll.Visible = false;
  75.             LinkButton11.Visible = false;
  76.             Linkmove.Visible = false;
  77.         }
  78.     }