Create Grid from database and databable ext.net
Databse Function::
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Diagnostics;
namespace iterna
{
public static class Utils
{
public static void Log(string text)
{
System.Diagnostics.Debug.WriteLine(" " + text);
}
}
public class DbCon
{
private SqlConnection con = null;
private string sqlConnStr = "";
private ConnectionState connectionState = ConnectionState.Closed;
///
///
///
public DbCon()using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Diagnostics;
namespace iterna
{
public static class Utils
{
public static void Log(string text)
{
System.Diagnostics.Debug.WriteLine(" " + text);
}
}
public class DbCon
{
private SqlConnection con = null;
private string sqlConnStr = "";
private ConnectionState connectionState = ConnectionState.Closed;
///
///
///
{
this.sqlConnStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
this.con = new SqlConnection(this.sqlConnStr);
this.con.StateChange += new StateChangeEventHandler(this.Connection_StateChange);
}
public SqlConnection SqlCon()
{
this.sqlConnStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
this.con = new SqlConnection(this.sqlConnStr);
return this.con;
}
///
///
///
/// Connection string zur DB
public DbCon(string connStr)
{
this.sqlConnStr = connStr;
this.con = new SqlConnection(this.sqlConnStr);
this.con.StateChange += new StateChangeEventHandler(this.Connection_StateChange);
}
///
/// Listener, der aufgerufen wird, wenn sich der DB-Verbindungstatus geändert hat
/// Speichert diesen in connState zwischen.
///
///
///
public void Connection_StateChange(object sender, StateChangeEventArgs e)
{
this.connectionState = e.CurrentState;
}
///
/// Erzeugt eine Instanz von SqlCommand mit der aktuellen DB-Verbindung
///
///
///
public SqlCommand SetCmd(string cmd)
{
// Falls die Verbindung geschlossen wurde, diese erneut herstellen
if ((this.connectionState == ConnectionState.Closed || con == null) && !(this.connectionState == ConnectionState.Connecting))
{
try
{
this.con.Open();
}
catch (Exception ex)
{
//System.ArgumentException argEx = new System.ArgumentException("Index is out of range", "index", ex);
//throw argEx;
//DfeUtils.Die(ex);
}
}
return new SqlCommand(cmd, this.con);
}
public SqlCommand SetCmd(string cmd, CommandType type)
{
SqlCommand c = this.SetCmd(cmd);
c.CommandType = type;
return c;
}
///
/// Schließt die verbindung zu Datenbank
///
public void CloseConnection()
{
if (this.connectionState == ConnectionState.Open)
{
this.con.Close();
}
}
///
/// Stellt die Verbindung zur Datenbank her, falls diese geschlossen ist
///
public void OpenConnection()
{
if (this.connectionState == ConnectionState.Closed)
{
this.con.Open();
}
}
}
}
DataTable
public DataTable Tabledata()
{
DataTable datatable = new DataTable();
DbCon db = new DbCon();
try
{
SqlConnection conn = db.SqlCon();
SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);
adapter.Fill(datatable);
}
catch (Exception _Exception)
{
//datatable.Columns.Add("Keine Daten vorhanden");
datatable.Columns.Add("Keine Daten vorhanden");
datatable.Rows.Add(0);
//Console.WriteLine(_Exception.Message);
}
finally
{
db.CloseConnection();
}
//this.DataSet = datatable;
return datatable;
}
public DataTable Tabledata()
{
DataTable datatable = new DataTable();
DbCon db = new DbCon();
try
{
SqlConnection conn = db.SqlCon();
SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);
adapter.Fill(datatable);
}
catch (Exception _Exception)
{
//datatable.Columns.Add("Keine Daten vorhanden");
datatable.Columns.Add("Keine Daten vorhanden");
datatable.Rows.Add(0);
//Console.WriteLine(_Exception.Message);
}
finally
{
db.CloseConnection();
}
//this.DataSet = datatable;
return datatable;
}
Create Grid::
public void CreateGrid(string title, string renderer, string level){
Model model = new Model();
foreach (DataColumn dc in this.DataSet.Columns)
{
ModelField modelField = new ModelField { Name = dc.ColumnName, Mapping = dc.ColumnName, Type = ModelFieldType.String };
model.Fields.Add(modelField);
}
Store store = new Store();
this.storeId = this.gridpnl.ID + "_store"; //create store id unique and set
store.ID = this.storeId; //set the store page Id
this.storePageSize = 20; //setting page Size if the paging is call
store.PageSize = this.storePageSize;
store.Model.Add(model);
store.DataSource = this.DataSet;
store.DataBind();
this.gridpnl.Store.Add(store);
this.storeName = store;
//Property of the gridPanel
//this.gridpnl.Title = title;
//this.gridpnl.Layout = "AnchorLayout";
//this.gridpnl.AnchorHorizontal = "100%";
this.gridpnl.Layout = Ext.Net.LayoutType.Fit.ToString();
this.gridpnl.Width = ((this.DataSet.Columns.Count * 55) + 113);
//this.gridpnl.Flex = 1;
//this.gridpnl.Padding = 20;
this.gridpnl.StyleSpec = "padding: 20px 0px 0px 0px;";
this.gridpnl.AnimCollapse = true;
//this.gridpnl.AutoHeight = true;
//this.gridpnl.Flex = 5;
this.gridpnl.IDMode = IDMode.Static;
this.gridpnl.ColumnModel.Sortable = true;
int index = 1;
//int l = 1;
foreach (DataColumn dc in this.DataSet.Columns)
{
Column nc = new Column();
/* for maintaining the first column without any lebel and the date format of the column*/
if (level == "level1" || level == "level3")
{
//for description column
string[] format = new string[] { "yyyy-MM-dd" };
DateTime datetime;
if (DateTime.TryParseExact(dc.ColumnName, format, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.NoCurrentDateDefault, out datetime))
nc.Text = datetime.ToString(@"yyyy-MMM", System.Globalization.CultureInfo.InvariantCulture);
else
nc.Text = dc.ColumnName;
nc.MaxHeight = 18;
nc.MinHeight = 17;
//l++;
}
else
{
nc.Text = dc.ColumnName.Replace("_", " ");
}
//nc.StyleSpec = "padding:2px 6px 7px 10px";
nc.DataIndex = dc.ColumnName;
if (index > this.descLength) nc.Flex = 1; index++;
//nc.Width = 70;
nc.Align = Ext.Net.Alignment.Right;
this.gridpnl.ColumnModel.Columns.Add(nc);
}
}
Comments
Post a Comment