Can't get Heads to show with only one record

本文关键字:with show only one record to Heads get Can | 更新日期: 2023-09-27 18:27:05

我正在下拉一些COUNT为0或大于1的记录。查询工作正常,可以正常下拉,并且在SQL结果中看起来很棒。但我正在循环查看结果,创建一个Header,并根据Country对其进行分组,并列出该Country的用户名称。

这是我的SQL:

SELECT * FROM (
SELECT u.ContactName
      ,cu.[User ID]
      ,c.Name
      ,c.ID
      ,cu.[Foreign Table]
      ,count(*) OVER (PARTITION BY c.ID) AS user_in_this_country
FROM   dbo.Country AS c
INNER JOIN   dbo.CountryUser AS cu ON c.ID = cu.[Foreign ID]
INNER JOIN   dbo.UserColder  AS  u ON cu.[User ID] = u.ID
WHERE  EXISTS (
    SELECT *
    FROM   CountryUser AS cu2
    WHERE  cu2.[Foreign ID] = cu.[Foreign ID]
    AND    cu2.[User ID] <> cu.[User ID]
    AND    cu2.[Foreign Table] = 'Country')
    )
    t
    WHERE user_in_this_country > 1 or user_in_this_country = 0
    ORDER BY Name ASC

这将下拉如下数据:

Justin United States 2
Bob United States 2

然后我循环并添加标题/组的方法是:

string lastValue = "";
protected string AddGroupingHeader(string Value)
{

    //Get the data field value of interest for this row 
    string currentValue = Value;

    //Specify name to display if dataFieldValue is a database NULL 
    if (currentValue.Length == 0)
    {
        currentValue = "";
    }

    //See if there's been a change in value 
    if (lastValue != currentValue)
    {
        //There's been a change! Record the change and emit the header 
        lastValue = currentValue;
        return "<div style='float: left; font-size: 16px; margin: 10px 0px 0px 10px;'>" + currentValue + "</div>";
    }
    else
    {
        return "";
    }
}

在aspx中,我只是传递这样的值:

<%# AddGroupingHeader(Eval("Name").ToString())%>

现在,如果我有几个记录,它工作正常,显示如下:

United States
 - Justin
 - Bob
China
 - Frank
 - Ted

但这就是问题所在。如果我只有:

United States Justin
United Bob

然后它就不放标题了。它这样做:

Justin
Bob

很抱歉发了这么长的帖子,但有人看到什么会导致这种情况吗?我已经被难住了两天了。:(

谢谢!

Can't get Heads to show with only one record

删除float样式。方法CCD_ 2可以被简化。您可以称之为只传递值,该值用作标头。

string _lastHeader;
protected string AddGroupingHeader(string header)
{ 
    if (header != _lastHeader) {
        _lastHeader = header;
        return "<div style='font-size: 16px; margin: 10px 0px 0px 10px;'>" + header + "</div>";
    } 
    return "";
}