Prakash Mondal

Prakash Mondal

  • NA
  • 288
  • 208.9k

How to bind DataList inside Gridview in ASP.NET using C#??

Apr 2 2014 11:59 AM
I want to bind DataList inside GridView.....
I try to bind DataList inside GridView. but only bind DataList Individually..

My Code is:

DataBaseTable:



DesignView(Display.aspx):



SourseView(Display.aspx):

 <form id="form1" runat="server">
    <div>
   
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            onprerender="GridView1_PreRender">
            <Columns>
                <asp:BoundField DataField="Days" HeaderText="Days" />
                <asp:TemplateField HeaderText="10:00-12:00">
                    <ItemTemplate>
                        <asp:DataList ID="DataList1" runat="server">
                            <ItemTemplate>
                                <table class="style1">
                                    <tr>
                                        <td>
                                            <asp:LinkButton ID="LinkButton1" runat="server"><%#Eval("Dept10") %></asp:LinkButton>
                                        </td>
                                        <td>
                                            <asp:Label ID="Label1" runat="server" Text='<%#Eval("Team10") %>'></asp:Label>
                                        </td>
                                    </tr>
                                </table>
                            </ItemTemplate>
                        </asp:DataList>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="12:00-02:00">
                    <ItemTemplate>
                        <asp:DataList ID="DataList2" runat="server">
                            <ItemTemplate>
                                <table class="style1">
                                    <tr>
                                        <td>
                                            <asp:LinkButton ID="LinkButton2" runat="server"><%#Eval("Dept12") %></asp:LinkButton>
                                        </td>
                                        <td>
                                            <asp:Label ID="Label2" runat="server" Text='<%#Eval("Team12") %>'></asp:Label>
                                        </td>
                                    </tr>
                                </table>
                            </ItemTemplate>
                        </asp:DataList>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <br />
        <br />
        For Test (Only bind DataList):<br />
        10:00-12:00<br />
        <asp:DataList ID="DataList3" runat="server">
            <ItemTemplate>
                <table class="style1">
                    <tr>
                        <td>
                            <asp:LinkButton ID="LinkButton3" runat="server"><%#Eval("Dept10") %></asp:LinkButton>
                        </td>
                        <td>
                            <asp:Label ID="Label3" runat="server" Text='<%#Eval("Team10") %>'></asp:Label>
                        </td>
                    </tr>
                </table>
            </ItemTemplate>
        </asp:DataList>
   
    </div>
    </form>



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........



Answers (1)