I try to bind DataList inside GridView. but only bind DataList Individually..
My Code is:
DataBaseTable:
DesignView(Display.aspx):
SourseView(Display.aspx):
CodeView(Default.aspx.cs):
public partial class Display : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\TestDB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
SqlCommand cmd = new SqlCommand();
DataTable DT = new DataTable();
DataTable dt10 = new DataTable();
DataTable dt12 = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
dt10.Columns.Add("Dept10");
dt10.Columns.Add("Team10");
dt12.Columns.Add("Dept12");
dt12.Columns.Add("Team12");
BindGridView();
}
public void BindGridView()
{
try
{
string str = "select Dept,Days,Time,Team from Routine";
SqlDataAdapter da = new SqlDataAdapter(str, con);
da.Fill(DT);
DataTable CopyDT = new DataTable();
// CopyDT = DT;
CopyDT = DT.Copy();
string dept = string.Empty;
string time = string.Empty;
string day = string.Empty;
string team = string.Empty;
DataList DL10 = (DataList)GridView1.FindControl("DataList1");
DataList DL12 = (DataList)GridView1.FindControl("DataList2");
foreach (DataRow r in CopyDT.Rows)
{
dept = r["Dept"].ToString();
day = r["Days"].ToString();
time = r["Time"].ToString();
team = r["Team"].ToString();
switch (time)
{
case "10:00-12:00":
DataRow row10 = dt10.NewRow();
row10["Dept10"] = dept ;
row10["Team10"] = team ;
dt10.Rows.Add(row10);
DataList3.DataSource = dt10;
DataList3.DataBind();
//DL10.DataSource = dt10;
//DL10.DataBind();
break;
case "12:00-02:00":
DataRow row12 = dt12.NewRow();
row12["Dept12"] = dept ;
row12["Team12"] = team ;
dt12.Rows.Add(row12);
//DL12.DataSource = dt10;
//DL12.DataBind();
break;
default :
break;
}
}
GridView1.DataSource = CopyDT;
GridView1.DataBind();
con.Close();
}
catch (Exception pm)
{
}
}
protected void GridView1_PreRender(object sender, EventArgs e)
{
ForMergeRow.MergeRows(GridView1);
}
ClassFile(ForMergeRow.cs):
public static void MergeRows(GridView gridView)
{
for (int rowIndex = gridView.Rows.Count - 2; rowIndex >= 0; rowIndex--)
{
GridViewRow row = gridView.Rows[rowIndex];
GridViewRow previousRow = gridView.Rows[rowIndex + 1];
for (int cellIndex = 0; cellIndex < row.Cells.Count; cellIndex++)
{
if (row.Cells[cellIndex].Text == previousRow.Cells[cellIndex].Text)
{
row.Cells[cellIndex].RowSpan = previousRow.Cells[cellIndex].RowSpan < 2 ? 2 : previousRow.Cells[cellIndex].RowSpan + 1;
previousRow.Cells[cellIndex].Visible = false;
}
}
}
}
OutPut:
But in this code, individually DataList (DataList3) bind properly but DataList1 & DataList2 inside GridView1 not be binded day wise.........
I use DataList3 for individually test..
code is:
case "10:00-12:00":
DataRow row10 = dt10.NewRow();
row10["Dept10"] = dept ;
row10["Team10"] = team ;
dt10.Rows.Add(row10);
DataList3.DataSource = dt10;
DataList3.DataBind();
//DL10.DataSource = dt10;
//DL10.DataBind();
break;
But I want to Bind DataList1 and DataList2 inside this GridView.........
I want to Output this Style:
Please Help me........
Lakshmanan Sethu SankaranarayanPosted Apr 2, 2014, 1:16 PM