Groups | Blog | Home
all groups > asp.net datagrid control > november 2005 >

asp.net datagrid control : Customizing pager/header/footer


Tumurbaatar S.
11/4/2005 12:00:00 AM
I need to put web controls inside pager, header or footer
parts of the datagrid. Any ideas?

S.M. Altaf [MVP]
11/4/2005 12:00:00 AM
Hi,

In the ItemDataBound event of the datagrid, you can add the controls to the
datagrid header cells programmatically, or you can take a look at
HeaderTemplates.

Instead of giving you a link, I'll just give you the google search URL:

http://www.google.com/search?q=asp.net+datagrid+headertemplate&btnG=Search&hs=Gpg&hl=en&lr=&safe=off&client=firefox-a&rls=org.mozilla%3Aen-US%3Aofficial

HTH

-Altaf
--------------------------------------------------------------------------------
All that glitters has a high refractive index.
www.mendhak.com


[quoted text, click to view]

Tumurbaatar S.
11/8/2005 12:00:00 AM
Thank you!
I've added controls to my datagrid's footer and pager. And now I can
customize them at item's Create or DataBound events. But I cannot find
a method to access these controls after, for example, after postback.
DataGrid.Items contains only normal item rows. The header/footer/pagers
are not included in this collection. My datagrid has several controls
on the footer row and I do postback using some button. And within
this button's Click event handler I need to examine content of these
controls. Any ideas?
Also, in several articles about adding controls to the pager, for example,
I saw that before dynamically adding a control, authors check out whether
this control was added in prior round trips. They says that the datagrid
can save previously added control and without checking this, it may
lead to duplicate controls. In my code I don't check it and all work
normally: no duplicated/doubled controls. Anybody can explain this?



[quoted text, click to view]

S.M. Altaf [MVP]
11/8/2005 12:00:00 AM
That isn't very difficult.

Loop through the DataGridItemCollection. Start by looking for ItemType =
Header. In the header, you can go through each cell and do a .FindControl()
for the control that you have in your header (and assuming you have given
each control an ID). CTYpe() the control to the control type that you put
there, and then access the values that you want.

HTH
ALtaf
--------------------------------------------------------------------------------
All that glitters has a high refractive index.
www.mendhak.com


[quoted text, click to view]

Tumurbaatar S.
11/8/2005 12:00:00 AM
From MSDN/.NET Reference:
"...Note Only items bound to the data source are contained in the
Items collection. The header, footer, and separator are not included in
the collection....".

What collection do you mean?


[quoted text, click to view]

alvinz_c
11/9/2005 1:49:25 AM
Hi, Tumurbaatar. You are right. Only data-bound items are included in the
Items collection. For instance, to accessing LinkButton control in the

1. Header
DataGridItem item = (DataGridItem)DataGrid1.Controls[0].Controls[0];
LinkButton lbtnHeader = (LinkButton)item.FindControl("lbtnInHeader");


1. Footer
DataGridItem item =
(DataGridItem)DataGrid1.Controls[0].Controls[DataGrid1.Controls[0].Controls.Count-2];
LinkButton lbtnFooter = (LinkButton)item.FindControl("lbtnInFooter");


1. Pager
DataGridItem item =
(DataGridItem)DataGrid1.Controls[0].Controls[DataGrid1.Controls[0].Controls.Count-1];
LinkButton lbtnPager = (LinkButton)item.FindControl("lbtnInPager");


The DataGrid1.Controls[0] refers to the DataGridTable in the page hierarchy
control tree. Take note that, if the pager is implemented, the footer is the
second last DataGridItem control, otherwise I would be the last one, always.


Tumurbaatar S.
11/11/2005 12:00:00 AM
Got an error: Specified argument was out of the range of valid values.
Parameter name: index.

On the 1st Page_Load event my DataGrid is empty.
DataGrid1.Controls.Count returns 0, i.e. DataGrid1.Controls[0]
does not exist.
Why?
There's a DropDownList in my DataGrid's footer and I need
to populate list items from the Page_Load().
Referencing DataGrid1.Controls[0] after postback works:
no exception raised, items populated normally.


[quoted text, click to view]

AddThis Social Bookmark Button