Merge Gridview Row Based If Field2 Empty

RAVI 896 Reputation points
2024-03-25T08:35:28.5+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>
    
        <br />
        <asp:GridView ID="GV1" runat="server" AllowSorting="True" AutoGenerateColumns="False"
            CellSpacing="3" Font-Bold="True" Font-Names="Calibri" Font-Size="11pt"  
            Width="960px">
            <Columns>
                <asp:TemplateField>
                    <HeaderTemplate>
                        SI NO</HeaderTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblSRNO" runat="server" Text='<%#Container.DataItemIndex+1 %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                 
                <asp:TemplateField HeaderText="Field 1" SortExpression="Shade">
                    <ItemTemplate>
                        <asp:Label ID="LS2" runat="server" Text='<%# Bind("Field1") %>'></asp:Label>
                    </ItemTemplate>
                    <HeaderStyle ForeColor="#00C000" />
                    <ItemStyle Width="200px" />
                </asp:TemplateField>
                
                
                
                <asp:BoundField DataField="Field2" HeaderText="Field 2" SortExpression="Process">
                    <HeaderStyle ForeColor="Red" />
                </asp:BoundField>
                
                
                
                
                
                
                <asp:TemplateField HeaderText="Field 3" SortExpression="PercByWeight">
                    <ItemTemplate>
                        <asp:Label ID="L3" runat="server" Text='<%# Bind("Field3") %>'></asp:Label>
                    </ItemTemplate>
                    <HeaderStyle ForeColor="Red" />
                    <ItemStyle HorizontalAlign="Center" />
                </asp:TemplateField>
                
                
                <asp:TemplateField HeaderText="Field 4" SortExpression="ChemicalName">
                    <ItemTemplate>
                        <asp:Label ID="L2" runat="server" Text='<%# Bind("Field4") %>'></asp:Label>
                    </ItemTemplate>
                    <HeaderStyle ForeColor="Red" />
                    <ItemStyle HorizontalAlign="Center" Width="200px" />
                </asp:TemplateField>
                 
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>

This is my aspx code behind

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);
        GV1.DataSource = hdt;
        GV1.DataBind();
        hcon1.Close();
    }
}

This is my ms sql

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, NULL, N'WGL', NULL) INSERT [dbo].[Table1] ([ID], [Field1], [Field2], [Field3], [Field4]) VALUES (2, NULL, 1.5, N'JKL', 0.3) INSERT [dbo].[Table1] ([ID], [Field1], [Field2], [Field3], [Field4]) VALUES (3, NULL, 0.3, N'EDC', 1.5) INSERT [dbo].[Table1] ([ID], [Field1], [Field2], [Field3], [Field4]) VALUES (4, NULL, NULL, N'WAS', NULL) INSERT [dbo].[Table1] ([ID], [Field1], [Field2], [Field3], [Field4]) VALUES (5, NULL, 9.9, N'HH', NULL) INSERT [dbo].[Table1] ([ID], [Field1], [Field2], [Field3], [Field4]) VALUES (6, NULL, 3.4, N'VV', 3.4) SET IDENTITY_INSERT [dbo].[Table1] OFF 

Coming Out put like this qazqaz

I need like this as below

User's image

Thanking You

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-26T02:57:47.5533333+00:00

    Hi @RAVI,

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

     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);
         GV1.DataSource = hdt;
         GV1.DataBind();
         hcon1.Close();
     }
     int previousCellValue = 0;
     protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
     {
         //check if the row is a datarow
         if (e.Row.RowType == DataControlRowType.DataRow)
         {
             //cast the dataitem back to a row
             DataRowView row = e.Row.DataItem as DataRowView;
             Label lbl = e.Row.FindControl("lblSRNO") as Label;
             //check if the current id matches the previous row
             if (row["Field2"].ToString() == "")
             {
                 //clear the first cell
                 e.Row.Cells[0].Text = row["Field3"].ToString();
                 e.Row.Cells[0].Attributes.Add("style", "background-color: grey;");
                 e.Row.Cells[0].Attributes.CssStyle["text-align"] = "center";
                 //apply column span
                 e.Row.Cells[0].ColumnSpan = 5;
                 for (var i = 1; i < e.Row.Cells.Count; i++)
                 {
                     e.Row.Cells[i].Visible = false;
                 }
                 previousCellValue++;
             }
             else
             {
                 lbl.Text = (Convert.ToInt32(lbl.Text) - previousCellValue).ToString();
             }
         }
     }
    
    <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>
                <br />
                <asp:GridView ID="GV1" runat="server" AllowSorting="True" AutoGenerateColumns="False"
                    CellSpacing="3" Font-Bold="True" Font-Names="Calibri" Font-Size="11pt"
                    Width="960px" OnRowDataBound="GridView1_RowDataBound">
                    <Columns>
                        <asp:TemplateField>
                            <HeaderTemplate>
                                SI NO
                            </HeaderTemplate>
                            <ItemTemplate>
                                <asp:Label ID="lblSRNO" runat="server" Text='<%#Container.DataItemIndex+1 %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Field 1" SortExpression="Shade">
                            <ItemTemplate>
                                <asp:Label ID="LS2" runat="server" Text='<%# Bind("Field1") %>'></asp:Label>
                            </ItemTemplate>
                            <HeaderStyle ForeColor="#00C000" />
                            <ItemStyle Width="200px" />
                        </asp:TemplateField>
                        <asp:BoundField DataField="Field2" HeaderText="Field 2" SortExpression="Process">
                            <HeaderStyle ForeColor="Red" />
                        </asp:BoundField>
                        <asp:TemplateField HeaderText="Field 3" SortExpression="PercByWeight">
                            <ItemTemplate>
                                <asp:Label ID="L3" runat="server" Text='<%# Bind("Field3") %>'></asp:Label>
                            </ItemTemplate>
                            <HeaderStyle ForeColor="Red" />
                            <ItemStyle HorizontalAlign="Center" />
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Field 4" SortExpression="ChemicalName">
                            <ItemTemplate>
                                <asp:Label ID="L2" runat="server" Text='<%# Bind("Field4") %>'></asp:Label>
                            </ItemTemplate>
                            <HeaderStyle ForeColor="Red" />
                            <ItemStyle HorizontalAlign="Center" Width="200px" />
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
            </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