GridView 内で使用される BoundField を拡張するコントロールを作成したいと考えています。私がやりたいのは、名前にデータ列を付けたいという点で、DataField プロパティに似た HighlightField という名前の別のプロパティを提供することです。そのデータ列を指定すると、値が true か false かを確認し、指定された行の指定された列内の指定されたテキストを強調表示します。
それが意味をなさない場合の擬似コード:
<asp:GridView id="grid">
<Columns>
<asp:BoundField DataField="Name" />
<cc:HighlightField DataField="Name" HighlightField="IsHighlighted" />
</Columns>
</asp:GridView>
そして、データバインドか何かの中で:
if(this row's IsHighlighted value is true)
set the CssClass of this datacell = "highlighted"
(or wrap a span tag around the text)
Ravish は正しい方向を示してくれました。
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";
}
}
}