SIGN UP MEMBER LOGIN:    
ARTICLE

Repeater within ASP.NET 2.0 Gridview

Posted by Navin Pandit Articles | ASP.NET Controls in C# August 28, 2008
This article demonstrates how to bind a repeater control within a GridView control having a relation between the GridView and Repeater data.
Reader Level:

Introduction

           

This article and code snippet here shows how to bind a repeater control and Gridview control to a relational data so the GridView and the Repeater control show related data.

 

Inserting a Repeater within Gridview is an easy task but to bind the headline in the GridViewand data items correspond to the particular head line into a Repeater control is a little bit trickier job. According to a requirement, I have to bind company name as headline and top four news of the company as its data items under the headline, again next company name and top four news of respective company and so on.

 

Here, I have to use a repeater control to bind news while I am binding company name in grid view. We can also make the company name as link button which on click, will navigate to corresponding page. For this we have to pass navigate URL for a company dynamically. We can do this by writing code in OnRowDataBound event of gridview. We can also make news row as link.

 

 

Mechanism

           

Take a GridView control and set its AutoGenerateColumns="False".  Put a label control within the GridView to bind company name. You can also use its BoundField.

 

Now, within the same ItemTemplate, place a repeater control and put one more label control within this repeater control to bind news of respective company. If you are using different ItemTemplate, then it will bind in another column, otherwise it will bind in the same column but in different rows as shown in fig. below.

 

Now it is your choice, whether you are interested to bind in same or different column. Here, I have used div tag, to separate company and their news rows. The div tag gives one more advantage i.e. we can apply CSS to a particular div for richer UI.

 

The complete source code is here.

           

            <div style="width:400px; font-family:Arial; font-size:small">

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="100%" GridLines="None">

            <Columns>

                <asp:TemplateField>

                <ItemTemplate>

                    <div style="color:Blue;font-weight:bold">

                        <asp:Label ID="Label1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"compname") %>'></asp:Label>

                    </div>

                    <asp:Repeater ID="Repeater1" runat="server" DataSource='<%#DataBinder.Eval(Container, "DataItem.InnerVal") %>'>

                    <ItemTemplate>

                    <div style="padding-left:20px; padding-top:5px">

                        <asp:Label ID="Label2" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"news") %>'></asp:Label>

                    </div>

                   

                    </ItemTemplate>

                    </asp:Repeater>

                    <div style="text-align:right">

                        <asp:HyperLink ID="link" runat="server">More</asp:HyperLink>

                    </div>

                </ItemTemplate>

                </asp:TemplateField>

            </Columns>

        </asp:GridView>

    </div>

 

 

It is obvious that we have to write some code in code behind to get the data from database.

I have used a stored procedure that returns two tables, i.e. for company name and news section.

 

Now I am making a relation between these two tables in a DataSet, with column 'compname'.  And finally, binding the data in data controls.

 

Here is the code on the page load event handler. As you can see from the binddat method, I get data in a DataSet from the database, sets a relation and sets the DataSource property of the GridView control to the DataSet. Rest data binding is done in the above ASP.NET code.

 

       protected void Page_Load(object sender, EventArgs e)

    {

        binddata();

    }

 

    // Function to bind data in data controls..

    public void binddata()

    {

        string str = "Data Source=VS-NAVINCHANDRA\\SQLEXPRESS;Initial Catalog=dbNavin;Integrated Security=SSPI"; // your connection string here

        SqlConnection con = new SqlConnection(str);

        DataSet ds = new DataSet();

        SqlDataAdapter da = new SqlDataAdapter("getCompNews", con); // name of your stored procedure,

        da.Fill(ds);

 

        ds.Relations.Add("InnerVal", ds.Tables[0].Columns["compname"], ds.Tables[1].Columns["compname"]);   // making a relation between two tables.

        GridView1.DataSource = ds.Tables[0];

        GridView1.DataBind();

    }

 

           

On clicking 'More' link button, it should redirect to page which shows all the news of that particular company. So, we have to set NavigateUrl property for this control dynamically. To do this, write the code given below. As I have passed company name in query string, you can very easily access respective company data from your table to display.

 

 

            protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

    {

        if (e.Row.RowType == DataControlRowType.DataRow)

        {

            HyperLink lnkMore = (HyperLink)e.Row.FindControl("link");

            Label lbl=(Label)e.Row.FindControl("Label1");

            lnkMore.NavigateUrl = "~/Company.aspx?cmp="+lbl.Text;

        }

    }

 

 

As I have mentioned for stored procedure, so you must create a stored procedure that returns tables. If you don't want to create it, just copy and paste the stored procedure given below and compile it. Make sure that your database contains all tables and respective columns.

 

            Create Procedure [dbo].[getCompNews]

 

as

begin

create table #Temp

(

compname varchar(50),

news varchar(150)

)

 

Insert into #Temp

select a.compname, b.news from Company as a

inner join  CompNews as b on a.CompId=b.CompId order by compname

 

 

Select distinct compname from #Temp

select * from #Temp

end

drop table #Temp

 

Here I am using stored procedure instead of using direct sql query. Offcourse a stored procedure is much better than a query.

 

 

Login to add your contents and source code to this article
share this article :
post comment
 

Off course you can add multiple rows into repeater as well in grid view too. For snap and detail, pls visit: <br/> http://www.codeproject.com/Articles/27260/Repeater-within-Gridview-in-C-ASP-NET-2-0 In this snap you can see multiple company headlines displayed with multiple rows of news body. Hope you got the point. Regards, Navin For more articles, visit: http://www.navinpandit.blogspot.in/p/technical.html

Posted by Navin Pandit Apr 04, 2012

I ahappy to get idea fron this articvle ,can i add more rows into the repeater which is already in side the gridview, For example i can add row into the grid view as well as into the repeater. Waiting for your reply.. nitin.jaine@gmail.com

Posted by Nitin jain Apr 04, 2012

Hi,
you can do it by adding delegates for the control dynamically or any other way that supports bubble event.

Posted by Navin Chandra Pandit Feb 06, 2009

Thanks for the help, but one more quick question.  The code behind event list does not create an event handler for the repeater control since it is nested within the gridview.  Do I have to create an event handler for the nested repeater before I can manipulate the hyperlink with your code?

Posted by Sean Fullman Jan 21, 2009

Hello Dear,
You can manipulate your data, for this you have to code explicitly in ItemDataBound event of repeater control. To bind navigate url property for hyperlink, you may use this code:

protected void repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType = = ListItemType.Item || e.Item.ItemType = = ListItemType.AlternatingItem)
        {
            HyperLink hlnk = new HyperLink();
            hlnk = (HyperLink)e.Item.FindControl("hlnkId");
            hlnk.NavigateUrl = "~/navincp.aspx;
         }
    }

Hope it will help you.

-Navin C

Posted by Navin Chandra Pandit Jan 21, 2009
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Team Foundation Server Hosting
Become a Sponsor