A BoundField always displays the value of a data item as text when a row is in normal
display mode. When a row is selected for editing, a BoundField displays the value of a
data item in a single-line text field.
The most important three properties of the BoundField class are the
1) DataField
2) DataFormatString
3) HeaderText
The GridView control includes an AutoGenerateColumns property assigned the value
False. If we don’t disable automatically generated columns, both columns represented by
the BoundFields and all the columns from the data source display redundantly
A BoundField 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. |
Code: GridView04.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridView04.aspx.cs"
Inherits="LearnAsp.Net.ControlDemo.GridView.GridView04" %>
<!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" OnSelectedIndexChanged="grid_SelectedIndexChanged"
ForeColor="#333333">
<Columns>
<asp:BoundField DataField="empno" HeaderText="Employee ID" />
<asp:BoundField DataField="ename" HeaderText="Name" />
<asp:BoundField DataField="job" HeaderText="Designation" />
<asp:BoundField DataField="sal" HeaderText="Salary" />
<asp:ButtonField CommandName="Select" Text="Select" />
</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 GridView04 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
GridBind();
}
protected void GridBind()
{
String query = " select * from emp ";
OracleConnection objConn;
OracleCommand objCmd;
// Retrieve Details from the DB
string strConn = ConfigurationManager.ConnectionStrings["LearnAsp"].ToString();
objConn = new OracleConnection(strConn);
objCmd = new OracleCommand(query, objConn);
DataTable dtOfficerList = new DataTable();
objConn.Open();
OracleDataAdapter oda = new OracleDataAdapter(objCmd);
oda.Fill(dtOfficerList);
objConn.Close();
if (dtOfficerList.Rows.Count > 0)
{
grid.DataSource = dtOfficerList;
grid.DataBind();
}
else
{
grid.DataSource = null;
grid.DataBind();
}
}
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 + ".";
}
}
}
Output:
No comments:
Post a Comment
Please do not enter any spam link in the comment box.