Saturday, 16 March 2013

GridView: Properties and Method

GridView Properties:

<asp:GridView ID="studentListGridView" runat="server"
    AutoGenerateColumns="False"
    AllowPaging="True"
    EmptyDataText="No record found"
    onpageindexchanging="studentListGridView_PageIndexChanging"
    DataKeyNames="STUD_AdmissionNumber"
    EnablePersistedSelection="True" 
    PageSize="15"
    ondatabound="CustomersGridView_DataBound" 
    onrowcancelingedit="grid_RowCancelingEdit" 
    onrowediting="grid_RowEditing" 
    onrowupdating="grid_RowUpdating" 

>

<asp:GridView ID="studentListGridView" runat="server"
    AutoGenerateColumns="False"
    AllowPaging="True"
    EmptyDataText="No record found"
    onpageindexchanging="studentListGridView_PageIndexChanging"
    DataKeyNames="STUD_AdmissionNumber"
    EnablePersistedSelection="True" PageSize="15">
    <Columns>
        <asp:BoundField DataField="STUD_ID" HeaderText="STUD_ID"
            SortExpression="STUD_ID" Visible="False" />
        <asp:BoundField DataField="STUD_AdmissionNumber" HeaderText="Admission Number"
            SortExpression="STUD_AdmissionNumber" />
        <asp:BoundField DataField="STUD_Surname" HeaderText="Surname"
            SortExpression="STUD_Surname" />
      <asp:BoundField DataField="STUD_Forename" HeaderText="Forename" 
            SortExpression="STUD_Forename" ReadOnly="True" />
        <asp:BoundField DataField="STUD_Called" HeaderText="Called"
            SortExpression="STUD_Called" />
        <asp:CheckBoxField DataField="STUD_Induction" HeaderText="Induction"
            SortExpression="STUD_Induction" />

        <asp:ImageField DataImageUrlField="Image1FileName" 
            DataImageUrlFormatString="../ImageFolders/ProductImages/{0}"    
            HeaderText="ProductImage" ReadOnly="True">
        </asp:ImageField>

        <asp:TemplateField HeaderText="Price" SortExpression="Price">
            <EditItemTemplate>
                <asp:TextBox ID="priceTextBox" runat="server" 
                    Text='<%#String.Format("{0:0.00}",Eval("Price"))%>' Width="45px">  
                </asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label3" runat="server" Text='<%#String.Format("
                  {0:0.00}",Eval("Price"))%>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField>
            <ItemTemplate>
                <asp:HyperLink ID="HyperLink1" runat="server"
                NavigateUrl='<%# "../CatalogAdmin.aspx?DepartmentID=" +
                    Request.QueryString["DepartmentID"] + "&amp;CategoryID=" +
                    Request.QueryString["CategoryID"] + "&amp;ProductID=" +
                    Eval("ProductID") %>'>Select</asp:HyperLink>
            </ItemTemplate>
        </asp:TemplateField>

        <asp:templatefield>
             <itemtemplate>
                <%# Convert.ToBoolean(Eval("MyBitField")) ? "Yes" : "No" %>
             </itemtemplate>
             <ItemTemplate>
              <asp:Label ID="Label1" runat="server" Text='<%# Convert.ToBoolean(Eval                         
                   ("Monday"))?"Yes":""%>'></asp:Label>
             </ItemTemplate>
        </asp:templatefield>  
        <asp:TemplateField HeaderText="AdmissionNumber" 
            SortExpression="AdmissionNumber">
            <EditItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Eval("AdmissionNumber") %>' 
                   Width="20" Height="15"></asp:Label>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("AdmissionNumber") %>' 
                ></asp:Label>
            </ItemTemplate>
        </asp:TemplateField> 
        <asp:HyperLinkField DataNavigateUrlFormatString="" 
            DataTextField="Link" HeaderText="Website Link" DataNavigateUrlFields="Link" 
            Target="_blank" />
        <asp:boundfield datafield="Your_Date_Column" dataformatstring="{0:MMMM d, yyyy}"
              <asp:CommandField ShowEditButton="True" />

        <asp:CheckBoxField DataField="STUD_Induction" HeaderText="Induction"
            SortExpression="STUD_Induction" />
    </Columns>
    <PagerSettings FirstPageText="First page"
        LastPageText="Last page" 
        PreviousPageText="Previous" 
        NextPageText="Next"
        Position="TopAndBottom" 
        PageButtonCount="20"/>
    <PagerStyle BackColor="#6699FF" />
</asp:GridView>



GridView Method:

GridView.DataSource
GridView.DataBind();
GridView.PageIndex = e.NewPageIndex //set the current page of GridView to New Page
GridViewRow pagerRow = CustomersGridView.BottomPagerRow //get the pager row 
DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList")
Label pageLabel = (Label)pagerRow.Cells[0].FindControl("CurrentPageLabel");
grid.EditIndex = e.NewEditIndex; //set the row for which to enable edit mode
MaintainScrollPositionOnPostback="true"//add this in the Page_Load() function
string id = grid.DataKeys[e.RowIndex].Value.ToString();//get the current product id
string name = ((TextBox)grid.Rows[e.RowIndex].FindControl("nameTextBox")).Text;
bool success = CatalogAccess.UpdateProduct(id, name, description)//execute the update command
grid.EditIndex = -1;//cancel edit mode

//find the checkbox control
string chkInduction = ((CheckBox)studentListGridView.Rows[e.RowIndex].Cells[5].Controls[0]).Checked.ToString();

Note:
Checkbox Within EditItemTemplate Of GridView Column:
<asp:TemplateField HeaderText="Circuit Training2"
            SortExpression="CircuitTraining">
            <EditItemTemplate>
                <asp:CheckBox ID="CheckBox1" runat="server"
                    Checked='<%# Eval("CircuitTraining").ToString().Equals("Yes") %>' />
            </EditItemTemplate>
            <ItemTemplate>
                <asp:CheckBox ID="CheckBox1" runat="server"
                    Checked='<%# Eval("CircuitTraining").ToString().Equals("Yes") %>' Enabled="false" />
            </ItemTemplate>
        </asp:TemplateField>










No comments:

Post a Comment