0

在数据库中搜索仅在带有链接按钮的 Gridivew 中显示名字和姓氏。

在此处输入图像描述

在第一张图片(1)中,我只搜索名字,并显示名字和姓氏。在第二个图像(2)中,我只搜索第一个字符并显示它,但我显示它 5 次,为什么?只想要一次?

这是我的代码

  private void GetEmployee()
    {
        var db = new knowitCVdbEntities();
        var employee = (from p in db.EMPLOYEES
                        where
                            p.firstname.Contains(TextBoxSearch.Text) ||
                            p.lastname.Contains(TextBoxSearch.Text) ||
                            p.position.Contains(TextBoxSearch.Text)
                        select p).ToList();

        foreach (var vEmp in employee)
        {
            if (vEmp != null)
            {
                HiddenFieldID.Value = vEmp.employee_id.ToString();

                        if (Session["DataTableSearch"] != null)
                        {
                            _dt = (DataTable)Session["DataTableSearch"];
                        }
                        else
                        {
                            _dt.Columns.Add("Firstname");
                            _dt.Columns.Add("employeeId");

                        }
                        //_dt.Rows.Clear();
                        DataRow dr = _dt.NewRow();
                        dr["Firstname"] = vEmp.firstname+" "+vEmp.lastname;
                         dr["employeeId"] = vEmp.employee_id;

                        _dt.Rows.Add(dr);
                        Session["DataTableSearch"] = _dt;
                        GridViewDisplayName.DataSource = _dt;
                        GridViewDisplayName.DataBind();
                    }
            }

    }
4

2 回答 2

0

当您完成并运行它时,它将保存旧的搜索,

所以你必须在一开始就调用 _dt.clear ,这样理论上你每次只会有一个结果:)

编辑-事实上,您的代码中实际上已经明确了,只是注释掉了,只是在错误的地方:)

于 2013-05-20T15:22:38.223 回答
0

在你的行之前插入这个foreach (var vEmp in employee)

_dt.Clear();

所以你的代码是:

private void GetEmployee()
{
    var db = new knowitCVdbEntities();
    var employee = (from p in db.EMPLOYEES
                    where
                        p.firstname.Contains(TextBoxSearch.Text) ||
                        p.lastname.Contains(TextBoxSearch.Text) ||
                        p.position.Contains(TextBoxSearch.Text)
                    select p).ToList();

    _dt.Clear();

    foreach (var vEmp in employee)
    {
        if (vEmp != null)
        {
            HiddenFieldID.Value = vEmp.employee_id.ToString();

                    if (Session["DataTableSearch"] != null)
                    {
                        _dt = (DataTable)Session["DataTableSearch"];
                    }
                    else
                    {
                        _dt.Columns.Add("Firstname");
                        _dt.Columns.Add("employeeId");

                    }
                    //_dt.Rows.Clear();
                    DataRow dr = _dt.NewRow();
                    dr["Firstname"] = vEmp.firstname+" "+vEmp.lastname;
                     dr["employeeId"] = vEmp.employee_id;

                    _dt.Rows.Add(dr);
                    Session["DataTableSearch"] = _dt;
                    GridViewDisplayName.DataSource = _dt;
                    GridViewDisplayName.DataBind();
                }
        }

}
于 2013-05-20T15:24:10.027 回答