Me gustaría crear un control que amplíe el BoundField que se utiliza dentro de un GridView. Lo que me gustaría hacer es proporcionar otra propiedad llamada HighlightField que sea similar a la propiedad DataField en la que deseo darle el nombre de columna de datos. Dada esa columna de datos, vería si el valor es verdadero o falso y resaltará el texto dado dentro de la columna dada en la fila dada.Extender un (ASP.NET) BoundField
Algunos pseudo-código si eso no tiene sentido:
<asp:GridView id="grid">
<Columns>
<asp:BoundField DataField="Name" />
<cc:HighlightField DataField="Name" HighlightField="IsHighlighted" />
</Columns>
</asp:GridView>
Y luego dentro de la databind o algo:
if(this row's IsHighlighted value is true)
set the CssClass of this datacell = "highlighted"
(or wrap a span tag around the text)
Ravish me señaló en la dirección correcta, esto es lo que terminé con:
public class HighlightedBoundField : BoundField
{
public string HighlightField
{
get { return ViewState["HighlightField"].ToString(); }
set
{
ViewState["HighlightField"] = value;
OnFieldChanged();
}
}
public override void InitializeCell(DataControlFieldCell cell, DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
{
base.InitializeCell(cell, cellType, rowState, rowIndex);
bool isDataRowAndIsHighlightFieldSpecified = cellType == DataControlCellType.DataCell && !string.IsNullOrEmpty(HighlightField);
if (isDataRowAndIsHighlightFieldSpecified)
{
cell.DataBinding += new EventHandler(cell_DataBinding);
}
}
void cell_DataBinding(object sender, EventArgs e)
{
TableCell cell = (TableCell)sender;
object dataItem = DataBinder.GetDataItem(cell.NamingContainer);
cell.Text = DataBinder.GetPropertyValue(dataItem, DataField).ToString();
bool highlightThisCellsText = Convert.ToBoolean(DataBinder.GetPropertyValue(dataItem, HighlightField));
if (highlightThisCellsText)
{
cell.CssClass += " highlight";
}
}
}
Me gusta, le daré un giro y volveré – rball
¿No hay forma de hacer una unión de datos bidireccional? ¿Podría crear una propiedad booleana en su HighlightBoundField llamada "IsHighlighted" y hacer algo como esto: –
Chris