User Tools

Site Tools


notes:asp.net:gridview

ASP.NET GridView

Example: A simple GridView:

<asp:GridView id="GridView1" AutoGenerateColumns="False" CellPadding="5" runat="server">
    <AlternatingRowStyle BackColor="Gainsboro" />
    <HeaderStyle ForeColor="White" BackColor="Navy"/>
    <Columns>
        <asp:BoundField DataField="FirstName" HeaderText="First name" />
        <asp:BoundField DataField="LastName" HeaderText="Last name" />
    </Columns>
</asp:GridView>
protected void Page_Load(object sender, EventArgs e)
{
    BindGrid();
}
 
private void BindGrid()
{
    var people = new[]
    {
        new { FirstName = "Michael", LastName = "Davis" },
        new { FirstName = "Mike", LastName = "Johnson" },
        new { FirstName = "John", LastName = "Smith" },
        new { FirstName = "Leon", LastName = "McFerry" },
        new { FirstName = "Wanda", LastName = "Chotomska" },
    };
 
    GridView1.DataSource = people;
    GridView1.DataBind();
}

Example: A GridView with a hyperlink column:

<asp:GridView id="GridView1" AutoGenerateColumns="False" CellPadding="5" runat="server">
    <AlternatingRowStyle BackColor="Gainsboro"/>
    <Columns>
        <asp:HyperLinkField HeaderText="ID" DataTextField="ID"
            DataNavigateUrlFields="ID" DataNavigateUrlFormatString="details.aspx?ID={0}" />
        <asp:BoundField DataField="Product" HeaderText="Product" />
    </Columns>
</asp:GridView>
protected void Page_Load(object sender, EventArgs e)
{
    BindGrid();
}
 
private void BindGrid()
{
    var products = new[]
    {
        new { ID = 100, Product = "Umbrella" },
        new { ID = 101, Product = "Table" },
        new { ID = 102, Product = "Chair" },
        new { ID = 103, Product = "Lamp" },
        new { ID = 104, Product = "Bookcase" },
    };
 
    GridView1.DataSource = products;
    GridView1.DataBind();
}

Example: A GridView with a template column:

<asp:GridView id="GridView1" AutoGenerateColumns="False" CellPadding="5" runat="server">
    <AlternatingRowStyle BackColor="Gainsboro" />
    <HeaderStyle ForeColor="White" BackColor="Navy"/>
    <Columns>
        <asp:TemplateField HeaderText="Name">
            <ItemTemplate>
                <asp:Label ID="lblFirstName" Text='<%# Eval("FirstName") %>' runat="server" />
                <asp:Label ID="lblLastName" Text='<%# Eval("LastName") %>' runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
protected void Page_Load(object sender, EventArgs e)
{
    BindGrid();
}
 
private void BindGrid()
{
    var people = new[]
    {
        new { FirstName = "Michael", LastName = "Davis" },
        new { FirstName = "Mike", LastName = "Johnson" },
        new { FirstName = "John", LastName = "Smith" },
    };
 
    GridView1.DataSource = people;
    GridView1.DataBind();
}

Example: A GridView populated from an ObjectDataSource:

<asp:GridView id="GridView1" AutoGenerateColumns="False" CellPadding="5" 
    DataSourceID="PeopleDataSource" runat="server">
    <AlternatingRowStyle BackColor="Gainsboro" />
    <HeaderStyle ForeColor="White" BackColor="Navy"/>
    <Columns>
        <asp:BoundField DataField="FirstName" HeaderText="First name" />
        <asp:BoundField DataField="LastName" HeaderText="Last name" />
    </Columns>
</asp:GridView>
 
<asp:ObjectDataSource ID="PeopleDataSource" SelectMethod="GetPeople" TypeName="DataLayer.People" runat="server" />
namespace DataLayer
{
    // ...
 
    public class People
    {
        public object[] GetPeople()
        {
            var people = new[]
            {
                new { FirstName = "Michael", LastName = "Davis" },
                new { FirstName = "Mike", LastName = "Johnson" },
                new { FirstName = "John", LastName = "Smith" },
            };
 
            return people;
        }
    }
}

Example: A GridView with a RowDataBound event handler:

<asp:GridView id="GridView1" AutoGenerateColumns="False" CellPadding="5" 
    OnRowDataBound="GridView1_RowDataBound" runat="server">
    <HeaderStyle ForeColor="White" BackColor="Navy"/>
    <Columns>
        <asp:BoundField DataField="ProductName" HeaderText="Product name" />
    </Columns>
</asp:GridView>
protected void Page_Load(object sender, EventArgs e)
{
    BindGrid();
}
 
private void BindGrid()
{
    var products = new[]
    {
        new Product { ProductName = "Umbrella", ProductType = 1 },
        new Product { ProductName = "Table", ProductType = 2 },
        new Product { ProductName = "Chair", ProductType = 2 },
        new Product { ProductName = "Lamp", ProductType = 3 },
        new Product { ProductName = "Bookcase", ProductType = 2 },
    };
 
    GridView1.DataSource = products;
    GridView1.DataBind();
}
 
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    GridViewRow row = e.Row;
 
    if (row.RowType == DataControlRowType.DataRow)
    {
        Product item = row.DataItem as Product;
        int type = item.ProductType;
 
        switch (type)
        {
            case 1:
                row.ForeColor = System.Drawing.Color.Blue;
                break;
            case 2:
                row.ForeColor = System.Drawing.Color.Gray;
                break;
            case 3:
                row.ForeColor = System.Drawing.Color.Green;
                break;
        }
    }
}
 
// ...
 
public class Product
{
    public string ProductName { get; set; }
    public int ProductType { get; set; }
}

Example: A GridView with a RowCommand event handler and a data key:

<asp:GridView id="GridView1" AutoGenerateColumns="False" CellPadding="5" 
    OnRowCommand="GridView1_RowCommand"
    DataKeyNames="ID" 
    EmptyDataText="There are no products to display."
    runat="server">
    <HeaderStyle ForeColor="White" BackColor="Navy"/>
    <Columns>
        <asp:BoundField DataField="Product" HeaderText="Product" />
        <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton ID="btnDelete" Text="Delete" CausesValidation="False"
                    CommandName="DeleteProduct" CommandArgument='<%# Container.DisplayIndex %>'
                    OnClientClick="return confirm('Are you sure you want to delete this record?')" 
                    runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
 
    </Columns>
</asp:GridView>
protected void Page_Load(object sender, EventArgs e)
{
    BindGrid();
}
 
private void BindGrid()
{
    var products = new[]
    {
        new { ID = 100, Product = "Umbrella" },
        new { ID = 101, Product = "Table" },
        new { ID = 102, Product = "Chair" },
    };
 
    GridView1.DataSource = products;
    GridView1.DataBind();
}
 
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    string cmd = e.CommandName;
    string arg = e.CommandArgument.ToString();
 
    if (cmd == "DeleteProduct")
    {
        // Get the id of the selected contact.
        GridView grid = (GridView)sender;
        int index = Int32.Parse(arg);
        int id = (int)grid.DataKeys[index]["ID"];
 
        // ... delete the item with the id from a data store
    }
}
notes/asp.net/gridview.txt · Last modified: 2016/04/14 by admin