-----------------------------------------------------------
New Message on MumbaiUserGroup
-----------------------------------------------------------
From: niyati
Message 6 in Discussion
Hi,
I am using the same and I am even getting my sorted results. My querry is; it gets
sorted only in the ascending order and not in desc or both ways. So how can I do that.
Below is the code which allows me to sort my data in the datagrid.
HTML:
<asp:datagrid id="DataGrid1" style="Z-INDEX: 100; LEFT: 8px; POSITION: absolute; TOP:
8px" runat="server" AllowSorting="True" Font-Size="X-Small" Font-Names="Arial"
CellPadding="5" AutoGenerateColumns="False" OnSortCommand="DataGrid1_SortCommand">
<AlternatingItemStyle Font-Size="X-Small" Font-Names="Arial"
BackColor="LightGray"></AlternatingItemStyle>
<HeaderStyle Font-Size="X-Small" Font-Names="Arial" Font-Bold="True"
BorderWidth="5px" BorderColor="#000063" BackColor="PapayaWhip"></HeaderStyle>
<Columns>
<asp:BoundColumn DataField="Id" SortExpression="Id"
HeaderText="Id"></asp:BoundColumn>
<asp:BoundColumn DataField="Fname" SortExpression="Fname"
HeaderText="Fname"></asp:BoundColumn>
<asp:BoundColumn DataField="lname" SortExpression="lname"
HeaderText="lname"></asp:BoundColumn>
<asp:BoundColumn DataField="emailid" SortExpression="emailid"
HeaderText="emailid"></asp:BoundColumn>
<asp:BoundColumn DataField="empextno" SortExpression="empextno"
HeaderText="empextno"></asp:BoundColumn>
</Columns>
</asp:datagrid>
-----------------------------------------------------------------------------------------------
Codebehind:
string SortField;
DataView dv;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if(!IsPostBack)
{
if (SortField == "")
{
SortField = "Fname";
}
BindGrid();
}
}
void BindGrid()
{
DataGrid1.DataSource=CreateDataSource();
DataGrid1.DataBind();
}
ICollection CreateDataSource()
{
//-----------------------------------------------
SqlCommand cmd =new SqlCommand("select * from emp_details",sqlCon);
sqlCon.Open();
SqlDataReader sdr=cmd.ExecuteReader();
DataTable dt=new DataTable();
DataRow dr;
dt.Columns.Add(new DataColumn("Id",typeof(Int32)));
dt.Columns.Add(new DataColumn("Fname",typeof(string)));
dt.Columns.Add(new DataColumn("Lname",typeof(string)));
dt.Columns.Add(new DataColumn("Emailid",typeof(string)));
dt.Columns.Add(new DataColumn("empextno",typeof(Int32)));
while(sdr.Read())
{
dr = dt.NewRow();
dr[0] = sdr["empid"];
dr[1] = sdr["empfname"];
dr[2] =sdr["emplname"];
dr[3] =sdr["empemailid"];
dr[4] =sdr["empextno"];
dt.Rows.Add(dr);
}
sdr.Close();
dv = new DataView(dt);
dv.Sort=SortField;
sqlCon.Close();
return dv;
//-------------------------------------------------
}
public void DataGrid1_SortCommand(object source, DataGridSortCommandEventArgs e)
{
SortField = e.SortExpression;
BindGrid();
}
===========================
C if you can help me out.
Thanks,
Niyati
Sports, sports and more sports! Keep up with all that�s happening! Stay connected with
MSN Sports!
-----------------------------------------------------------
To stop getting this e-mail, or change how often it arrives, go to your E-mail
Settings.
http://groups.msn.com/mumbaiusergroup/_emailsettings.msnw
Need help? If you've forgotten your password, please go to Passport Member Services.
http://groups.msn.com/_passportredir.msnw?ppmprop=help
For other questions or feedback, go to our Contact Us page.
http://groups.msn.com/contact
If you do not want to receive future e-mail from this MSN group, or if you received
this message by mistake, please click the "Remove" link below. On the pre-addressed
e-mail message that opens, simply click "Send". Your e-mail address will be deleted
from this group's mailing list.
mailto:[EMAIL PROTECTED]