Skip to main content

Gridview Sorting Problem

When you bind a List to a Gridview, you can not sort it, because your data source is not a Dataset, Dataview or DataTable. So you should convert your data source (List) to a Dataset for example.


You can do it such as following code:


    1 public static DataSet dsSearch()
    2 {
    3 
    4     DataTable dtTelcoCenter = new DataTable();
    5     dtTelcoCenter.Columns.Add("Id", typeof(int));
    6     dtTelcoCenter.Columns.Add("City", typeof(string));
    7     dtTelcoCenter.Columns.Add("Capacity", typeof(int));
    8 
    9     IList<Telco> telcoCenter = Search(centerName, Prefix);// Search method return a list
   10 
   11 
   12     foreach (Telco telco in telcoCenter)
   13     {
   14         DataRow rowTelcoCenter = dtTelcoCenter.NewRow();
   15         rowTelcoCenter["Id"] = telco.Id;
   16         rowTelcoCenter["City"] = telco.City;
   17         rowTelcoCenter["Capacity"] = telco.Capacity;
   18         dtTelcoCenter.Rows.Add(rowTelcoCenter);
   19     }
   20     DataSet dsTelcoCenter = new DataSet();
   21     dsTelcoCenter.Tables.Add(dtTelcoCenter);
   22     return dsTelcoCenter;
   23 }

You can bind this Dataset to your Gridview data source

2 comments

Popular posts from this blog

Parameter Sniffing

Sometimes maybe you find out your SP (Store Procedure) doesn't response in reasonable time despite Select statement, which you used in that SP, has good performance and result is shown you immediately. What's wrong with your SP?
There are different answers for that problem but when I faced on this problem, I realized it's strongly related to Parameter Sniffing.

You can solve Parameter Sniffing easily with follow this example:
If you have a SP such as GetOrderForCustomers that has Parameter Sniffing issue:

Create procedure GetOrderForCustomers (@CustID Varchar (20))
As
Begin
Select * from orders
Where customerid = @CustID
End

To solve problem you should change your SP similar:

Create procedure GetOrderForCustomers (@CustID Varchar (20))
As
Begin
Declare@LocCustID Varchar (20)
Set @LocCustID = @CustID
Select * from orders
Where customerid = @LocCustID
End

Good luck.

How can you save a MS Office file into a database?

If you want to save a MS Office document such as 
Word doc in a database, you can do it with the 
following VB.net code:

    Dim vStream As New FileStream("C:\Alex.doc", FileMode.Open)      Dim vLen As Integer = New FileInfo("C:\Alex.doc").Length     Dim vBlob(vLen) As Byte     Dim n As Integer = vStream.Read(vBlob, 0, vLen)     Dim vSql As String = "INSERT INTO tBlobTable (vId,vBlob) VALUES (1,@vBlob)"      Dim sql As New SqlCommand(vSql, vConn)     Dim sqlPar As New SqlParameter("@vBlob", SqlDbType.Image)     sqlPar.Value = vBlob     sql.Parameters.Add(sqlPar)
I hope you would find it useful.