首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如果日期在Gridview中不同,则在行之间插入空格

如果日期在Gridview中不同,则在行之间插入空格
EN

Stack Overflow用户
提问于 2015-04-20 20:32:56
回答 2查看 1.7K关注 0票数 0

我陷入了一个问题,我有一个网格视图,如果日期不同,我想显示行之间的空格。

我尝试添加新行,但是它有Id列和Company Id列,它们不能为空。因此不能添加新行。

现在,我尝试在行之间添加空格。它适用于所有记录,但没有不同的日期条件。

我最终想要的是,如果日期相同,则行之间没有空格,如果不同,则为空格。

这将为日期提供更好的可读性。

注意:数据是按不同列排序的

我正在使用Mysql

EN

回答 2

Stack Overflow用户

发布于 2015-04-20 21:00:49

假设您使用DataTable作为GridView的DataSource (也可以以类似的方式处理不同的源):

代码语言:javascript
运行
复制
DataRow lastRow = null;
protected void GrdidView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        DataRow thisRow = ((DataRowView) e.Row.DataItem).Row;
        if(lastRow != null) // only add separators between two rows
        {
            DateTime thisDate = thisRow.Field<DateTime>("ColumnName");
            DateTime lastDate = lastRow.Field<DateTime>("ColumnName");
            if (thisDate.Date != lastDate.Date)
            {
                e.Row.Style.Add("border-top-width", "20px");
            }
        }
        lastRow = thisRow;
    }
}
票数 1
EN

Stack Overflow用户

发布于 2015-04-21 00:53:09

如果在GridView列中使用列模板,可以尝试这种方法。在GridView prerender事件循环中遍历该表以比较日期。

代码语言:javascript
运行
复制
<asp:GridView runat="server" id="GridView" AutoGenerateColumns="False" >
    <Columns>
            <asp:TemplateField>
               <ItemTemplate>
                <asp:Literal runat="server" id="ltDate" Text='<%#Bind("MyDate")%>' />
                </ItemTemplate>
            </asp:TemplateField>
    </Columns>
</asp:GridView>

private void GridView_PreRender(object sender, System.EventArgs e)
    {
    string sCompareDate = "";
    foreach (GridViewRow gvRow in GridView.Rows) {
        //First Row
        if (string.IsNullOrEmpty(sCompareDate))
            sCompareDate = ((Literal)gvRow.FindControl("ltDate")).Text;

        if (sCompareDate != ((Literal)gvRow.FindControl("ltDate")).Text) {
            gvRow.Cells(0).Attributes.Add("style", "border-bottom-width:30px;");
            sCompareDate = ((Literal)gvRow.FindControl("ltDate")).Text;
    }
}
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29748004

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档