asp.net c# tutorials and examples code for web developers

How to change GridView selected row border color

GridView selected row border color
the following asp.net c# example code demonstrate us how can we set or change the gridview selected row border color programmatically.

gridview SelectedRow property get a reference to a GridViewRow object that represent the selected row in the control. gridview SelectedRowStyle property get a reference to the TableItemStyle object that enable us to set the appearance of the selected row in a gridview server control.

gridview SelectedRowStyle property can be set programmatically in the form Property.Subproperty. so if we want to change the border color of selected row then we need to set a value for SelectedRowStyle property's BorderColor Subproperty as SelectedRowStyle.BorderColor.

this is a simple way to programmatically assign any valid color for gridview server control's selected row border.
GridViewSelectedRowBorderColor.aspx
<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System.Drawing" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        GridView1.SelectedRowStyle.BorderColor = Color.Crimson;
    }    
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>How to set change GridView selected row border color programmatically in asp.net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Navy; font-style:italic;">GridView Example: Change Selected Row Border Color</h2>
        <asp:SqlDataSource 
            ID="SqlDataSource2"
            runat="server"
            ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
            SelectCommand="Select ProductID, ProductName, UnitPrice From Products"
            CancelSelectOnNullParameter="false"
            >
        </asp:SqlDataSource>
        <asp:GridView 
            ID="GridView1"
            runat="server"
            DataSourceID="SqlDataSource2"
            AllowPaging="true"
            Width="525"
            ForeColor="Snow"
            BackColor="OrangeRed"
            BorderColor="Orange"
            AutoGenerateColumns="false"
            Font-Names="Comic Sans MS"
            AutoGenerateSelectButton="true"
            >
            <SelectedRowStyle 
                BackColor="Orange"
                BorderWidth="2"
                />
            <PagerStyle 
                ForeColor="AliceBlue" 
                Height="40"
                BackColor="DodgerBlue"
                Font-Size="Large"
                VerticalAlign="Top"
                HorizontalAlign="Left"
                />
            <HeaderStyle 
                BackColor="DodgerBlue"
                BorderColor="CornflowerBlue" 
                Height="35"
                Font-Italic="false" 
                ForeColor="Snow" 
                />
            <Columns>
                <asp:BoundField DataField="ProductID" HeaderText="Product ID" ReadOnly="true" />
                <asp:BoundField DataField="Productname" HeaderText="Product Name" />
                <asp:BoundField DataField="UnitPrice" HeaderText="Unit Price" />
            </Columns>        
        </asp:GridView>
        <br /><br />
        <asp:Button 
            ID="Button1" 
            runat="server"
            Text="Change Selected Row Border Color"
            Font-Bold="true"
            Height="45"
            ForeColor="Crimson"
            OnClick="Button1_Click"
            />        
    </div>
    </form>
</body>
</html>








Related asp.net example