Merge Repeater Row If Field2 Is Empty

RAVI 896 Reputation points
2024-03-23T12:20:46.5066667+00:00

Hello

This is my aspx page

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %> 
<html>
<head runat="server">
    <title>Untitled Page</title>
        
<style type="text/css">
    table {
    border-collapse: collapse;
}
    
    table, th, td, tr {
    border: 1px solid black;
}
</style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
       <asp:Repeater ID="Repeater2" runat="server"  >
                         <HeaderTemplate>
                           <table class="table-hide">
                         <tr >   
                         
                           <th Style="width:110px;text-align:center;background-color:#F9E79F;font-family:Palatino Linotype;;font-size:16px;">FIELD 1</th>
                           
                            <th Style="width:110px;text-align:center;background-color:#F9E79F;font-family:Palatino Linotype;;font-size:16px;">FIELD 2</th>
                             
                         <th colspan="3" style="text-align:center;background-color:#F9E79F;font-family:Palatino Linotype;;font-size:16px;">FIELD 3</th>
                                   
                         <th Style="width:40px;text-align:center;background-color:#F9E79F;font-family:Palatino Linotype;;font-size:16px;">FIELD 4</th> 
                          
                     </tr>  
                         </HeaderTemplate>
                             <ItemTemplate>
                             
                                 <tr  id="GraphTable" class="hello">
                             
                                 <td colspan="1"> 
                                  <asp:Label ID="edtName" Font-Names="Calibri"  runat="server" Font-Bold="true" Font-Size="20px" 
                                    Text='<%#  Eval("Field1") %>'></asp:Label> 
                                  </td>
                                     
                                      <td   id="tdControl" style="text-align:center;font-family:Calibri;">
                                    <asp:Label ID="LP" Font-Bold="true" Font-Size="20px" Width="70px" runat="server" Text = '<%#Eval("Field2").ToString() == "0" ? "" : Eval("Field2", "{0:N3}")%>'></asp:Label>
                                     </td>
                                     
                                     
                                     <td colspan="3" style="font-family:Calibri;font-size:20px;font-weight:bold;width:210px;text-align:left;" >
                                        <%# Eval("Field3")%> 
                                     </td>
                                  
                                     
                                    
                                     <td style="text-align: right;font-family:Calibri;">
                                         <asp:Label ID="LQ" Font-Bold="true" Font-Size="20px"  Width="90px" runat="server" Text ='<%#Eval("Field4").ToString() == "0" ? "" : Eval("Field4","{0:N3}")%>' style="text-align: right;"></asp:Label> 
                                     </td>
                                       
                                     
                                     
                                     
                                    
                                 </tr>
                             </ItemTemplate>
                             <FooterTemplate> 
                            </FooterTemplate>
                         </asp:Repeater>  
    </div>
    </form>
</body>
</html>


this is my code file

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Collections.Specialized;
using System.Text;
using System.Drawing;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Net.Configuration;

public partial class Default4 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //

        DataTable hdt = new DataTable();
        SqlConnection hcon1 = new SqlConnection(ConfigurationManager.ConnectionStrings["CHEMIMSConnectionString"].ConnectionString);
        hcon1.Open();
        SqlCommand hcmd1 = new SqlCommand("select * from Table1 ", hcon1);
        SqlDataAdapter hada1 = new SqlDataAdapter(hcmd1);
        hada1.Fill(hdt);
        Repeater2.DataSource = hdt;
        Repeater2.DataBind();
        hcon1.Close();
    }
}

