我陷入了一个问题,我有一个网格视图,如果日期不同,我想显示行之间的空格。
我尝试添加新行,但是它有Id列和Company Id列,它们不能为空。因此不能添加新行。
现在,我尝试在行之间添加空格。它适用于所有记录,但没有不同的日期条件。
我最终想要的是,如果日期相同,则行之间没有空格,如果不同,则为空格。
这将为日期提供更好的可读性。
注意:数据是按不同列排序的
我正在使用Mysql
发布于 2015-04-20 13:00:49
假设您使用DataTable
作为GridView的DataSource
(也可以以类似的方式处理不同的源):
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;
}
}
发布于 2015-04-20 16:53:09
如果在GridView列中使用列模板,可以尝试这种方法。在GridView prerender事件循环中遍历该表以比较日期。
<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;
}
}
}
https://stackoverflow.com/questions/29748004
复制