Hi
I have data in DataSet like
State DistrictName
UP Allahabad
UP Kanpur
UP Lucknow
UP Noida
I want to show data in GridView
Like
UP
Allahabad
Kanpur
Lucknow
Noida
But it shows 'UP' against all the district (in a tabular form).
But I want to show result in GridView like the above format.
thanks in Advance
Regards
Anurag
Loading
Satyapriya NayakPosted Dec 9, 2011, 7:33 AM
Try this...
Run the attachments.
Default.aspx code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Pagetitle>
head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False"
BorderStyle="None" BorderWidth="1px" CellPadding="4"
GridLines="Horizontal" ForeColor="Black"
Height="119px" DataSourceID="SqlDataSource1"
OnDataBound="GridView1_DataBound1">
<Columns>
<asp:BoundField DataField="Country"
HeaderText="Country"
SortExpression="Country" />
<asp:BoundField DataField="State"
HeaderText="State"
SortExpression="State" />
<asp:BoundField DataField="City"
HeaderText="City"
SortExpression="City" />
Columns>
<HeaderStyle BackColor="#FF8040" />
<EditRowStyle BackColor="#FFCC00" />
<AlternatingRowStyle BackColor="White" ForeColor="Black" />
asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:TestConnectionString %>"
SelectCommand="SELECT [Country], [State], [City] FROM [Details] ORDER BY [State]">
asp:SqlDataSource>
div>
form>
body>
html>
Default.aspx.cs code
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void GridView1_DataBound1(object sender, EventArgs e)
{
for (int rowIndex = GridView1.Rows.Count - 2;
rowIndex >= 0;
rowIndex--)
{
GridViewRow gvRow = GridView1.Rows[rowIndex];
GridViewRow gvPreviousRow = GridView1.Rows[rowIndex + 1];
for (int cellCount = 0;
cellCount < gvRow.Cells.Count;
cellCount++)
{
if (gvRow.Cells[cellCount].Text == gvPreviousRow.Cells[cellCount].Text)
{
if (gvPreviousRow.Cells[cellCount].RowSpan < 2)
{
gvRow.Cells[cellCount].RowSpan = 2;
}
else
{
gvRow.Cells[cellCount].RowSpan = gvPreviousRow.Cells[cellCount].RowSpan + 1;
}
gvPreviousRow.Cells[cellCount].Visible = false;
}
}
}
}
}Thanks
If this post helps you mark it as answer
Satyapriya NayakPosted Dec 9, 2011, 8:01 AM
Try this... without using SqlDataSource.
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace Merge_Cells_in_Row_of_GridView
{
public partial class _Default : System.Web.UI.Page
{
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCommand com;
string str;
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(connStr);
con.Open();
str = "select country,state,city from location order by state";
com = new SqlCommand(str, con);
SqlDataReader reader = default(SqlDataReader);
reader = com.ExecuteReader();
GridView1.DataSource = reader;
GridView1.DataBind();
con.Close();
}
protected void GridView1_DataBound1(object sender, EventArgs e)
{
for (int rowIndex = GridView1.Rows.Count - 2; rowIndex >= 0; rowIndex += -1) {
GridViewRow gviewRow = GridView1.Rows[rowIndex];
GridViewRow gviewPreviousRow = GridView1.Rows[rowIndex + 1];
for (int cellCount = 0; cellCount <= gviewRow.Cells.Count - 1; cellCount++) {
if (gviewRow.Cells[cellCount].Text == gviewPreviousRow.Cells[cellCount].Text) {
if (gviewPreviousRow.Cells[cellCount].RowSpan < 2) {
gviewRow.Cells[cellCount].RowSpan = 2;
} else {
gviewRow.Cells[cellCount].RowSpan = gviewPreviousRow.Cells[cellCount].RowSpan + 1;
}
gviewPreviousRow.Cells[cellCount].Visible = false;
}
}
}
}
}
}
Thanks
If this post helps you mark it as answer