更改DataSource属性后,DataGridView设计模式设置将不起作用

时间:2018-08-02 19:38:00

标签: c# winforms datagridview

我有一个Win Form App,在其中一种表单中,我放置了TextBox进行搜索,并放置了DataGridView来显示结果。在网格中,我在设计模式下进行了一些设置(隐藏一些列,并在列中放置标题文本)。然后有一种这样的方法来填充网格:(首先,它在PrsCode中进行搜索,如果未找到,则搜索人员的姓氏,并且如果在上述两个字段中均未找到匹配项,则希望显示一个空白的网格)

public void FillGrid1(bool IsSearching = false)
    {
        if (IsSearching && !string.IsNullOrEmpty(TxbSearch.Text.Trim()))
        {
            var prsCode = from p in db.Prs
                          join pd in db.PDPs on p.ID equals pd.PrsID
                          where p.PrsCode.StartsWith(TxbSearch.Text.Trim())
                          select p;
            var prsLname = from p in db.Prs
                           join pd in db.PDPs on p.ID equals pd.PrsID
                           where p.Lname.Contains(TxbSearch.Text.Trim())
                           select p;
            if (prsCode.Count() > 0)
            {
                DG_PDP.DataSource = from p in db.Prs
                                    join pd in db.PDPs on p.ID equals pd.PrsID
                                    where p.PrsCode.StartsWith(TxbSearch.Text.Trim())
                                    select p;
            }
            else if (prsLname.Count() > 0)
            {
                DG_PDP.DataSource = from p in db.Prs
                                    join pd in db.PDPs on p.ID equals pd.PrsID
                                    where p.Lname.Contains(TxbSearch.Text.Trim())
                                    select p;
            }
            else
            {
                DG_PDP.DataSource = null;
            }
        }
        else
        {
            DG_PDP.DataSource = from p in db.Prs
               join pd in db.PDPs on p.ID equals pd.PrsID
               select p;
        }

在“文本框”的TextChanged事件中,我的编写如下:

FillGrid1(true);

在键入并找到匹配项时,效果很好。如果找不到匹配项,它将显示空白网格,这很好。但是,在按BackSpace并清洁TextBox以重新输入字符串之后,如果再次找到匹配项并且网格显示数据,则诸如设计模式设置的列可见性和标题文本之类的整个设置将不起作用,例如从来没有做过。如果我评论else块

else
{
    DG_PDP.DataSource = null;
}

设置问题已解决,但尚未达到我搜索的最后一个目标:

  

如果在上述两个字段中均未找到匹配项,则希望显示空白网格

1 个答案:

答案 0 :(得分:0)

我找到了答案,因为LINQ查询中“ select”之后有“ new”关键字,因此即使查询显示为null,datagridview.datasource也必须具有与查询新块中定义的字段相同的字段。它可能被认为是一种幽默的解决方案,但是由于在我的数据库中,PrsID从1开始,而不是“ DG_PDP.DataSource = null”,因此我将0与其他if子句放在同一查询的“ where”子句中,而后者当然什么也不返回。这样,设计模式设置的设置就不会损坏。如下所示:

public void FillGrid1(bool IsSearching = false)
{
if (IsSearching && !string.IsNullOrEmpty(TxbSearch.Text.Trim()))
{
    var prsCode = from p in db.Prs
                  join pd in db.PDPs on p.ID equals pd.PrsID
                  where p.PrsCode.StartsWith(TxbSearch.Text.Trim())
                  select p;
    var prsLname = from p in db.Prs
                   join pd in db.PDPs on p.ID equals pd.PrsID
                   where p.Lname.Contains(TxbSearch.Text.Trim())
                   select p;
    if (prsCode.Count() > 0)
    {
        DG_PDP.DataSource = from p in db.Prs
                            join pd in db.PDPs on p.ID equals pd.PrsID
                            where p.PrsCode.StartsWith(TxbSearch.Text.Trim())
                            select p;
    }
    else if (prsLname.Count() > 0)
    {
        DG_PDP.DataSource = from p in db.Prs
                            join pd in db.PDPs on p.ID equals pd.PrsID
                            where p.Lname.Contains(TxbSearch.Text.Trim())
                            select p;
    }
    else// it means no match has been found
    {
        //DG_PDP.DataSource = null;
        DG_PDP.DataSource = from p in db.Prs
                            join pd in db.PDPs on p.ID equals pd.PrsID
                            where p.id==0 // I know that there is no such an ID
                            select p;
    }
}
else
{
    DG_PDP.DataSource = from p in db.Prs
                        join pd in db.PDPs on p.ID equals pd.PrsID
                        join ep in db.ExecutivePosts on pd.ExePID equals ep.ID
                        join pr in db.PDP_Priorities on pd.PriorityID equals pr.ID
                        join ps in db.PDP_Satisfactions on pd.SatisfactionID equals ps.ID
                        join s in db.SubCrts on pd.SubCrtID equals s.ID
                        select p;
}