Home > Blockchain >  styling certain rows in my gridview asp.net
styling certain rows in my gridview asp.net

Time:11-21

so I have a gridview which is displaying a db table I have. I dont create the table manually obviously as this is what <%#Eval("text")%>' and Bind() do. I created a unique table with my sql query to add two rows of sum in the bottom of the table, ( 2 last records), my question is: is there a way in which I can access those rows to style them? I think its not possible but still Im asking maybe Ill find out that theres a way. thanks

CodePudding user response:

Yes, the need to style a individual cell (auto generated columns, or bound columns), or even style the whole row.

Let's do all 3

First up, style a "cell" (these are in the cells collection, and autogenerated columns, and NON templated columns).

So, say this simple grid

<asp:GridView ID="GridView1" runat="server"  Width="40%" 
    AutoGenerateColumns="False" DataKeyNames="ID"  CssClass="table table-hover">
    <Columns>
        <asp:BoundField DataField="FirstName" HeaderText="FirstName" />
        <asp:BoundField DataField="LastName" HeaderText="LastName" />
        <asp:BoundField DataField="HotelName" HeaderText="HotelName" />
        <asp:BoundField DataField="City" HeaderText="City" />
        <asp:TemplateField HeaderText="Description">
            <ItemTemplate>
                <asp:Label ID="lblDescript" runat="server"
                    Text='<%# Eval("Description")  %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Active" ItemStyle-HorizontalAlign="Center">
            <ItemTemplate>
                <asp:CheckBox ID="chkActive" runat="server"
                    Checked='<%# Eval("Active") %>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

(note how I included BOTH templated columns which have plain jane asp.net controls like label, textbox etc., or the built in GV columns (data bound ones, or auto generated)

Code to load the GV is this:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If Not IsPostBack Then
        LoadData()
    End If

End Sub


Sub LoadData()

    Dim strSQL = "SELECT * FROM tblHotelsA ORDER BY HotelName"
    Using conn As New SqlConnection(My.Settings.TEST4)
        Using cmdSQL As New SqlCommand(strSQL, conn)
            conn.Open()
            Dim rst As New DataTable
            rst.Load(cmdSQL.ExecuteReader)
            GridView1.DataSource = rst
            GridView1.DataBind()
        End Using
    End Using

End Sub

And we now have this:

enter image description here

Ok, so let's do example 1 - lets color the "city" Colum in above. Note how "city" is a "bound field".

So, auto generated, or bound fields = cells collection.

So, for things like conditional formatting, even combine some columns, change color, hide show images, or whatever?

you use the GV row databound event. (and this applies well to listview, repeaters, etc.)

So, lets say for city = Edmonton, we want a light blue cell, and for city = "Edmonton", we want some light green.

So, we use the row bound event.

This code:

Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs)

    If e.Row.RowType = DataControlRowType.DataRow Then

        Select Case e.Row.Cells(3).Text
            Case "Edmonton"
                e.Row.Cells(3).BackColor = System.Drawing.Color.SkyBlue
            Case "Banff"
                e.Row.Cells(3).BackColor = System.Drawing.Color.LightGreen
        End Select

    End If

End Sub

And we now have this:

enter image description here

However, the above for "templated" colums works a bit different.

Lets say we want to highligh the description when check box = true (checked).

In that case, we can't (don't) use the cells colleciton, but for templated controls, we use find control.

Like this:

        Dim cActive As CheckBox = e.Row.FindControl("chkActive")
        If cActive.Checked Then
            Dim lblDes As Label = e.Row.FindControl("lblDescript")
            lblDes.Font.Italic = True
            e.Row.Cells(4).BackColor = System.Drawing.Color.LightGray
        End If

NOTE how we EVEN set the font to italic in the label control.

And now we see this:

enter image description here

NOTE VERY close how we use findcontrol to get those templated controls.

Last but not least - bonus:

let's highlight the whole row for active, and NOT EVEN have the check box column in the GV (but it is in the database source).

So, say this markup for the columns:

    <Columns>
        <asp:BoundField DataField="FirstName" HeaderText="FirstName" />
        <asp:BoundField DataField="LastName" HeaderText="LastName" />
        <asp:BoundField DataField="HotelName" HeaderText="HotelName" />
        <asp:BoundField DataField="City" HeaderText="City" />
        <asp:TemplateField HeaderText="Description">
            <ItemTemplate>
                <asp:Label ID="lblDescript" runat="server" 
                    Text='<%# Eval("Description")  %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>

So, "active" check box is NOT EVEN displayed in the GV, (but, it is in the data source that feeds the gv).

So, we can do this:

Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs)

    If e.Row.RowType = DataControlRowType.DataRow Then

        Dim gData As DataRowView = e.Row.DataItem   ' Grid data - NOT the GV row!!!!!!

        If gData.Item("Active") = True Then

            e.Row.Style.Add("Background-color", "LightGray") ' highlight whole row.

        End If

    End If

End Sub

And we now get this:

enter image description here

Just keep in mind that the "dataitem" (the full database row) is ONLY avaiable during the row data bind, and it WILL/DOES go out of scope once data binding is complete.

CodePudding user response:

so basically using pretty much similar logics as albert's reply, I ended up catching two last rows manually and changing their colors.

    protected void grdReports_PreRender(object sender, EventArgs e)
    {
        //finding 2 last rows, changing backcolor to the sum specified color '#5D7B9D' and the forecolor to white

        //first table ( annual ) 
        GridViewRow lastRow = grdReports.Rows[grdReports.Rows.Count - 1];
        lastRow.BackColor = System.Drawing.Color.FromName("#5D7B9D");
        lastRow.ForeColor = Color.White;
        GridViewRow secondToLastRow = grdReports.Rows[grdReports.Rows.Count - 2];          
        secondToLastRow.BackColor = System.Drawing.Color.FromName("#5D7B9D");
        secondToLastRow.ForeColor = Color.White;

        //second table ( requested )
        GridViewRow lastRowSecond = grdReports2.Rows[grdReports2.Rows.Count - 1];
        lastRowSecond.BackColor = System.Drawing.Color.FromName("#5D7B9D");
        lastRowSecond.ForeColor = Color.White;
        GridViewRow secondToLastRowSecond = grdReports2.Rows[grdReports2.Rows.Count - 2];
        secondToLastRowSecond.BackColor = System.Drawing.Color.FromName("#5D7B9D");
        secondToLastRowSecond.ForeColor = Color.White;
    }
  • Related