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

时间:2017-05-11 02:59:43

标签: c# winforms visual-studio gridview textbox

我是编程新手。如何通过第一个字母搜索员工姓名并显示从例如:A开始的所有名称然后它应该显示在数据网格视图中?目前,我的代码只检测文本框中的2个字母,然后在网格视图中显示。我想要它检测第一个字母。这是我的代码:

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();
        }


    }

1 个答案:

答案 0 :(得分:0)

如果您只想获得特定文字的第一个字母,可以采用以下方法之一:

// 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);

偏离主题:考虑参数化查询

此外,在构建查询时,不应使用字符串连接,因为它会使您容易受到SQL注入攻击。请考虑使用参数化查询,这样可以保护您免受这些查询的影响,并且可以使您的代码更具可读性:

// 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