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