-->

02 February 2020

Asp.Net GridView CheckBoxFields 

  Asp.Net CS By Example       02 February 2020


 GridView CheckBoxFields 

 A CheckBoxField, as we can probably guess, displays a check box. When a row is not in edit mode, the check box displays but is disabled.

 The CheckBoxField inherits from the BoundField class, so it includes all the properties of the BoundField class.
It also supports the following property:
  1) . Text —Displays text next to each check box

 A CheckBoxField supports several other useful properties:

Sr.No Property Description
1) AccessibleHeaderText add an HTML abbr attribute to the column header
2) ApplyFormatInEditMode apply the DataFormatString to the field when the row is in edit display mode.
3) ConvertEmptyStringToNull convert an empty string ”” into the value Nothing (null) when editing a column
4) DataField specify the name of the field that the BoundField displays
5) DataFormatString use a format string to format a data item
6) FooterStyle format the column footer
7) FooterText display text in the column footer
8) HeaderImageUrl display an image in the column header
9) HeaderStyle format the column header
10) HeaderText display text in the column header.
11) HtmlEncode HTML-encode the value of a data item, which enables we to avoid script injection attacks.
12) InsertVisible not display a column when inserting a new record (does not apply to the GridView control).
13) ItemStyle fformat a data item
14) NullDisplayText Specify text displayed when a data item has the value Nothing (null).
15) ReadOnly Prevent the data item from being edited in edit mode
16) ShowHeader Display the column header
17) SortExpression Associate a sort expression with the column.
18) Visible hide or show a column.
19) Text Displays text next to each check box
  Code: GridView11_CheckBoxField .aspx.cs
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridView11_CheckBoxField .aspx.cs"
    Inherits="LearnAsp.Net.ControlDemo.GridView.GridView11_CheckBoxField" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">    
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h1>
            Employee Details</h1>
        <asp:GridView ID="grid" runat="server" AutoGenerateColumns="false" CellPadding="4"
            OnRowDataBound="grid_RowDataBound" ForeColor="#333333">
            <Columns>
                <asp:BoundField DataField="empno" HeaderText="EmpNo" />
                <asp:BoundField DataField="ename" HeaderText="Name" />
                <asp:BoundField DataField="job" HeaderText="desig" />
                <asp:BoundField DataField="sal" HeaderText="Salary" DataFormatString="{0:c}" />
                <asp:TemplateField HeaderText="Status" Visible="false">
                    <HeaderStyle Width="60px" HorizontalAlign="Center" />
                    <ItemTemplate>
                        <asp:Label ID="lblStatus" Text='<%#Eval("status")%>' runat="server" Visible="false" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Status">
                    <HeaderStyle Width="60px" HorizontalAlign="Center" />
                    <ItemTemplate>
                        <asp:CheckBox ID="ChkSelect" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <EditRowStyle BackColor="#999999" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        </asp:GridView>
        <br />
        <asp:Label ID="lblSelectRow" runat="server" />
    </div>
    </form>
</body>
</html>

  Code: GridView04.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OracleClient;
using System.Data;
using System.Configuration;

namespace LearnAsp.Net.ControlDemo.GridView
{
    public partial class GridView11_CheckBoxField : System.Web.UI.Page
    {
        static string strConn = ConfigurationManager.ConnectionStrings["LearnAsp"].ToString();
        private OracleConnection objConn = new OracleConnection(strConn);
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) { GridBind(); }
        }

        protected void GridBind()
        {
            String query = " select * from testEmployee ";

            OracleCommand objCmd;
            // Retrieve Details from the DB
            objCmd = new OracleCommand(query, objConn);
            DataTable dt = new DataTable();
            objConn.Open();
            OracleDataAdapter oda = new OracleDataAdapter(objCmd);
            oda.Fill(dt);
            objConn.Close();

            if (dt.Rows.Count > 0)
            {
                grid.DataSource = dt;
                grid.DataBind();
            }
            else
            {
                dt.Rows.Add(dt.NewRow());
                // Bind the DataTable which contain a blank row to the GridView
                grid.DataSource = dt;
                grid.DataBind();
                // Get the total number of columns in the GridView
                //to know what the Column Span should be
                int columnsCount = grid.Columns.Count;
                grid.Rows[0].Cells.Clear();// clear all the cells in the row
                grid.Rows[0].Cells.Add(new TableCell()); //add a new blank cell
                //set the column span to the new added cell
                grid.Rows[0].Cells[0].ColumnSpan = columnsCount;
                //You can set the styles here
                grid.Rows[0].Cells[0].HorizontalAlign = HorizontalAlign.Center;
                grid.Rows[0].Cells[0].ForeColor = System.Drawing.Color.Red;
                grid.Rows[0].Cells[0].Font.Bold = true;
                //set No Results found to the new added cell
                grid.Rows[0].Cells[0].Text = "NO RESULT FOUND!";
            }
        }

        protected void grid_SelectedIndexChanged(object sender, EventArgs e)
        {
            int selectedRowIndex;
            selectedRowIndex = grid.SelectedIndex;
            GridViewRow row = grid.Rows[selectedRowIndex];
            string name = row.Cells[1].Text;
            lblSelectRow.Text = "You selected " + name + ".";
        }

        protected void grid_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lbl = (Label)e.Row.FindControl("lblStatus");
                
                CheckBox chk = (CheckBox)e.Row.FindControl("ChkSelect");

                if (lbl.Text.ToString()=="Y")
                    chk.Checked = true;
            }
        }
    }
}
  Output:
logoblog

Thanks for reading Asp.Net GridView CheckBoxFields 

Previous
« Prev Post

No comments:

Post a Comment

Please do not enter any spam link in the comment box.