All about Gridview Complexity

Posted by Gayathri under ASP.NET on 4/25/2011 | Points: 10 | Views : 2306 | Status : [Member] | Replies : 5
1) When is the RowDataBound event of a gridview actually required?
I can even display the datas via template field directly. When is this RowDataBound actually needed.
2) Addnew, Edit ,Delete ,Update - i have these command fields in the gridview footer template and item template.
I want to hide the Addnew,Edit ,update ,delete to guest users in this page.
only the admin users should be able to see these links so that they can add contents (say list of books)
how is it possible?
3) How to bind an image control in the row databound event?
-Thanks
fast responses will be very much helpful.




Responses

Posted by: Ndebata on: 4/25/2011 [Member] Starter | Points: 25

Up
0
Down
1) When is the RowDataBound event of a gridview actually required?
I can even display the datas via template field directly. When is this RowDataBound actually needed.
Lets say you want to hide a partcular cell value or change the backgroud color or add an sort indicator image to the header dynamically you can use RowDataBound,
This event will be fired when databinding is done. In most of the static cases template is used.
2) Addnew, Edit ,Delete ,Update - i have these command fields in the gridview footer template and item template.
I want to hide the Addnew,Edit ,update ,delete to guest users in this page.
only the admin users should be able to see these links so that they can add contents (say list of books)
how is it possible?
In Page OnInt ,Check the user role and hide footer row or if any Cloumn you want to hide

//Hide a particular column
var actioncol = gvUser.Columns.Cast<DataControlField>().Where(c => c.HeaderText.Equals("<ColumnName>")).FirstOrDefault();
if (actioncol != null)
{
actioncol.Visible = false;
}


3) How to bind an image control in the row databound event? As i have mentioned you can add sort indicator image in the grid header
GridView gridView = (GridView)sender;
if (e.Row.RowType == DataControlRowType.Header)
{
if (this.ViewState[_SExpr] != null)
{
int cellIndex = -1;
foreach (DataControlField field in gridView.Columns)
{
if (field.SortExpression == Convert.ToString(this.ViewState[_SExpr]))
{
cellIndex = gridView.Columns.IndexOf(field);
}
}
if (cellIndex > -1)
{
Image sortImage = new Image();
if (this.ViewState[_SDirection] != null)
{
if (SortDirection.Ascending == (SortDirection)this.ViewState[_SDirection])
{
sortImage.ImageUrl = "~/Images/down.gif";
sortImage.AlternateText = "Ascending Order";
}
else
{
sortImage.ImageUrl = "~/Images/up.png";
sortImage.AlternateText = "Descending Order";
}
e.Row.Cells[cellIndex].Controls.Add(sortImage);
}
}
}
}


Gayathri, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Gayathri on: 4/26/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,
thanks for your reply. everything works fine. but still can you please be more clear on why is this row databound needed?
with row databound also i am able to display the datas from database using the template fields.
i need a small example if possible.

sorry to bug more.

Thanks


Gayathri, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Prabhakar on: 4/27/2011 [Member] [MVP] Starter | Points: 25

Up
0
Down
Hi gayathri

check it this link . . for Complete GridView.RowdataBound Event .

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx

Best Regard's
Prabhakar

Gayathri, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ndebata on: 4/27/2011 [Member] Starter | Points: 25

Up
0
Down
PFA Sample that explains the rowdatabound event.
 Download source file

Gayathri, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Gayathri on: 4/28/2011 [Member] Starter | Points: 25

Up
0
Down
Fabulous... Thanks a ton to Ndebata !!
and Thank you so much prabhakar.

Gayathri, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response