I have a gridview which has three columns date, pnl, cumpnl I am able to add some formatting when applied to all cells using code such as
Protected Sub OnRowCreated(sender As Object, e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
For Each cell As TableCell In e.Row.Cells
cell.Font.Size = 10
cell.HorizontalAlign = HorizontalAlign.Center
Next
End If
End Sub
Within that For...Next loop how can I reference only the cells in the pnl and cumpnl columns? I don't see anyway to reference cells by the columnheader name or index.
Update:
by Using a RowDataBound event I can now reset the format of the values in the columns but the setting of the forecolor based on the cells value keeps erring ("Input string not in correct format"). Also, I am hardcoding the the column indexes. I need a way to dynamically get the column index based on the column header name
Protected Sub gvDataRetrieval_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvDataRetrieval.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
For i As Integer = 1 To 2
e.Row.Cells(i).Text = FormatCurrency(e.Row.Cells(i).Text, 2).ToString()
If Double.Parse(e.Row.Cells(i).Text) < 0 Then e.Row.Cells(i).ForeColor = Drawing.Color.Red
Next
End If
End Sub
Outside for each loop:
e.Row.Cells(index)
Eg. For adding header tooltip:
Protected Sub Grid1_RowCreated(ByVal sender As Object, ByVal e As GridRowEventArgs)
//setting row cells
If e.Row.RowType = DataControlRowType.DataRow Then
For i As Integer = 1 To e.Row.Cells.Count
If i = 1 Then
e.Row.Cells(i).Font.Size= 8
ELSE
e.Row.Cells(i).Font.Size= 12
END IF
NEXT
End If
End Sub
Kapil's code in C#
private void DataGrid1_RowCreated(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 1; i < e.Row.Cells.Count; i++)
{
if (i == 1) { e.Row.Cells[i-1].Font.Bold = true; } else { e.Row.Cells[i-1].Font.Bold = false; }
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.