we can use a CommandField to customize the appearance of the Edit, Delete, Update,
Cancel, and Select buttons displayed by the GridView control.
we do not enable the AutoGenerateEditButton or AutoGenerateDeleteButton properties
when using a CommandField. Instead, we use the CommandField to set up the standard
editing buttons explicitly.
The CommandField supports the following properties:
Sr.No |
Property |
Description |
1) |
ButtonType |
specify the type of button displayed by the
CommandField. Possible values are Button, Image, and Link. |
2) |
CancelImageUrl |
specify an image to display for the Cancel button. |
3) |
CancelText |
specify the text to display for the Cancel button |
4) |
CausesValidation |
disable validation when an Edit button is clicked |
5) |
DeleteImageUrl |
specify an image to display for the Delete button |
6) |
DeleteText |
specify the text to display for the Delete button |
7) |
EditImageUrl |
specify an image to display for the Edit button |
8) |
EditText |
specify the text to display for the Edit button |
9) |
InsertImageUrl |
specify an image to display for the Insert button |
10) |
InsertText |
specify the text to display for the Insert button |
11) |
NewImageUrl |
specify an image to display for the New button (does not apply to GridView) |
12) |
NewText |
specify the text to display for the New button |
13) |
SelectImageUrl |
specify the image to display for the Select button |
14) |
SelectText |
specify the text to display for the Select button |
15) |
ShowCancelButton |
display the Cancel button |
16) |
ShowDeleteButton |
display the Delete button |
17) |
ShowEditButton |
display the Edit button |
18) |
ShowInsertButton |
display the Insert button (does not apply to GridView) |
19) |
ShowSelectButton |
display the Select button |
20) |
UpdateImageUrl |
specify the image to display for the Update button |
21) |
UpdateText |
specify the text to display for the Update button |
22) |
ValidationGroup |
associate the edit buttons with a validation group |
Code: GridView13_CommandFields.aspx.cs
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridView13_CommandFields.aspx.cs"
Inherits="LearnAsp.Net.ControlDemo.GridView.GridView13_CommandFields" %>
<!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" DataKeyNames="empno" OnPageIndexChanging="grid_PageIndexChanging"
OnRowCancelingEdit="grid_RowCancelingEdit" OnRowDeleting="grid_RowDeleting" OnRowEditing="grid_RowEditing"
OnRowUpdating="grid_RowUpdating" ForeColor="#333333">
<Columns>
<asp:ButtonField CommandName="Select" Text="Select" />
<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:CommandField ShowEditButton="true" EditText="E" ShowDeleteButton="true"
DeleteText="D" UpdateText="U" CancelText="C" ControlStyle-Font-Bold="true"/>
</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: GridView13_CommandFields.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 GridView13_CommandFields : 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 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 = dtOfficerList.NewRow();
grid.DataBind();
int columncount = grid.Rows[0].Cells.Count;
grid.Rows[0].Cells.Clear();
grid.Rows[0].Cells.Add(new TableCell());
grid.Rows[0].Cells[0].ColumnSpan = columncount;
grid.Rows[0].Cells[0].Text = "No Records 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_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridViewRow row = (GridViewRow)grid.Rows[e.RowIndex];
//select * from testEmployee
string name = row.Cells[0].Text;
objConn.Open();
OracleCommand cmd = new OracleCommand("delete FROM testEmployee where empno='" + Convert.ToInt32(grid.DataKeys[e.RowIndex].Value.ToString()) + "'", objConn);
cmd.ExecuteNonQuery();
objConn.Close();
GridBind();
}
protected void grid_RowEditing(object sender, GridViewEditEventArgs e)
{
grid.EditIndex = e.NewEditIndex;
GridBind();
}
protected void grid_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int userid = Convert.ToInt32(grid.DataKeys[e.RowIndex].Value.ToString());
GridViewRow row = (GridViewRow)grid.Rows[e.RowIndex];
TextBox txtName = (TextBox)row.Cells[1].Controls[0];
TextBox txtJob = (TextBox)row.Cells[2].Controls[0];
string name = txtName.Text;// row.Cells[1].Text;
String job = txtJob.Text;//row.Cells[2].Text;
grid.EditIndex = -1;
objConn.Open();
OracleCommand cmd = new OracleCommand("update testEmployee set ename='" + name + "',job=" + job + " where empno='" + userid + "'", objConn);
cmd.ExecuteNonQuery();
objConn.Close();
GridBind();
//GridView1.DataBind();
}
protected void grid_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grid.PageIndex = e.NewPageIndex;
GridBind();
}
protected void grid_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
grid.EditIndex = -1;
GridBind();
}
}
}
Output:
No comments:
Post a Comment
Please do not enter any spam link in the comment box.