Create DataGrid b> div> with code
form>
body>
Html>
CreateDataGrid.aspx.cs
Using system;
Using system.configuration;
Using system.data;
Using system.data.sqlclient;
Using system.Web;
Using system.Web.ui;
Using system.Web.ui.webcontrols;
Using system.Web.ui.htmlcontrols;
Using system.drawing;
///
Summary description of /// CreatedataGrid.
/// summary>
Namespace aspxwebcs
{
Public Class MyDataGrid: Page
{
Public String SQL = "SELECT Firstname, LastName, Homephone, Title from Employees"; public dataGrid mygrid = new datagrid ();
Public String Sortexpression;
///
/// Create a template column and a column template
/// summary>
Public templateColumnTM = new templateColumn ();
Public columntemplate mycol = new columnTemplate ();
/ / Return to DataView
Public DataView CreateDataSource ()
{
String strsql;
STRSQL = "Data Source = .; initial catalog = northwind; user ID = sa; password =;";
SqlConnection Conn = New SqlConnection (strsql);
SqlDataAdapter DB_SQLADAPTOR = New SqlDataAdapter (SQL, CONN);
DataSet DS = New Dataset ();
DB_SQLADAPTOR.FILL (DS, "EMPLOYEES");
DataView myview = ds.tables ["employees"]. Defaultview;
//myview.sort=sortexpression;
//Response.write(SQL);
Return myView;
}
///
/// Treatment Sort
/// summary>
/// param>
/// param>
Public void sort_grid (Object Sender, DataGridSortCommandeventArgs E)
{
Sortexpression = E.Sortexpression.toString ();
Session ["Sortfield"] = sortexpression.trim ();
IF (session "== null) session [" Order "] =" ASC ";
Session ["Order"] = (session ["ORDER"]. TOSTRING () == "DESC")? "ASC": "DESC";
IF (session ["sortfield"] == null) session ["sortfield"] = "firstname";
SQL = "Order by" session ["sortfield"]. Tostring () "" session "[" Order "]. TOSTRING ();
MyGrid.datasource = createdataSource ();
MyGrid.databind ();
}
///
/// Create and set the DataGrid property, where the properties here are set to fixed values, but can also be dynamically set
/// summary>
/// returns>
Public DataGrid Makegrid ()
{
MyGrid.cellpadding = 2;
MyGrid.attributes.add ("align", "center");
MyGrid.cellspacing = 0;
MyGrid.width = 500;
MyGrid.Borderwidth = 1;
MyGrid.BorderColor = ColorTranslator.FromHTML ("Black");
MyGrid.autogeneratecolumns = false;
MyGrid.Forecolor = ColorTranslator.From Html ("Black");
MyGrid.font.size = 9;
MyGrid.font.name = "Song";
MyGrid.Allowsorting = true;
/// sort command event processor
MyGrid.sortCommand = New DataGridSortCommandeventHandler (sort_grid);
/// Set Headerstyle
MyGrid.headerstyle.backcolor = ColorTranslator.From Html ("Gold");
MyGrid.headerstyle.ForeColor = ColorTranslator.From Html ("Black");
MyGrid.headerstyle.font.name = "Song";
MyGrid.headerstyle.font.size = 9;
MyGrid.headerstyle.font.bold = true;
MyGrid.headerstyle.horizontalalign = horizontalalign.center;
/// Set Alternating Style
MyGrid.alternatingItemStyle.backcolor = ColorTranslator.From Html ("Silver");
MyGrid.alternatingItemStyle.ForeColor = ColorTranslator.FromHTML ("Black");
/// Set ItemStyle
MyGrid.ItemStyle.horizontalalign = horizontalalign.left;
/// Create a tied column and attribute
Boundcolumn firstname = new boundcolumn ();
Boundcolumn lastname = new boundcolumn ();
Boundcolumn homephone = new boundcolumn ();
Boundcolumn title = new boundcolumn ();
FigStName.Headertext = "Name";
Firstname.Datafield = "firstname";
Firstname.sortexpression = "firstname";
Lastname.Headertext = "last name";
Lastname.Datafield = "lastname";
Lastname.sortexpression = "lastname"; homephone.Headertext = "Phone";
Homephone.Datafield = "Homephone";
Homephone.sortexpression = "homephone";
Title.Headertext = "Director";
Title.Datafield = "Title";
Title.Sortexpression = "Title";
MyGrid.columns.addat (0, firstname);
MyGrid.columns.addat (1, lastname);
MyGrid.columns.addat (2, homephone);
MyGrid.columns.addat (3, title);
/// Set the template column property and itemStyle Template
TM.Headertext = "** Delete Information **";
Tm.headerstyle.horizontalalign = horizontalalign.center;
Tm.ItemStyle.BackColor = ColorTranslator.From Html ("# fff778");
Tm.ItemStyle.horidge ;;
/// Create a column template.
/// column template inherits from itemplate
TM.ItemTemplate = mycol;
MyGrid.columns.addat (4, tm);
/// Bind and return
MyGrid.datasource = createdataSource ();
MyGrid.databind ();
Return mygrid;
}
}
/// columnTemplate inherits from Itemplate.
/// "instantiatein" defined who belongs to the child control
Public Class ColumnTemplate: Itemplate
{
Public void instantiatein (Control Container)
{
Label mylabel = new label ();
Mylabel.text = "Click Delete";
Checkbox mycheckbox = new checkbox ();
Container.Controls.Add (MyLabel);
Container.controls.add (MyCheckbox);
}
}
}