I want to insert a row into a gridview in which I place some sort of add (which I read from another table).
So I have regular rows in my gridview, for diplaying users, and I have after every x rows a row with no user information but some advertisement from a table.
How can I best approach this without that the extra Advertisement row affects the paging mechanism?
please mark answers as 'Answered' and post back solutions when you figure stuff out that isnt in the post already.
I have done that where i have 10 items in a page and after the 5th item I add the advertisement. It by Inheriting the gridview control and creating and AdContext, that during the rendering of rows, it would add and extra <tr> pair wih a <TD> with a colspan
equal to the number of fields. It inserted at the halfway point of the paging amount, so if there were four, it would insert at two. I will have to dig out the code if you need the sample, actually i should post it to codeplex.
let me know if this helps.
Brad Foley
Web Developer
http://www.blfoley.com --------------------------
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
I'll do you one better. You can use this code here.
Dictionary<string, string> Items = new Dictionary<string, string>();
Items.Add("Blue", "Rabbit");
Items.Add("Red", "Elephant");
Items.Add("Green", "Tiger");
Items.Add("Purple", "Lion");
grd1.RowDataBound += new GridViewRowEventHandler(grd1_RowDataBound);
grd1.DataSource = Items;
grd1.DataBind();
GridViewRow gvr = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Normal);
TableCell tc = new TableCell();
tc.Text = "Your Ad Content Here";
tc.ColumnSpan = grd1.Columns.Count; /* you'll need a column count, i know mine so it's hardcoded */
gvr.Cells.Add(tc);
grd1.Controls[0].Controls.AddAt(grd1.Controls[0].Controls.Count / 2, gvr);
It's much cleaner than the version and esentially does the same thing. you'll just need to make sure your column count is right (see comment) to ensure the columnspan gets set right.
Let me know if this helps.
Brad Foley
Web Developer
http://www.blfoley.com --------------------------
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
oh yea, don't mind the RowDataBound Event, it's not needed.
Brad Foley
Web Developer
http://www.blfoley.com --------------------------
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
hmm...I dont seem to be able to add the row every 5 rows...I now have this (just tried to insert it on specified index):
Protected Sub gvSearchResults_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvSearchResults.PreRender
Dim gvr As GridViewRow = New GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Normal)
Dim tc As TableCell = New TableCell
tc.Text = "Your Ad Content Here"
tc.ColumnSpan = 1
gvr.Cells.Add(tc)
'If gvSearchResults.Controls(0).Controls.Count Mod 5 = 0 Then
gvSearchResults.Controls(0).Controls.AddAt(2, gvr)
gvSearchResults.Controls(0).Controls.AddAt(3, gvr)
'End If
End Sub
What am i doing wrong?
please mark answers as 'Answered' and post back solutions when you figure stuff out that isnt in the post already.
Can you move that code logic to Load and try it there. There may be some funny business going on in the gridview, but your code looks correct.
Brad Foley
Web Developer
http://www.blfoley.com --------------------------
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
I now have this, but still no succes...the text is only shown after the first record in the gridview...?
Protected Sub gvSearchResults_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvSearchResults.Load
Dim gvr As GridViewRow = New GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Normal)
Dim tc As TableCell = New TableCell
tc.Text = "Your Ad Content Here"
tc.ColumnSpan = 1
gvr.Cells.Add(tc)
'If gvSearchResults.Controls(0).Controls.Count Mod 5 = 0 Then
gvSearchResults.Controls(0).Controls.AddAt(2, gvr)
gvSearchResults.Controls(0).Controls.AddAt(3, gvr)
'gvSearchResults.Controls(0).Controls.AddAt(15, gvr)
'AddAt((gvSearchResults.Controls(0).Controls.Count / 5), gvr)
'End If
End Sub
please mark answers as 'Answered' and post back solutions when you figure stuff out that isnt in the post already.
Peter Smith
Contributor
4604 Points
2092 Posts
insert advertisement row in gridview
Jan 16, 2009 02:43 PM|LINK
I want to insert a row into a gridview in which I place some sort of add (which I read from another table).
So I have regular rows in my gridview, for diplaying users, and I have after every x rows a row with no user information but some advertisement from a table.
How can I best approach this without that the extra Advertisement row affects the paging mechanism?
blfoleyus
Participant
1129 Points
192 Posts
Re: insert advertisement row in gridview
Jan 16, 2009 02:47 PM|LINK
I have done that where i have 10 items in a page and after the 5th item I add the advertisement. It by Inheriting the gridview control and creating and AdContext, that during the rendering of rows, it would add and extra <tr> pair wih a <TD> with a colspan equal to the number of fields. It inserted at the halfway point of the paging amount, so if there were four, it would insert at two. I will have to dig out the code if you need the sample, actually i should post it to codeplex.
let me know if this helps.
Web Developer
http://www.blfoley.com
--------------------------
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
naveenj
Contributor
6112 Points
1123 Posts
Re: insert advertisement row in gridview
Jan 16, 2009 04:22 PM|LINK
Nice Question [cool]
I would like to know the answer too...
Naveen Jose
ASP.NET Freelancer, Consultant
Please remember to click Mark as Answer on the post that helps you
jsriharsha
Contributor
2254 Points
459 Posts
Re: insert advertisement row in gridview
Jan 16, 2009 04:30 PM|LINK
Check this article .....it might help you
http://www.dotnetcurry.com/ShowArticle.aspx?ID=152
Peter Smith
Contributor
4604 Points
2092 Posts
Re: insert advertisement row in gridview
Jan 16, 2009 08:13 PM|LINK
Perhaps...could you paste the code you used for the inherited gridview? That would me a huge timesaver (for me ;)
Thanks!
blfoleyus
Participant
1129 Points
192 Posts
Re: insert advertisement row in gridview
Jan 16, 2009 11:04 PM|LINK
I'll do you one better. You can use this code here.
Dictionary<string, string> Items = new Dictionary<string, string>(); Items.Add("Blue", "Rabbit"); Items.Add("Red", "Elephant"); Items.Add("Green", "Tiger"); Items.Add("Purple", "Lion"); grd1.RowDataBound += new GridViewRowEventHandler(grd1_RowDataBound); grd1.DataSource = Items; grd1.DataBind(); GridViewRow gvr = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Normal); TableCell tc = new TableCell(); tc.Text = "Your Ad Content Here"; tc.ColumnSpan = grd1.Columns.Count; /* you'll need a column count, i know mine so it's hardcoded */ gvr.Cells.Add(tc); grd1.Controls[0].Controls.AddAt(grd1.Controls[0].Controls.Count / 2, gvr);It's much cleaner than the version and esentially does the same thing. you'll just need to make sure your column count is right (see comment) to ensure the columnspan gets set right.
Let me know if this helps.
Web Developer
http://www.blfoley.com
--------------------------
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
blfoleyus
Participant
1129 Points
192 Posts
Re: insert advertisement row in gridview
Jan 17, 2009 01:40 AM|LINK
oh yea, don't mind the RowDataBound Event, it's not needed.
Web Developer
http://www.blfoley.com
--------------------------
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
Peter Smith
Contributor
4604 Points
2092 Posts
Re: insert advertisement row in gridview
Jan 17, 2009 01:00 PM|LINK
hmm...I dont seem to be able to add the row every 5 rows...I now have this (just tried to insert it on specified index):
Protected Sub gvSearchResults_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvSearchResults.PreRender
Dim gvr As GridViewRow = New GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Normal)
Dim tc As TableCell = New TableCell
tc.Text = "Your Ad Content Here"
tc.ColumnSpan = 1
gvr.Cells.Add(tc)
'If gvSearchResults.Controls(0).Controls.Count Mod 5 = 0 Then
gvSearchResults.Controls(0).Controls.AddAt(2, gvr)
gvSearchResults.Controls(0).Controls.AddAt(3, gvr)
'End If
End Sub
What am i doing wrong?
blfoleyus
Participant
1129 Points
192 Posts
Re: insert advertisement row in gridview
Jan 18, 2009 02:58 PM|LINK
Can you move that code logic to Load and try it there. There may be some funny business going on in the gridview, but your code looks correct.
Web Developer
http://www.blfoley.com
--------------------------
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
Peter Smith
Contributor
4604 Points
2092 Posts
Re: insert advertisement row in gridview
Jan 18, 2009 03:50 PM|LINK
I now have this, but still no succes...the text is only shown after the first record in the gridview...?
Protected Sub gvSearchResults_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvSearchResults.Load
Dim gvr As GridViewRow = New GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Normal)
Dim tc As TableCell = New TableCell
tc.Text = "Your Ad Content Here"
tc.ColumnSpan = 1
gvr.Cells.Add(tc)
'If gvSearchResults.Controls(0).Controls.Count Mod 5 = 0 Then
gvSearchResults.Controls(0).Controls.AddAt(2, gvr)
gvSearchResults.Controls(0).Controls.AddAt(3, gvr)
'gvSearchResults.Controls(0).Controls.AddAt(15, gvr)
'AddAt((gvSearchResults.Controls(0).Controls.Count / 5), gvr)
'End If
End Sub