Click here to Skip to main content
16,016,157 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi frnds 

In grid view how to bold top 3 rows font ..please help me i have tried like this

C#
protected void grdOlymp_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[1].Font.Bold = true;
        }

if i tried like this the total 1st column getting bold ,but i need to bold top 3 rows  please give me some solution guys 

thank u
Posted
Comments
Sinisa Hajnal 22-Sep-14 6:11am    
Do you need only one column or the whole row?
u mani 22-Sep-14 6:15am    
Dear Hajnal, i need to bold whole row not a column . .
thans for reply ..

Check less-then function from jQuery[^]

There is also pseudo-selector in CSS:


You would use it like this (for full row select) in your CSS file:
CSS
your-grid-selector tr:lt(3) { font-weight: bold; }


if you need only one column then add :nth-child(column-index) to the selector above.
CSS
your-grid-selector tr:lt(3):nth-child(1){ font-weight: bold; }




If you absolutely have to have it server side (bad idea):
Check this event[^]


Note that this will change only second column, for the whole row you have to remove Cells part of the line :)
C#
protected void grdOlymp_DataBindingComplete (object sender, DataGridViewBindingCompleteEventArgse)
    {
        for (int i=0; i <= 2; i++)
            grdOlymp.Rows[i].Cells[1].Font.Bold = true;

        }



If this helps, please take time to accept the solution so that others may find it. Thank you.
 
Share this answer
 
v3
Comments
u mani 22-Sep-14 7:01am    
In asp.net Where to add this code
Sinisa Hajnal 22-Sep-14 7:17am    
You add it in DataBindingComplete event if you absolutely have to have it server side...but it is unnecessary. Just put some CSS and be done with it. You can then change the style whenever your users change their mind instead of having to recompile the application.

I've updated the solution for server side
Sinisa Hajnal 22-Sep-14 7:21am    
Ah, I understsand now: you meant where to add the code from my original solution...you add it in your CSS file, sorry, I should have explained it better.
Use the following Code to make the conditional formatting of first three rows:
 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) {   
    for (int i=0;i<3;i++)
    {
        if(e.Row.RowType == DataControlRowType.DataRow)  
        {
                e.Row.ForeColor = System.Drawing.Color.Crimson;  
                e.Row.Font.Italic = true;  
                e.Row.BackColor = System.Drawing.Color.LightPink;  
              
        }  
    }
}  
 
Share this answer
 
Comments
u mani 22-Sep-14 6:59am    
not working ,,
thanks for reply
Sinisa Hajnal 22-Sep-14 7:14am    
Not a good solution as you would loop through three rows on every row bound event. That is, if you have 50 rows you'll go fifty times three through the loop and even worse is that the loop is not used in the context and again uses e.Row instead of grid.rows(i)...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900