<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridViewExample3.aspx.cs" Inherits="GridViews.GridViewExample3" %>

 

<!DOCTYPE html>

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

    <title>GridView Example 3</title>

</head>

<body bgcolor="#99ccff">

   

    <h3>This GridView example demonstrates the use of CommandFields, <br />

        setting the GridView for editing, and updating a datasource.</h3>

   

    <ul>

        <li>Column 1: BoundField column that is bound to the database field <b>ProductNumber</b>.</li>

        <li>Column 2: BoundField column that is bound to the database field <b>Description</b>.</li>

        <li>Column 3: BoundField column that is bound to the database field <b>Price</b>.</li>

        <li>Column 4: TemplateField column that has a textbox bound to the database field <b>QOH</b>.</li>

        <li>Column 5: TemplateField column that has a textbox used with the Update button to modify the QOH value for a record stored the database.</li>

        <li>Column 6: CommandField column with Select button. Codebehind displays the ProductNumber of the selected row.</li>

        <li>Column 7: CommandField column Edit button. Enables database update of QOH in Column 5.</li>

    </ul>

The DataField property is used to bind a database field to this column, which will display the database field’s value for each record (row) in the GridView.

 
        

    <form id="form1" runat="server">

  

        <asp:Label ID="lblDisplay" runat="server" ForeColor="#CC0000"></asp:Label>

        <br /> <br />

 

<asp:GridView ID="gvProducts" runat="server" AutoGenerateColumns="False" OnRowCancelingEdit="gvProducts_RowCancelingEdit"        

  OnRowEditing="gvProducts_RowEditing" OnSelectedIndexChanged="gvProducts_SelectedIndexChanged" OnRowUpdating="gvProducts_RowUpdating">

            <Columns>

                <asp:BoundField DataField="ProductNumber" HeaderText="Product ID" ReadOnly="true" />

                <asp:BoundField DataField="Description" HeaderText="Description" ReadOnly="true" />

                <asp:BoundField DataField="Price" DataFormatString="{0:$###,##0.00}" HeaderText="Price" />

                <asp:TemplateField HeaderText="QOH">

                    <ItemTemplate>

                        <asp:TextBox ID="txtQOH" runat="server" Height="20px" Width="64px"

Manually binds a database field to a the Text property of a control contained in this TemplateField column.

 
                            Text='<%# Bind("QOH") %>'></asp:TextBox>

                    </ItemTemplate>

                </asp:TemplateField>

                <asp:TemplateField HeaderText="Add Qty">

                    <ItemTemplate>

                        <asp:TextBox ID="txtQtyAdd" runat="server" Height="20px" Width="64px"></asp:TextBox>

                    </ItemTemplate>

                </asp:TemplateField>

                <asp:CommandField ButtonType="Button" HeaderText="Show Product"

                    ShowSelectButton="True"></asp:CommandField>

                <asp:CommandField ButtonType="Button" HeaderText="Edit Product"

                    ShowEditButton="True" />

            </Columns>

        </asp:GridView>

    </form>

 

</body>

</html>