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

Sunday, August 30, 2009

Change GridView row height programmatically in asp.net c#

How to set change GridView row height programmatically in asp.net
the following example demonstrate you how can we change the gridview data rows height. gridview represent tabular data. so it's data are displayed as table columns and rows. by default data rows height is not big. if we can set the row height more smaller or bigger then we need to change the gridview row style height property value. run this code in your favorite asp.net ide and click the button. you can see the gridview changes it's data rows height programmatically.
GridViewRowHeight.aspx
<%@ Page Language="C#" AutoEventWireup="true" %>

<!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.RowStyle.Height = 35;
    }    
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>How to set change GridView row height programmatically in asp.net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Navy; font-style:italic;">GridView Example: Change Row Height</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"
            BackColor="Brown"
            ForeColor="Snow"
            BorderColor="Orange"
            Font-Names="Comic Sans MS"
            AutoGenerateColumns="false"
            Width="500"
            >
            <PagerStyle 
                BackColor="Crimson" 
                ForeColor="Snow" 
                Height="35"
                Font-Size="Large" 
                HorizontalAlign="Right" 
                />
            <HeaderStyle 
                BackColor="SaddleBrown" 
                Font-Italic="false" 
                Height="35"
                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 />
        <asp:Button 
            ID="Button1" 
            runat="server"
            Text="Change Row Height"
            Font-Bold="true"
            Height="45"
            ForeColor="Crimson"
            OnClick="Button1_Click"
            />        
    </div>
    </form>
</body>
</html>
Related asp.net examples