Merge same cells in gridview
Merge same repeat name in one cells like name, mobile....etc.
aspx code
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
Width="1100px" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None"
BorderWidth="1px" CellPadding="3" Font-Size="Small"
style="margin-left: 22px;" OnDataBound="OnDataBound">
<Columns>
<%--<asp:TemplateField HeaderText="Sno">
<ItemTemplate>
<%#Container.DataItemIndex+1 %>
</ItemTemplate>
</asp:TemplateField>--%>
<asp:BoundField DataField="pid" HeaderText="PID" />
<asp:BoundField DataField="pname" HeaderText="Name" />
<asp:BoundField DataField="pemail" HeaderText="Email" />
<asp:BoundField DataField="pmobile" HeaderText="Mobile" />
<asp:BoundField DataField="date" HeaderText="Date" />
<asp:TemplateField HeaderText="Action" >
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="viewprescriptionform_Click">View Prescription</asp:LinkButton>
<asp:Label ID="Labelpid" runat="server" Text='<%# Eval("pid") %>' Visible="false" />
<asp:Label ID="Label1" runat="server" Text='<%# Eval("date") %>' Visible="false" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="White" ForeColor="#000066" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#007DBB" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#00547E" />
</asp:GridView>
aspx.cs code
protected void showprescription()
{
string cs = ConfigurationManager.AppSettings["adc-db"];
cn = new SqlConnection(cs);
cn.Open();
string k = "select * from prescrb ";
cm = new SqlCommand(k, cn);
dr = cm.ExecuteReader();
GridView1.DataSource = dr;
GridView1.DataBind();
}
protected void OnDataBound(object sender, EventArgs e)
{
for (int i = GridView1.Rows.Count - 1; i > 0; i--)
{
GridViewRow row = GridView1.Rows[i];
GridViewRow previousRow = GridView1.Rows[i - 1];
if (row.Cells[0].Text == previousRow.Cells[0].Text)
{
if (previousRow.Cells[0].RowSpan == 0)
{
if (row.Cells[0].RowSpan == 0)
{
previousRow.Cells[0].RowSpan += 2;
}
else
{
previousRow.Cells[0].RowSpan = row.Cells[0].RowSpan + 1;
}
row.Cells[0].Visible = false;
}
}
}
for (int i = GridView1.Rows.Count - 1; i > 0; i--)
{
GridViewRow row = GridView1.Rows[i];
GridViewRow previousRow = GridView1.Rows[i - 1];
if (row.Cells[1].Text == previousRow.Cells[1].Text)
{
if (previousRow.Cells[1].RowSpan == 0)
{
if (row.Cells[1].RowSpan == 0)
{
previousRow.Cells[1].RowSpan += 2;
}
else
{
previousRow.Cells[1].RowSpan = row.Cells[1].RowSpan + 1;
}
row.Cells[1].Visible = false;
}
}
}
for (int i = GridView1.Rows.Count - 1; i > 0; i--)
{
GridViewRow row = GridView1.Rows[i];
GridViewRow previousRow = GridView1.Rows[i - 1];
if (row.Cells[2].Text == previousRow.Cells[2].Text)
{
if (previousRow.Cells[2].RowSpan == 0)
{
if (row.Cells[2].RowSpan == 0)
{
previousRow.Cells[2].RowSpan += 2;
}
else
{
previousRow.Cells[2].RowSpan = row.Cells[2].RowSpan + 1;
}
row.Cells[2].Visible = false;
}
}
}
for (int i = GridView1.Rows.Count - 1; i > 0; i--)
{
GridViewRow row = GridView1.Rows[i];
GridViewRow previousRow = GridView1.Rows[i - 1];
if (row.Cells[3].Text == previousRow.Cells[3].Text)
{
if (previousRow.Cells[3].RowSpan == 0)
{
if (row.Cells[3].RowSpan == 0)
{
previousRow.Cells[3].RowSpan += 2;
}
else
{
previousRow.Cells[3].RowSpan = row.Cells[3].RowSpan + 1;
}
row.Cells[3].Visible = false;
}
}
}
}
aspx code
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
Width="1100px" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None"
BorderWidth="1px" CellPadding="3" Font-Size="Small"
style="margin-left: 22px;" OnDataBound="OnDataBound">
<Columns>
<%--<asp:TemplateField HeaderText="Sno">
<ItemTemplate>
<%#Container.DataItemIndex+1 %>
</ItemTemplate>
</asp:TemplateField>--%>
<asp:BoundField DataField="pid" HeaderText="PID" />
<asp:BoundField DataField="pname" HeaderText="Name" />
<asp:BoundField DataField="pemail" HeaderText="Email" />
<asp:BoundField DataField="pmobile" HeaderText="Mobile" />
<asp:BoundField DataField="date" HeaderText="Date" />
<asp:TemplateField HeaderText="Action" >
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="viewprescriptionform_Click">View Prescription</asp:LinkButton>
<asp:Label ID="Labelpid" runat="server" Text='<%# Eval("pid") %>' Visible="false" />
<asp:Label ID="Label1" runat="server" Text='<%# Eval("date") %>' Visible="false" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="White" ForeColor="#000066" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#007DBB" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#00547E" />
</asp:GridView>
aspx.cs code
protected void showprescription()
{
string cs = ConfigurationManager.AppSettings["adc-db"];
cn = new SqlConnection(cs);
cn.Open();
string k = "select * from prescrb ";
cm = new SqlCommand(k, cn);
dr = cm.ExecuteReader();
GridView1.DataSource = dr;
GridView1.DataBind();
}
protected void OnDataBound(object sender, EventArgs e)
{
for (int i = GridView1.Rows.Count - 1; i > 0; i--)
{
GridViewRow row = GridView1.Rows[i];
GridViewRow previousRow = GridView1.Rows[i - 1];
if (row.Cells[0].Text == previousRow.Cells[0].Text)
{
if (previousRow.Cells[0].RowSpan == 0)
{
if (row.Cells[0].RowSpan == 0)
{
previousRow.Cells[0].RowSpan += 2;
}
else
{
previousRow.Cells[0].RowSpan = row.Cells[0].RowSpan + 1;
}
row.Cells[0].Visible = false;
}
}
}
for (int i = GridView1.Rows.Count - 1; i > 0; i--)
{
GridViewRow row = GridView1.Rows[i];
GridViewRow previousRow = GridView1.Rows[i - 1];
if (row.Cells[1].Text == previousRow.Cells[1].Text)
{
if (previousRow.Cells[1].RowSpan == 0)
{
if (row.Cells[1].RowSpan == 0)
{
previousRow.Cells[1].RowSpan += 2;
}
else
{
previousRow.Cells[1].RowSpan = row.Cells[1].RowSpan + 1;
}
row.Cells[1].Visible = false;
}
}
}
for (int i = GridView1.Rows.Count - 1; i > 0; i--)
{
GridViewRow row = GridView1.Rows[i];
GridViewRow previousRow = GridView1.Rows[i - 1];
if (row.Cells[2].Text == previousRow.Cells[2].Text)
{
if (previousRow.Cells[2].RowSpan == 0)
{
if (row.Cells[2].RowSpan == 0)
{
previousRow.Cells[2].RowSpan += 2;
}
else
{
previousRow.Cells[2].RowSpan = row.Cells[2].RowSpan + 1;
}
row.Cells[2].Visible = false;
}
}
}
for (int i = GridView1.Rows.Count - 1; i > 0; i--)
{
GridViewRow row = GridView1.Rows[i];
GridViewRow previousRow = GridView1.Rows[i - 1];
if (row.Cells[3].Text == previousRow.Cells[3].Text)
{
if (previousRow.Cells[3].RowSpan == 0)
{
if (row.Cells[3].RowSpan == 0)
{
previousRow.Cells[3].RowSpan += 2;
}
else
{
previousRow.Cells[3].RowSpan = row.Cells[3].RowSpan + 1;
}
row.Cells[3].Visible = false;
}
}
}
}