How to Sort a Datagrid?
Hi to all,
I am new in this forum and I'm trying to resolve this problem:
I have a domain in wich I created 700 user account.
The first problem was to view all users of active directory (this function properly).
The second step is to sort the User Name column.
I tried the same method used when I want to sort the articles in a DB, but it don't work.
You can help me?
This is the 2 pages I created:
- .aspx -
- .aspx.cs -
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.DirectoryServices;
namespace ADSI
{
///
/// Descrizione di riepilogo per adsi_vista.
///
public class ElencoUtentiTabella : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid2;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected DirectoryEntry de;
protected DataSet ds;
private void Page_Load(object sender, System.EventArgs e)
{
DirectorySearcher src = new
DirectorySearcher("(&(objectCategory=Person)(objectClass=user))");
//DataTable for users
DataTable tbUsers = new DataTable("users");
//Create Columns for DataTable.
tbUsers.Columns.Add("UserName",System.Type.GetType("System.String"));
ds.Tables.Add(tbUsers);
//DataTable for properties
DataTable tbProperties = new DataTable("properties");
//Create Columns for DataTable.
tbProperties.Columns.Add("PropertyName",System.Type.GetType("System.String")
);
tbProperties.Columns.Add("PropertyValue",System.Type.GetType("System.String"
));
ds.Tables.Add(tbProperties);
src.SearchRoot = de;
src.SearchScope = SearchScope.Subtree;
foreach(SearchResult res in src.FindAll())
{
System.Collections.IDictionaryEnumerator ien =
res.Properties.GetEnumerator();
DataRow topRow = ds.Tables["users"].NewRow();
topRow["UserName"] =res.Properties["Name"][0];
ds.Tables["users"].Rows.Add(topRow);
}
DataGrid1.DataSource = ds.Tables["users"];
DataGrid1.DataBind();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.de = new
System.DirectoryServices.DirectoryEntry("LDAP://DOMINIO/CN=Users,DC=DOMINIO,
DC=IT","administrator","prova123");
this.ds = new System.Data.DataSet();
((System.ComponentModel.ISupportInitialize)(this.ds)).BeginInit();
this.DataGrid1.SelectedIndexChanged += new
System.EventHandler(this.DataGrid1_SelectedIndexChanged);
//
// ds
//
this.ds.DataSetName = "NewDataSet";
this.ds.Locale = new System.Globalization.CultureInfo("en-AU");
this.Load += new System.EventHandler(this.Page_Load);
((System.ComponentModel.ISupportInitialize)(this.ds)).EndInit();
}
#endregion
...
Purushottam RathorePosted May 28, 2008, 2:04 AM
Sorting DataGrid both ways:
The DataGrid should be populated with some data. Now in the Properties window of the DataGrid, set AllowSorting = "true". Okay, now sorting is enabled and all you need now is the actual code that sorts the DataGrid.
DataGrid Sorting Code:
Whenever the Columns header is clicked, SortCommand event is fired. So, place the code below in the SortCommand event handler:
private void Sort_DataGrid(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
// Never use Queries like this always use Stored procedures
SqlCommand myCommand = new SqlCommand("SELECT * FROM Categories", myConnection);
myCommand.CommandType = CommandType.Text;
SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand);
DataSet ds = new DataSet();
myAdapter.Fill(ds, "Categories");
DataView dv = new DataView(ds.Tables["Categories"]);
if ((numberDiv % 2) == 0)
dv.Sort = e.SortExpression + " " + "ASC";
else
dv.Sort = e.SortExpression + " " + "DESC";
numberDiv++;
myDataGrid.DataSource = dv;
myDataGrid.DataBind();
}
In the above code, "numberDiv" is the public static Int32 variable which keeps count of the even and odd number of mouse clicks on the header. The heart of this example is the DataView object which has a SortExpression property, it represents the name of the column clicked.