this is sql database

 
/****** Object:  Table [dbo].[Table1]    Script Date: 03/23/2024 17:47:09 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Table1](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[Field1] [varchar](150) NULL,
	[Field2] [float] NULL,
	[Field3] [varchar](50) NULL,
	[Field4] [float] NULL,
 CONSTRAINT [PK_Table1] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
SET IDENTITY_INSERT [dbo].[Table1] ON
INSERT [dbo].[Table1] ([ID], [Field1], [Field2], [Field3], [Field4]) VALUES (1, NULL, 200, N'APPLE', 10)
INSERT [dbo].[Table1] ([ID], [Field1], [Field2], [Field3], [Field4]) VALUES (2, NULL, 5, N'CR1', 3.5)
INSERT [dbo].[Table1] ([ID], [Field1], [Field2], [Field3], [Field4]) VALUES (3, NULL, 2, N'M1', 4)
INSERT [dbo].[Table1] ([ID], [Field1], [Field2], [Field3], [Field4]) VALUES (4, NULL, NULL, N'WASH', NULL)
INSERT [dbo].[Table1] ([ID], [Field1], [Field2], [Field3], [Field4]) VALUES (5, NULL, 6, N'T8', 2.3)
INSERT [dbo].[Table1] ([ID], [Field1], [Field2], [Field3], [Field4]) VALUES (6, NULL, 7, N'T9', 3.7)
INSERT [dbo].[Table1] ([ID], [Field1], [Field2], [Field3], [Field4]) VALUES (7, NULL, NULL, N'D/W', NULL)
SET IDENTITY_INSERT [dbo].[Table1] OFF

It showing like this

Screenshot 2024-03-23 174837

but i need to merege row if field2 is empty and make that row complete grey color like this as below

User's image

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,256 questions
0 comments No comments
{count} votes

Accepted answer
  1. Lan Huang-MSFT 25,556 Reputation points Microsoft Vendor
    2024-03-25T05:42:22.2866667+00:00

    Hi @RAVI,

    You can implement it in OnItemDataBound method. The specific code is as follows:

    protected void repRequests_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            //Reference the Repeater Item.
            RepeaterItem item = e.Item;
            //Reference the Controls.
            string Field2 = (item.FindControl("LP") as Label).Text;
            HtmlTableRow tdValue = (HtmlTableRow)e.Item.FindControl("GraphTable");
            HtmlTableCell td = (HtmlTableCell)e.Item.FindControl("tdControl");
            if (Field2 == "")
            {
                td.ColSpan = 6;
                tdValue.Cells.RemoveAt(3);
                tdValue.Cells.RemoveAt(1);
                tdValue.Cells.RemoveAt(0);
                tdValue.Attributes.Add("style", "background-color: grey;");
            }
        }
    }
    
    <html>
    <head runat="server">
        <title>Untitled Page</title>
        <style type="text/css">
            table {
                border-collapse: collapse;
            }
            table, th, td, tr {
                border: 1px solid black;
            }
        </style>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:Repeater ID="Repeater2" runat="server" OnItemDataBound="repRequests_ItemDataBound">
                    <HeaderTemplate>
                        <table class="table-hide" id="table">
                            <tr>
                                <th style="width: 110px; text-align: center; background-color: #F9E79F; font-family: Palatino Linotype; font-size: 16px;">FIELD 1</th>
                                <th style="width: 110px; text-align: center; background-color: #F9E79F; font-family: Palatino Linotype; font-size: 16px;">FIELD 2</th>
                                <th colspan="3" style="text-align: center; background-color: #F9E79F; font-family: Palatino Linotype; font-size: 16px;">FIELD 3</th>
                                <th style="width: 40px; text-align: center; background-color: #F9E79F; font-family: Palatino Linotype; font-size: 16px;">FIELD 4</th>
                            </tr>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <tr id="GraphTable" class="hello" runat="server">
                            <td colspan="1">
                                <asp:Label ID="edtName" Font-Names="Calibri" runat="server" Font-Bold="true" Font-Size="20px"
                                    Text='<%#  Eval("Field1") %>'></asp:Label>
                            </td>
                            <td runat="server" style="text-align: center; font-family: Calibri;">
                                <asp:Label ID="LP" Font-Bold="true" Font-Size="20px" Width="70px" runat="server" Text='<%#Eval("Field2").ToString() == "0" ? "" : Eval("Field2", "{0:N3}")%>'></asp:Label>
                            </td>
                            <td colspan="3" id="tdControl" style="font-family: Calibri; font-size: 20px; font-weight: bold; width: 210px; text-align: center;">
                                <%# Eval("Field3")%> 
                            </td>
                            <td style="text-align: right; font-family: Calibri;">
                                <asp:Label ID="LQ" Font-Bold="true" Font-Size="20px" Width="90px" runat="server" Text='<%#Eval("Field4").ToString() == "0" ? "" : Eval("Field4","{0:N3}")%>' Style="text-align: right;"></asp:Label>
                            </td>
                        </tr>
                    </ItemTemplate>
                    <FooterTemplate>
                        </table>
                    </FooterTemplate>
                </asp:Repeater>
            </div>
        </form>
    </body>
    </html>
    

    User's image

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful