如何在基于组合框和文本框的datagridview中显示数据? [英] How to display data inside datagridview based from combobox and textbox?

查看:73
本文介绍了如何在基于组合框和文本框的datagridview中显示数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!

我想根据我的组合框和文本框中的条件在datagridview中显示数据。如果用户选择组合框内的项目,则会显示数据,同样使用文本框。例如:如果用户选择,2015-2016并按搜索按钮,数据将显示到与组合框相关的datagridview。如果用户输入任何字符,并按搜索按钮,它将根据组合框和文本框显示数据。用户也可以根据这两个工具进行搜索。我这里有我的代码,但没有任何反应。任何人都可以帮助我吗?



Hi!
I would like to display data inside my datagridview based from the conditions from my combo box and textbox. Data will display if the user select an item inside the combo box, likewise with the text box. For example: If the user choose, 2015-2016 and press search button, data will display to the datagridview related to the combo box. if the user input any characters, and press search button, it will display the data based from the combo box and textbox.The user can also search based from both tools. I have a my code here, but nothing happens.Can anyone help me?

 //Button_Search Code
MySqlConnection = new SqlConnection(conn.GetServers());//Connection to the database
          MySqlConnection.Open();
          m_da = new SqlDataAdapter("Select AssessmentNumber,StudentFirstName, StudentMiddleName, StudentLastName,Gender,GradeLevel,Status,Address,City,Province, Age from tblAssessment where SchoolYear ='" + schoolyear.SelectedItem + "' AND AssessmentNumber LIKE '" + stud_search.Text + "' OR StudentFirstName LIKE '" + stud_search.Text + "' OR StudentLastName LIKE '" + stud_search.Text + "' ", MySqlConnection);
          ds = new System.Data.DataSet();
          m_da.Fill(ds, "Student_Info");
          dg_studSearch.DataSource = ds.Tables[0];







void IsLoadToCombobox() // this is what my data in combobox will be.
        {
            MySqlConnection = new SqlConnection(conn.GetServers());

            SqlCommand sqlCmd = new SqlCommand("SELECT DISTINCT SchoolYear FROM tblSchoolYear", MySqlConnection);
            MySqlConnection.Open();
            SqlDataReader sqlReader = sqlCmd.ExecuteReader();

            while (sqlReader.Read())
            {
                schoolyear.Items.Add(sqlReader["SchoolYear"].ToString());
            }

            sqlReader.Close();
        }



这是我的全部代码。


that's all my code.

推荐答案

很有可能是它的内容提示问题的TextBox:SQL LIKE与Windows*。*文件通配符的工作方式不同。

尝试添加前导和尾随%:

It's quite possible that it's the content of your TextBoxes that gives the problem: SQL LIKE doesn't work the same as Windows "*.*" file wildcards.
Try adding a leading and trailing "%":
... OR StudentFirstName LIKE '%" + stud_search.Text + "%' OR ...

它可能会开始做你想做的事。



但严重的是,正如RyanDev所说,永远不会连接用于构建SQL命令的字符串。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。使用参数化查询代替,甚至用于测试 - 它更安全(并且通常也更容易阅读)。

如果你不这样做,你就赢了;后来又回来了修复它 - 当你的DB被意外损坏时它会非常严重地咬你...而且你不知道它是怎么发生的。

and it may start to do what you want.

But seriously, as RyanDev says, never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead, even for testing - it's a lot safer (and generally easier to read as well).
If you don't do it to start with, you won;t come back later and fix it - and it will bite you very badly later when your DB gets damaged by accident...and you don't know how it happened.


这篇关于如何在基于组合框和文本框的datagridview中显示数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