C# Dynamic datagrid paging
Introduction:
In this article demonstrates,how to create dynamic paging using c#.
Main:
For ex,if we used scrollable datagrid,we need dynamic paging.Just Copy and try the belowcode,
Step -1: Create one scrollable datagrid,
<div id="Scrollablegrid" style="display:block;overflow:auto;width:980px;height:250px;">
<asp:Grid id="SampleGrid" runat="server" AutoGenerateColumn="true" AllowPaging="true" PageSize="20"/>
</div>
<asp:table id="pagingtable" width=100% runat="server">
<asp:TableRow>
<asp:TableCell id="Cell"></asp:TableCell>
</asp:TableRow>
</asp:Table>
<div id="Scrollablegrid" style="display:block;overflow:auto;width:980px;height:250px;"> <asp:Grid id="SampleGrid" runat="server" AutoGenerateColumn="true" AllowPaging="true" PageSize="20"/> </div> <asp:table id="pagingtable" width=100% runat="server"> <asp:TableRow> <asp:TableCell id="Cell"></asp:TableCell> </asp:TableRow> </asp:Table> |
Step -2: Bind the pager Properties into pager table using grid_itemcreated event,
private void SampleGrid_ItemCreated(Object sender,DataGridItemEventArgs e)
{
if(e.Item.ItemType == "Pager")
{
int var_i = new Int16();
pagingtable.Rows[0].Cells.Clear();
TableCell ocell = new TableCell();
oCell.ColumnSpan = 4;
oCell.Width = new Unit(800,UnitType.Pixel);
oCell.HorizondalAlign = HorizondalAlign.Left;
pagingtable.Rows[0].Cells.Add(oCell);
for (var_i=0; var_i<e.Item.Controls[0].Controls.Count;var_i++)
{
if (e.Item.Controls[0].HasControls())
{
if (e.Item.Controls[0].Controls[var_i].GetType().ToString() == "System.Web.UI.WebControls.DataGridLinkButton")
{
LinkButton ibutton = new LinkButton();
ibutton = (LinkButton) e.Item.Controls[0].Controls[var_i];
ibutton.Attributes.Add("href",Page.GetPostBackClientHyperlink(e.Item.Controls[0].Controls[var_i],""));
pagingtable.Rows[0].Cells[0].Controls.Add(ibutton);
}
else if (e.Item.Controls[0].Controls[var_i] is LiteralControl)
{
LiteralControl oControl = new LiteralControl();
oControl = (LiteralControl) e.Item.Controls[0].Controls[var_i];
pagingtable.Rows[0].Cells[0].Controls.Add(oControl.Text);
}
else if (e.Item.Controls[0].Controls[var_i] is Label)
{
Lable oControl = new Label();
oControl = (Label) e.Item.Controls[0].Controls[var_i];
pagingtable.Rows[0].Cells[0].Controls.Add(oControl.Text);
}
}
}
}
//For handling Postbacks
private void page_prerender(object sender,System.EventArgs e)
{
if(SampleGrid.HasControls())
{
foreach(DataGridItem dgitem in SampleGrid.Controls[0].Controls)
{
int var_i = new Int16();
for (var_i=0; var_i<dgitem.Item.Controls[0].Controls.Count;var_i++)
{
if (dgitem.Item.Controls[0].Controls[var_i].GetType().ToString() == "System.Web.UI.WebControls.DataGridLinkButton")
{
LinkButton ibutton = new LinkButton();
ibutton = (LinkButton) dgitem.Item.Controls[0].Controls[var_i];
ibutton.Attributes.Add("href",Page.GetPostBackClientHyperlink(dgitem.Item.Controls[0].Controls[var_i],""));
pagingtable.Rows[0].Cells[0].Controls.Add(ibutton);
}
}
}
}
}
private void SampleGrid_ItemCreated(Object sender,DataGridItemEventArgs e) { if(e.Item.ItemType == "Pager") { int var_i = new Int16(); pagingtable.Rows[0].Cells.Clear(); TableCell ocell = new TableCell(); oCell.ColumnSpan = 4; oCell.Width = new Unit(800,UnitType.Pixel); oCell.HorizondalAlign = HorizondalAlign.Left; pagingtable.Rows[0].Cells.Add(oCell); for (var_i=0; var_i<e.Item.Controls[0].Controls.Count;var_i++) { if (e.Item.Controls[0].HasControls()) { if (e.Item.Controls[0].Controls[var_i].GetType().ToString() == "System.Web.UI.WebControls.DataGridLinkButton") { LinkButton ibutton = new LinkButton(); ibutton = (LinkButton) e.Item.Controls[0].Controls[var_i]; ibutton.Attributes.Add("href",Page.GetPostBackClientHyperlink(e.Item.Controls[0].Controls[var_i],"")); pagingtable.Rows[0].Cells[0].Controls.Add(ibutton); } else if (e.Item.Controls[0].Controls[var_i] is LiteralControl) { LiteralControl oControl = new LiteralControl(); oControl = (LiteralControl) e.Item.Controls[0].Controls[var_i]; pagingtable.Rows[0].Cells[0].Controls.Add(oControl.Text); } else if (e.Item.Controls[0].Controls[var_i] is Label) { Lable oControl = new Label(); oControl = (Label) e.Item.Controls[0].Controls[var_i]; pagingtable.Rows[0].Cells[0].Controls.Add(oControl.Text); } } } } //For handling Postbacks private void page_prerender(object sender,System.EventArgs e) { if(SampleGrid.HasControls()) { foreach(DataGridItem dgitem in SampleGrid.Controls[0].Controls) { int var_i = new Int16(); for (var_i=0; var_i<dgitem.Item.Controls[0].Controls.Count;var_i++) { if (dgitem.Item.Controls[0].Controls[var_i].GetType().ToString() == "System.Web.UI.WebControls.DataGridLinkButton") { LinkButton ibutton = new LinkButton(); ibutton = (LinkButton) dgitem.Item.Controls[0].Controls[var_i]; ibutton.Attributes.Add("href",Page.GetPostBackClientHyperlink(dgitem.Item.Controls[0].Controls[var_i],"")); pagingtable.Rows[0].Cells[0].Controls.Add(ibutton); } } } } } |
Conclusion:
Hope this helps,
Happy Coding.
[...] original post here: Dynamic datagrid paging using c# | NetProgrammingHelp.com Share and [...]
Your post has been more than helpful. I just started a blog and I’m not getting any traffic at all. Can you help me out? Thanks Dave
This is a useful post, but I was wondering how do I suscribe to the RSS feed?
Great information… thanks for your review and tutorial about how to comment. I think if we walk to other blog we must comment to give appreciate for owner..
This is a very fascinating post, I was looking for this info. Just so you know I discovered your webpage when I was checking for blogs like mine, so please check out my site sometime and leave me a comment to let me know what you think.
Terrific posting, this is very similar to a site that I have. Please check it out sometime and feel free to leave me a comenet on it and tell me what you think. Im always looking for feedback.
Really good summary, this is very similar to a site that I have. Please check it out sometime and feel free to leave me a comenet on it and tell me what you think. Im always looking for feedback.
Im happy I found this blog, I couldnt obtain any info on this topic prior to. Also operate a website and if you wish to ever serious in a little bit of guest writing for me you should feel free to let me know, i’m always look for people to check out my blog page. Please stop by and leave a comment sometime!
Good article, this is very similar to a site that I have. Please check it out sometime and feel free to leave me a comenet on it and tell me what you think. I’m always looking for feedback.
Im glad I located this site, I couldnt get any knowledge on this topic before. Also manage a site and for anyone who is ever interested in doing a bit of guest writing for me make sure you feel free to let me know, im always look for people to check out my blog. Please stop by and leave a comment sometime!
I don’t agree with everything in this post, but you do make some very good points. I’m very interested in this subject and I myself do alot of research as well. Either way it was a well thoughtout and nice read so I figured I would leave you a comment. Feel free to check out my website sometime and let me know what you think.
Good site, where did you come up with the information in this article? Im happy I found it though, ill be checking back soon to see what other articles you have.
This is a terrific article, I located your website searching aol for a similar content and came to this. I couldnt come across to much different info on this piece of content, so it was wonderful to locate this one. I will be back to check out some other articles that you have another time.
I was browsing today, unsure of what I was looking for, but knew that I needed help with my finances, and your site just popped up. (Divine intervention, or what!)
IÕve been to your site half a dozen times now, and this time IÕm adding it to my bookmarks
ÊYour Êpages are always relevant, unlike the same-old stuff on other sites (which are coming off my bookmarks!) ÊTwo thumbs up!
How-do-you-do, just needed you to know I have added your site to my Google bookmarks because of your extraordinary blog layout. But seriously, I think your site has one of the freshest theme I’ve came across. It really helps make reading your blog a lot easier.
I’ve really enjoyed reading your articles. You obviously know what you are talking about! Your site is so easy to navigate too, I’ve bookmarked it in my favourites. . . . .
Thank you for the sensible critique. Me & my neighbour were preparing to do some research about that. We got a good book on that matter from our local library and most books where not as influensive as your information.
Have you ever considered adding more videos to your blog posts to keep the readers more entertained? I mean I just read through the entire article of yours and it was quite good but since I’m more of a visual learner,I found that to be more helpful well let me know how it turns out! I love what you guys are always up too. Such clever work and reporting! Keep up the great works guys I’ve added you guys to my blogroll. This is a great article thanks for sharing this informative information.. I will visit your blog regularly for some latest post.
It does seem that everybody is into this kind of stuff lately. Don’t really understand it though, but thanks for trying to explain it. Appreciate you shedding light into this matter. Keep it up
This is a wonderful article, I found your webpage searching google for a similar subject matter and arrived to this. I couldnt find to much different information on this blog post, so it was great to discover this one. I will probably be returning to look at some other posts that you have another time.
I have visited your posts before. The more I learn, the more I keep coming back! ;-P
Thank you for the sensible critique. Me and my neighbor were just preparing to do some research about this. We got a grab a book from our local library but I think I learned more from this post. I am very glad to see such great information being shared freely out there.
This is my second visit to this blog. We are starting a new initiative in the same niche as this blog. Your blog provided us with valuable information to work on. You have done a fantastic job.
Recently, I did not give whole lot thought to commenting on weblog articles and have left feedback even less. Viewing your useful piece of writing, might inspire me to do this again.
Have you ever considered adding more videos to your blog posts to keep the readers more entertained? I mean I just read through the entire article of yours and it was quite good but since I’m more of a visual learner,I found that to be more helpful well let me know how it turns out! I love what you guys are always up too. Such clever work and reporting! Keep up the great works guys I’ve added you guys to my blogroll. This is a great article thanks for sharing this informative information.. I will visit your blog regularly for some latest post.
Aw, this was a really quality post. In theory I’d like to write like this also – taking time and real effort to make a good article… but what can I say… I procrastinate alot and never seem to get anything done… Regards…
This is my first visit to your blog. We are starting a new initiative in the same niche as this blog. Your blog provided us with important information to work on. You have done a admirable job.
Intimately, the post is in reality the freshest topic on this registry related issue. I concur with your conclusions and will eagerly look forward to your forthcoming updates. Saying thanks will not just be enough, for the wonderful clarity in your writing. I will immediately grab your rss feed to stay abreast of any updates.
Thanx for the effort, keep up the good work Great work, I am going to start a small Blog Engine course work using your site I hope you enjoy blogging with the popular BlogEngine.net.Thethoughts you express are really awesome. Hope you will right some more posts.
If what I ultimately have to offer is half as much as what you now have to offer, I’ll be able to consider it a job well done.
This is a very useful post, I was looking for this knowledge. Just so you know I found your weblog when I was looking around for blogs like mine, so please check out my site sometime and leave me a comment to let me know what you think.
P thx for ur tips i’d love to follow u
Hey, awesome site! Keep it up! I will be difinatley be coming back in the near future =)
Thank you so much, I have been searching for some posts on this for weeks and you’ve got just what I want! It’s amazing how information and technology can get the best of us sometimes! Google rocks yea!
Its very informative and interesting article.all the points are very useful. Simple but very effective writing. Thanks for sharing such a nice post.
Very good article, very much, but there is little requirement is to make your web site can be a good link, in order to promote together.