Click here to Skip to main content
14,599,444 members

Repeater within Gridview in C# ASP.NET 2.0

Rate this:
4.44 (11 votes)
Please Sign up or sign in to vote.
4.44 (11 votes)
25 Jun 2008CPOL
This article discusses binding a gridview/repeater control from a dataset using a stored procedure


Inserting a repeater within gridview is an easy task, but binding the headline in gridview and having data items correspond to the particular headline into a repeater, is a little bit tricky. Following a requirement I was given, I had to bind company name as a headline and the top four news of the company as data items under the headline. Then the next company name, and the top four news of that respective company and so on. Like this:


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


Take a gridview control and set its AutoGenerateColumns="False". Put a label control within the grid to bind company name, so 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 a different ItemTemplate, then it will bind in another column, otherwise it will bind in the same column but in different rows as shown in the figure below. Now it is your choice, whether you are interested in binding in the same or a different column. Here, I have used a div tag, to separate company and their news rows. The div tag gives one more advantage. For example, we can apply CSS to a particular div for richer UI. The complete source code looks like this:

<div style="width:400px; font-family:Arial; font-size:small">
  <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"

    Width="100%" GridLines="None">
        <div style="color:Blue;font-weight:bold">
          <asp:Label ID="Label1" runat="server" Text='<%#
          DataBinder.Eval(Container.DataItem,"compname") %>'></asp:Label>
        <asp:Repeater ID="Repeater1" runat="server" DataSource='<%#DataBinder.Eval(
          Container, "DataItem.InnerVal") %>'>
        <div style="padding-left:20px; padding-top:5px">
        <asp:Label ID="Label2" runat="server" Text='<%#
           DataBinder.Eval(Container.DataItem,"news") %>'></asp:Label>
        <div style="text-align:right">
          <asp:HyperLink ID="link" runat="server">More</asp:HyperLink>

It is obvious; we have to write some code in the code behind to get the data from the 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 dataset, with the column 'compname'. And finally, binding the data in data controls. The code looks like this:

protected void Page_Load(object sender, EventArgs e)

// Function to bind data in data controls..
public void binddata()
    // your connection string here
    string str = "Data Source=VS-NAVINCHANDRA\\SQLEXPRESS;" +
         "Initial Catalog=dbNavin;Integrated Security=SSPI"; 
    SqlConnection con = new SqlConnection(str);
    DataSet ds = new DataSet();
    // name of your stored procedure,
    SqlDataAdapter da = new SqlDataAdapter("getCompNews", con); 

        // making a relation between two tables.
    GridView1.DataSource = ds.Tables[0];

On clicking the ‘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 the 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've mentioned, because I've used a stored procedure 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]

create table #Temp
compname varchar(50),
news varchar(150)

Insert into #Temp
select a.compname, 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
drop table #Temp

Here I am using a stored procedure instead of using a direct SQL query. Of course a stored procedure is much better than a query, so I recommend going for the stored procedure.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Navin Pandit
Software Developer (Senior)
India India
Save paper, save tree. Stop global warming.

Comments and Discussions

Questioncan you tell how to use select many row in repeater with check-box and then update all at one Pin
irfanansari4-Apr-14 12:53
Memberirfanansari4-Apr-14 12:53 
AnswerRe: can you tell how to use select many row in repeater with check-box and then update all at one Pin
Navin Pandit10-Apr-14 4:17
MemberNavin Pandit10-Apr-14 4:17 
GeneralMy vote of 5 Pin
Kanasz Robert25-Sep-12 22:39
MemberKanasz Robert25-Sep-12 22:39 
GeneralRe: My vote of 5 Pin
Navin Pandit23-Dec-12 19:16
MemberNavin Pandit23-Dec-12 19:16 
QuestionDatabase Pin
Eid Shkhaidem23-Aug-11 4:05
MemberEid Shkhaidem23-Aug-11 4:05 
AnswerRe: Database Pin
Navin Pandit23-Aug-11 19:16
MemberNavin Pandit23-Aug-11 19:16 
GeneralMy vote of 5 Pin
vai_rajput10-Apr-11 19:52
Membervai_rajput10-Apr-11 19:52 
GeneralRe: My vote of 5 Pin
Navin Pandit10-Apr-11 20:03
MemberNavin Pandit10-Apr-11 20:03 
GeneralGood Article Pin
zubairy14-Apr-10 6:53
Memberzubairy14-Apr-10 6:53 
GeneralRe: Good Article Pin
Navin Pandit17-Aug-11 19:17
MemberNavin Pandit17-Aug-11 19:17 
Generalrepeater on new row Pin
david weinstein17-Dec-09 7:11
Memberdavid weinstein17-Dec-09 7:11 
GeneralRe: repeater on new row [modified] Pin
Navin Pandit20-Dec-09 17:17
MemberNavin Pandit20-Dec-09 17:17 
GeneralCollapsiblePanel in the grid View Pin
Aasif Inamdar31-Oct-09 19:49
MemberAasif Inamdar31-Oct-09 19:49 
GeneralRe: CollapsiblePanel in the grid View Pin
Navin Pandit1-Nov-09 22:26
MemberNavin Pandit1-Nov-09 22:26 
GeneralVery Helpful Pin
dav0idz15-Apr-09 15:47
Memberdav0idz15-Apr-09 15:47 
GeneralRe: Very Helpful Pin
Navin Pandit23-Apr-09 19:14
MemberNavin Pandit23-Apr-09 19:14 
QuestionHow do you display just the first 3 results ? Pin
Mayuresh802-Nov-08 15:20
MemberMayuresh802-Nov-08 15:20 
AnswerRe: How do you display just the first 3 results ? Pin
Navin Pandit19-Nov-08 20:30
MemberNavin Pandit19-Nov-08 20:30 
GeneralGood article Pin
Rajib Ahmed26-Jun-08 16:41
MemberRajib Ahmed26-Jun-08 16:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Posted 25 Jun 2008

Tagged as


44 bookmarked