Create class under APP_Code: DynamicallyTemplatedGridViewHandler.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Specialized;
using System.Data.SqlClient;
public class DynamicallyTemplatedGridViewHandler : ITemplate
{
#region data memebers
ListItemType ItemType;
public string FieldName;
public string FieldHeader;
string InfoType;
#endregion
#region constructor
/// <summary>
///
/// </summary>
/// <param name="item_type">Item/EditItem</param>
/// <param name="field_name"></param>
/// <param name="info_type"></param>
public DynamicallyTemplatedGridViewHandler(ListItemType item_type, string field_name, string field_header, string info_type)
{
ItemType = item_type;
FieldName = field_name;
InfoType = info_type;
FieldHeader = field_header;
}
#endregion
#region Methods
public void InstantiateIn(System.Web.UI.Control Container)
{
switch (ItemType)
{
case ListItemType.Header:
Literal header_ltrl = new Literal();
header_ltrl.Text = "<b>" + FieldHeader + "</b>";
Container.Controls.Add(header_ltrl);
break;
case ListItemType.Item:
switch (InfoType)
{
case "Text":
TextBox txt = new TextBox();
txt.ID = FieldName;
txt.Text = String.Empty;
txt.Attributes["onkeydown"] = "return checkNumber(event)";
txt.Attributes["onkeyup"] = "return checkNumber(event)";
txt.Attributes["autocomplete"] = "off";
txt.DataBinding += new EventHandler(OnDataBinding);
Container.Controls.Add(txt);
break;
default:
Label field_lbl = new Label();
field_lbl.ID = FieldName;
field_lbl.Text = String.Empty;
field_lbl.DataBinding += new EventHandler(OnDataBinding);
Container.Controls.Add(field_lbl);
break;
}
break;
}
}
#endregion
#region Event Handlers
private void OnDataBinding(object sender, EventArgs e)
{
object bound_value_obj = null;
Control ctrl = (Control)sender;
IDataItemContainer data_item_container = (IDataItemContainer)ctrl.NamingContainer;
bound_value_obj = DataBinder.Eval(data_item_container.DataItem, FieldName);
switch (ItemType)
{
case ListItemType.Item:
if (sender is Label)
{
Label field_ltrl = (Label)sender;
field_ltrl.Text = bound_value_obj.ToString();
}
else if (sender is TextBox)
{
TextBox field_ltrl = (TextBox)sender;
field_ltrl.Text = bound_value_obj.ToString();
}
break;
}
}
#endregion
}
DEFAULT.ASPX
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>Untitled Page</title>
<script type="text/javascript">
function checkNumber(e) {
if (e.keyCode == 8 || e.keyCode == 9 || e.keyCode == 13 || e.keyCode == 46 || e.keyCode == 35 || e.keyCode == 36 || e.keyCode == 37 || e.keyCode == 39)
return true;
if ((e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode >= 96 && e.keyCode <= 105)) {
return true;
}
else
return false;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:Button runat="server" ID="btnSave" Text="Save" OnClick="btnSave_Click" />
<hr />
<div>
<asp:GridView ID="gvTable" runat="server" AutoGenerateColumns="False" CellPadding="4"
CellSpacing="4">
</asp:GridView>
</div>
</form>
</body>
</html>
DEFAULT.ASPX.CS
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Specialized;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
#region Data members
public DataTable dtTable = new DataTable();
#endregion
#region Events Handlers
protected void Page_Load(object sender, EventArgs e)
{
PopulateDataTable();
CreateTemplatedGridView();
}
public void btnSave_Click(object se, EventArgs e)
{
}
#endregion
#region Methods
void PopulateDataTable()
{
if (!IsPostBack)
{
gvTable.Columns.Clear();
string ServerName = "PCT37";
string UserName = "sa";
string Password = "tatva";
string DatabaseName = "BakerLand";
string TableName = "spOrders";
SqlConnection Connection = new System.Data.SqlClient.SqlConnection("Data Source=" + ServerName + ";Initial Catalog=" + DatabaseName + ";User ID=" + UserName + ";Password=" + Password + "; Connect Timeout=120;");
SqlDataAdapter adapter = new SqlDataAdapter(TableName, Connection);
adapter.SelectCommand.CommandType = CommandType.StoredProcedure;
adapter.SelectCommand.Parameters.Add("odate", SqlDbType.SmallDateTime).Value = DateTime.Now;
try
{
adapter.Fill(dtTable);
}
finally
{
Connection.Close();
}
Session["dtTable"] = dtTable;
}
else
{
dtTable = (DataTable)Session["dtTable"];
}
}
void CreateTemplatedGridView()
{
for (int i = 0; i < dtTable.Columns.Count; i++)
{
TemplateField itemField = new TemplateField();
// create HeaderTemplate
itemField.HeaderTemplate = new DynamicallyTemplatedGridViewHandler(ListItemType.Header,
dtTable.Columns[i].ColumnName,
GetFieldHeader(dtTable.Columns[i].ColumnName),
dtTable.Columns[i].DataType.Name);
if (i < 5)
{
// create ItemTemplate
itemField.ItemTemplate = new DynamicallyTemplatedGridViewHandler(ListItemType.Item,
dtTable.Columns[i].ColumnName,
GetFieldHeader(dtTable.Columns[i].ColumnName),
"");
}
else
{
// create ItemTemplate
string itemTitle;
string itemCode;
itemTitle = dtTable.Columns[i].ColumnName.Split("_".ToCharArray())[0];
itemCode = dtTable.Columns[i].ColumnName.Split("_".ToCharArray())[1];
itemField.ItemTemplate = new DynamicallyTemplatedGridViewHandler(ListItemType.Item,
dtTable.Columns[i].ColumnName,
GetFieldHeader(dtTable.Columns[i].ColumnName),
"Text");
}
// then add to tItemTmpFieldhe GridView
gvTable.Columns.Add(itemField);
}
// bind and display the data
gvTable.DataSource = dtTable;
gvTable.DataBind();
}
/// <summary>
/// Return Item 1 from Item 1_100001
/// </summary>
/// <param name="strFieldName"></param>
/// <returns></returns>
string GetFieldHeader(string strFieldName)
{
int ind = strFieldName.LastIndexOf('_');
if (ind >= 0)
return strFieldName.Remove(ind);
else
return strFieldName;
}
/// <summary>
/// Return Item 100001 from Item 1_100001
/// </summary>
/// <param name="strFieldName"></param>
/// <returns></returns>
string GetFieldCode(string strFieldName)
{
int ind = strFieldName.LastIndexOf('_');
if (ind >= 0)
return strFieldName.Remove(0, ind + 1);
else
return strFieldName;
}
#endregion
}
No comments:
Post a Comment