简体   繁体   English

在文本框中检测第一个字母,然后出现在gridview中

[英]Detect first letter in textbox then appears in gridview

I'm a newbie in programming. 我是编程的新手。 How can I search the employee name by the first letter and display all those name which starts from eg: A then it should get display in data grid view? 我该如何按首字母搜索员工姓名并显示所有以例如以下字母开头的姓名:那么,该姓名应显示在数据网格视图中? Currently, my code just detect 2 letters in textbox, then display in grid view. 目前,我的代码仅在文本框中检测到2个字母,然后在网格视图中显示。 I want it detect the first letter. 我希望它检测到第一个字母。 Here is my code: 这是我的代码:

private void textBoxName_KeyPress(object sender, KeyPressEventArgs e)
{
    con = new SqlConnection(cs);
    con.Open();
    adapt = new SqlDataAdapter(
        "select name, empno, workno from m_employee where name like '%" + 
        textBoxName.Text + "%' and not [recsts] = 'R' order by empno", con);
    dt = new DataTable();
    adapt.Fill(dt);
    dataGridView1.DataSource = dt;
    con.Close();

    if (textBoxName.Text != null)
    {
        dataGridView1.Visible = true;
    }

    if (textBoxName.Text == "")
    {
        dataGridView1.Visible = false;
    }

}

private void textBoxName_TextChanged(object sender, EventArgs e)
{
    textBoxName.Text = textBoxName.Text.TrimEnd();

    if (textBoxName.Text == "")
    {
        dataGridView1.Visible = false;
    }

    (dataGridView1.DataSource as DataTable).DefaultView.RowFilter = 
        string.Format("Name LIKE '%{0}%'", textBoxName.Text);
}

private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
    {

            textBoxName.Text = dataGridView1.Rows[e.RowIndex].Cells["name"].Value.ToString();
            textBoxEmplNo.Text = dataGridView1.Rows[e.RowIndex].Cells["empno"].Value.ToString();
            textBoxWorkNo.Text = dataGridView1.Rows[e.RowIndex].Cells["workno"].Value.ToString();

        string selectSql = "select icnum, empno, passport, deptno, section from m_employee where workno=@workno";
        SqlCommand cmd = new SqlCommand(selectSql, con);
        cmd.Parameters.AddWithValue("@workno", textBoxWorkNo.Text);

        bool isDataFound = false;

        try
        {
            con.Open();

            using (SqlDataReader read = cmd.ExecuteReader())
            {

                while (read.Read())
                {
                    isDataFound = true;

                    textBoxICPass.Text = (read["icnum"].ToString());
                    textBoxPassport.Text = (read["passport"].ToString());
                    textBoxDept.Text = (read["deptno"].ToString());
                    textBoxSection.Text = (read["section"].ToString());

                    string imgFilePath = @"C:\Users\hamizah\Documents\Visual Studio 2013\WebSites\EV\photo\" + textBoxEmplNo.Text + ".jpg";
                    if (File.Exists(imgFilePath))
                    {
                        pictureBox1.Visible = true;
                        pictureBox1.Image = Image.FromFile(imgFilePath);
                    }
                    else
                    {
                        // Display message that No such image found
                        // MessageBox.Show("No Image Found");
                        pictureBox1.Visible = true;
                        pictureBox1.Image = Image.FromFile(@"C:\Users\hamizah\Documents\Visual Studio 2013\WebSites\EV\photo\No-image-found.jpg");
                    }


                }
            }
        }
        finally
        {
            con.Close();
        }


    }

If you only want to get the first letter of your specific text, you can do one of the following approaches to do so: 如果只想获取特定文本的首字母,则可以采用以下方法之一:

// Access the first character by it's index
var letter = YourTextBox.Text[0];

// Access the first letter via the Substring() method
var letter = YourTextBox.Text.Substring(0, 1);

Off-topic: Consider Parameterized Queries 主题外:考虑参数化查询

Additionally, you should not use string concatenation when building your queries as it leaves you vulnerable to SQL Injection attacks. 另外,构建查询时不应使用字符串连接,因为它容易受到SQL Injection攻击的影响。 Consider instead using parameterized queries, which should keep you protected from those and may make your code a bit more readable: 考虑改用参数化查询,这可以使您免受那些查询的侵扰,并使您的代码更具可读性:

// Build your query using parameters
var query = "SELECT ... WHERE Name LIKE @letter AND ...";

// Build your adapter to execute
var adapter = new SqlDataAdapter(query, connection);

// Add your parameter
adapter.SelectCommand.Parameters.AddWithValue("@letter", letter + "%");

// Execute it here

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

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