Manipulation with cells, generated by GridView DataControlField derived classes.

Sometimes you need to customise DataControlField derived field (e.g. HyperLinkField or ButtonField )to show it differently then default behavior, but you don't want to create template for the field. If you customization requirements are compex, use TemplateField.
Below there is an example, how you can access cell, generated by  HyperLinkField and change content of the cell.
E.g  for HyperLinkField column you normally  want to show date as Hyperlink, but if date is in past,show text.  

protected void gvMyGVName_RowDataBound(object sender, GridViewRowEventArgs e)
{

 

    //The method is created as an example and  was never tested(and even compiled)

    if (e.Row.RowType == DataControlRowType.DataRow)

    {

        DataRowView drv = e.Row.DataItem as DataRowView;

        DataRow dataRow = drv.Row;

 

        if((DateTime)(dataRow["MyDateColumn"])<=DateTime.Now)

        {

            DataControlFieldCell cell = GridViewHelper.CellBySortExpression(e.Row, "MyDateColumn") as DataControlFieldCell;

            GridViewHelper.DataControlField_SetNullDisplayText(cell, "Expired");

        }

    }

 

}  

    //Function belongs to my GridViewHelper class.

 

        /// <summary>

        /// Hide link and add text control

        /// </summary>

        /// <param name="cell"></param>

        /// <param name="nullDisplayText"></param>

        public static void DataControlField_SetNullDisplayText(DataControlFieldCell cell, string nullDisplayText)

        {

 

        //TODO: implement derived from HyperLinkField class and set nullDisplayTex property

            //       TableCell cell = (TableCell) control;

            Debug.Assert((cell.ContainingField is HyperLinkField) || (cell.ContainingField is ButtonField));//

            if(cell.ContainingField is HyperLinkField)

            {

                if (((cell.Controls.Count < 1) || !(cell.Controls[0] is HyperLink)))

                {

                    throw new ApplicationException("HyperLinkField_WrongControlType"+ cell.ToString());

                }

                HyperLink link = (HyperLink)cell.Controls[0];

                link.Visible = false;

            }

            if(cell.ContainingField is ButtonField)

            {

                if (((cell.Controls.Count < 1) || !(cell.Controls[0] is LinkButton)))

                {

                    throw new ApplicationException("ButtonField_WrongControlType" + cell.ToString());

                }

                LinkButton link = (LinkButton)cell.Controls[0];

                link.Visible = false;

            }

            Label label = new Label();

            label.Text = nullDisplayText;

              label.Visible = true;

           cell.Controls.Add(label);

        }

«October»
SunMonTueWedThuFriSat
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678