本文介绍了过滤datagridview时如何将数字转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用员工编号而不是其他内容搜索时,我发现这是错误,即名称或电子邮件地址'Employee_ID'运算符后缺少操作数?请协助提供过滤gridview的正确指南



Hi I get this is Error when I search with employee number instead of something else i.e name or email address Missing operand after 'Employee_ID ' operator? Please assist in providing guideline for the rightway of filtering gridview

private void txtSearch_KeyUp(object sender, KeyEventArgs e)
       {
           string outputInfo = "";
           string[] keyWords = txtSearch.Text.Split(' ');


           try {

               foreach (string word in keyWords)
               {
                   if (outputInfo.Length == 0)
                   {

                       /*Convert Employee number to string if search text is numbers*/
                      outputInfo = "(Firstname LIKE '%" + word + "%' OR LastName LIKE '%" +
                           word + "%' OR Emailaddress LIKE '%" + word + "%' OR GID LIKE '%" + word + "%' OR '" + string.Format("'Employee_ID LIKE '%" + word + "%')");


                   }
                   else
                   {
                       /*Convert Employee number to string if search text is numbers*/
                       outputInfo = "(Firstname LIKE '%" + word + "%' OR LastName LIKE '%" +
                           word + "%' OR Emailaddress LIKE '%" + word + "%' OR GID LIKE '%" + word + "%' OR '" + string.Format("'Employee_ID LIKE '%" + word.ToString() + "%')");


                   }
               }

               //Applies the filter to the DataView
               myView.RowFilter = outputInfo.ToString();

           }catch(Exception ex){
               MessageBox.Show("Result " + ex.Message.ToString() + " Not Found");
           }


       }





我是什么尝试过:



我试图将Employee_ID转换为字符串,不确定我是否正确使用



What I have tried:

I have tried to convert Employee_ID to string not sure if i am doing it right

outputInfo = "(Firstname LIKE '%" + word + "%' OR LastName LIKE '%" +
                            word + "%' OR Emailaddress LIKE '%" + word + "%' OR GID LIKE '%" + word + "%' OR '" + string.Format("'Employee_ID LIKE '%" + word + "%')");

推荐答案

OR '" + string.Format("'Employee_ID 



在<$ c之后看起来像额外的单引号$ c> OR 之前 Employee_ID


/*Convert Employee_ID column to string before using like */
outputInfo = "(Firstname LIKE '%" + word + "%' OR LastName LIKE '%" + word + "%' OR Emailaddress LIKE '%" + word + "%' OR GID LIKE '%" + word + "%' OR "+ string.Format("CONVERT(Employee_ID,'System.String')")+" LIKE '%" + word + "%')";





这很好用。我希望有人发现它有用。感谢所有人提前获得帮助



That works just fine. I hope someone finds it useful. Thanks all in advance for assistance


这篇关于过滤datagridview时如何将数字转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 22:45