<script runat="server">
public void Page_Load(Object sender, EventArgs e)
{
// Initialize only the first time...
if (!Page.IsPostBack)
{
lblURL.Text = Request.Url + "<hr>";
}
}
public void OnLoadData(Object sender, EventArgs e)
{
// Prepare the command strings
String strCmd1 = "SELECT employeeid, lastname, firstname, titleofcourtesy, title, ISNULL(reportsto,0) As boss FROM Employees";
StringBuilder sb = new StringBuilder("");
sb.Append("SELECT et.employeeid, t.territorydescription FROM EmployeeTerritories AS et ");
sb.Append("INNER JOIN Territories AS t ");
sb.Append("ON t.territoryid = et.territoryid");
String strCmd2 = sb.ToString();
// Executes the commands
DataSet ds = new DataSet();
SqlDataAdapter da;
SqlConnection conn = new SqlConnection(txtConn.Text);
da = new SqlDataAdapter(strCmd1, conn);
da.Fill(ds, "EmployeesTable");
da = new SqlDataAdapter(strCmd2, conn);
da.Fill(ds, "TerritoriesTable");
// Create a relation between the two tables
DataColumn dc1 = ds.Tables["EmployeesTable"].Columns["employeeid"];
DataColumn dc2 = ds.Tables["TerritoriesTable"].Columns["employeeid"];
DataRelation drel = new DataRelation("EmployeesAndTerritories", dc1, dc2);
ds.Relations.Add(drel);
// Bind the data
grid.DataSource = ds.Tables["EmployeesTable"];
// Display the data
grid.DataBind();
}
ArrayList GetTerritories(DataRowView drv)
{
// Extract the underlying row from the DataRowView object
DataRow dr = drv.Row;
// Retrieve the child rows according to the data relation
DataRow[] adr = dr.GetChildRows("EmployeesAndTerritories");
// Create and return an array made of the values in the
// TerritoryDescription column
ArrayList a = new ArrayList();
foreach(DataRow tmp in adr)
a.Add(tmp["territorydescription"]);
return a;
}
public void SortCommand(Object sender, DataGridSortCommandEventArgs e)
{
if (e.SortExpression != "*")
grid.Attributes["SortExpression"] = e.SortExpression;
else
{
// Retrieves the drop-down list control through its ID
DataGridItem dgi = (DataGridItem) e.CommandSource;
DropDownList dd = (DropDownList) dgi.FindControl("ddSort");
// Retrieves the sorting expression from the list
grid.Attributes["SortExpression"] = dd.SelectedItem.Value;
// Persists the currently selected drop-down item
grid.Attributes["FieldIndex"] = dd.SelectedIndex.ToString();
}
UpdateView();
}
public void ItemCreated(Object sender, DataGridItemEventArgs e)
{
ListItemType lit = e.Item.ItemType;
if (lit == ListItemType.Header)
{
// Create and fill a drop-down list control
DropDownList dd = new DropDownList();
dd.ID = "ddSort";
ListItem li1, li2, li3;
// ListItem constructor takes Text and Value for the item
li1 = new ListItem("Title of courtesy", "titleofcourtesy");
dd.Items.Add(li1);
li2 = new ListItem("Last Name", "lastname");
dd.Items.Add(li2);
li3 = new ListItem("First Name", "firstname");
dd.Items.Add(li3);
// Selects the item, if any, that was selected last time
dd.SelectedIndex = Convert.ToInt32(grid.Attributes["FieldIndex"]);
// Add the drop-down list to the header of the 2nd column
TableCell cell = (TableCell) e.Item.Controls[1];
cell.Controls.Add(dd);
}
if (lit == ListItemType.Footer)
{
e.Item.Cells.RemoveAt(2);
e.Item.Cells.RemoveAt(0);
e.Item.Cells[0].ColumnSpan = 3;
// Populate the Views list
DropDownList ddViews = (DropDownList) e.Item.FindControl("ddViews");
ListItem l;
l = new ListItem("Ms. Surname, Name", "courtesylastfirst.ascx");
ddViews.Items.Add(l);
l = new ListItem("Name Surname - (Ms.)", "firstlastcourtesy.ascx");
ddViews.Items.Add(l);