Solved

sorting Data in Datagrid

Posted on 2004-10-03
30
3,082 Views
Last Modified: 2008-03-17
Hello EveryBody,

I've an ODBC connection that collect all the data from a *.csv file. The datagrid is filled automatically with the fields I gave into the dsn file.

Now I would love to have the possibility to sort my data into the datagrid. The columns are created automatically and also the headers. I saw that there's a link with javascript in each of my header with the "_doPostBack" command.
How can I sort my data?
I past my code to be clearer


Thanx a lot


Nick



CODE:
            private void Page_Load(object sender, System.EventArgs e)
            {
                  // Put user code to initialize the page here
                  if (!IsPostBack)
                  {
                        string account = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
                        Label.Text = account;
                        string strConn = "Driver={Microsoft Text Driver (*.txt; *.csv)};DSN=query;DefaultDir=W:\\Inetpub\\wwwroot\\temporaneo;DBQ=W:\\Inetpub\\wwwroot\\temporaneo;";
                        string dove = "1700";
                        OdbcConnection objCSV = new OdbcConnection(strConn);
                              objCSV.Open();
                              string connstring = "select * from `query1.csv`  WHERE [Codice Gestore] IN ("+dove+");";

                        OdbcCommand oCmd = new OdbcCommand(connstring, objCSV);

                        DataGrid1.DataSource = oCmd.ExecuteReader();
                        DataGrid1.DataBind();

                              oCmd.Dispose();
                              objCSV.Close();

                  }
            }
0
Comment
Question by:nschwend
  • 13
  • 13
  • +3
30 Comments
 
LVL 28

Expert Comment

by:mmarinov
ID: 12211037
0
 

Author Comment

by:nschwend
ID: 12211058

Any link that shows me sources coded in C#
or any samples
0
 
LVL 3

Expert Comment

by:nhewitt21
ID: 12211078
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 12211088
0
 

Author Comment

by:nschwend
ID: 12211097
my problem is that I don't use any dataset... I've only my datagrid filled with

OdbcCommand oCmd = new OdbcCommand(connstring, objCSV);

DataGrid1.DataSource = oCmd.ExecuteReader();
0
 

Author Comment

by:nschwend
ID: 12211137
someone would be very nice to apply the examples to my code, because I'm not able to make it run...


THANX A LOT

Nick

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.Odbc;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace WebApplication1
{
      /// <summary>
      /// Summary description for WebForm1.
      /// </summary>
      public class WebForm1 : System.Web.UI.Page
      {
            protected System.Web.UI.WebControls.Label Label;
            protected System.Data.DataSet ds;
            protected System.Web.UI.WebControls.DataGrid DataGrid1;
            protected System.Web.UI.WebControls.DataGrid myGrid;
            protected System.Web.UI.WebControls.DataGridSortCommandEventArgs ex;
            

      
            private void Page_Load(object sender, System.EventArgs e)
            {
                  // Put user code to initialize the page here
                  if (!IsPostBack)
                  {
                        string account = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
                        Label.Text = account;
                        string strConn = "Driver={Microsoft Text Driver (*.txt; *.csv)};DSN=query;DefaultDir=W:\\Inetpub\\wwwroot\\temporaneo;DBQ=W:\\Inetpub\\wwwroot\\temporaneo;";
                        string dove = "1700";
                        OdbcConnection objCSV = new OdbcConnection(strConn);
                              objCSV.Open();
                              string connstring = "select * from `query1.csv`  WHERE [Codice Gestore] IN ("+dove+");";
                        
                        OdbcCommand oCmd = new OdbcCommand(connstring, objCSV);

                  

                        

                        DataGrid1.DataSource = oCmd.ExecuteReader();
                        DataGrid1.DataBind();

                              oCmd.Dispose();
                              objCSV.Close();
                  }
            }

            #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);
            }
            
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {    
                  this.DataGrid1.SelectedIndexChanged += new System.EventHandler(this.DataGrid1_SelectedIndexChanged);
                  this.Load += new System.EventHandler(this.Page_Load);

            }
            #endregion

            private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
            {
            
            }



      }
}
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 12211151
use this for dataset and the the samples from the web pages
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.Odbc;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace WebApplication1
{
     /// <summary>
     /// Summary description for WebForm1.
     /// </summary>
     public class WebForm1 : System.Web.UI.Page
     {
          protected System.Web.UI.WebControls.Label Label;
          protected System.Data.DataSet ds;
          protected System.Web.UI.WebControls.DataGrid DataGrid1;
          protected System.Web.UI.WebControls.DataGrid myGrid;
          protected System.Web.UI.WebControls.DataGridSortCommandEventArgs ex;
         

     
          private void Page_Load(object sender, System.EventArgs e)
          {
               // Put user code to initialize the page here
               if (!IsPostBack)
               {
                    string account = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
                    Label.Text = account;
                    string strConn = "Driver={Microsoft Text Driver (*.txt; *.csv)};DSN=query;DefaultDir=W:\\Inetpub\\wwwroot\\temporaneo;DBQ=W:\\Inetpub\\wwwroot\\temporaneo;";
                    string dove = "1700";
                    OdbcConnection objCSV = new OdbcConnection(strConn);
                         objCSV.Open();
                         string connstring = "select * from `query1.csv`  WHERE [Codice Gestore] IN ("+dove+");";
                   
                    OdbcCommand oCmd = new OdbcCommand(connstring, objCSV);
            OdbcDataAdapter adapter = new OdbcDataAdapter();
            adapter.SelectCommand = oCmd;
            DataSet ds = new DataSet();
            adapter.Fill (ds);

             
                    DataGrid1.DataSource = ds.Tables[0];
                    DataGrid1.DataBind();

                         oCmd.Dispose();
                         objCSV.Close();
               }
          }

          #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);
          }
         
          /// <summary>
          /// Required method for Designer support - do not modify
          /// the contents of this method with the code editor.
          /// </summary>
          private void InitializeComponent()
          {    
               this.DataGrid1.SelectedIndexChanged += new System.EventHandler(this.DataGrid1_SelectedIndexChanged);
               this.Load += new System.EventHandler(this.Page_Load);

          }
          #endregion

          private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
          {
         
          }



     }
}

B..M    
0
 

Author Comment

by:nschwend
ID: 12211253
Thank you so much mmarinov. You're really kind. But is it right that there's no sorting yet?

Now how can I proceed?


Thanx again!!

Nick
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12211685
Set the sort expression for each bound field with the title of the field in your datasource, ie the title of your db field.  The sorting is then automatically handled by the control

Aeros
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 12212204
nschwend,

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.Odbc;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace WebApplication1
{
     /// <summary>
     /// Summary description for WebForm1.
     /// </summary>
     public class WebForm1 : System.Web.UI.Page
     {
          protected System.Web.UI.WebControls.Label Label;
          protected System.Data.DataSet ds;
          protected System.Web.UI.WebControls.DataGrid DataGrid1;
          protected System.Web.UI.WebControls.DataGrid myGrid;
          protected System.Web.UI.WebControls.DataGridSortCommandEventArgs ex;
         

     
          private void Page_Load(object sender, System.EventArgs e)
          {
               // Put user code to initialize the page here
               if (!IsPostBack)
               {
                    DataSet ds = SelectSource();
                    DataGrid1.DataSource = ds;
                    DataGrid1.DataBind();

                         oCmd.Dispose();
                         objCSV.Close();
               }
          }

          #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);
          }
         
          /// <summary>
          /// Required method for Designer support - do not modify
          /// the contents of this method with the code editor.
          /// </summary>
          private void InitializeComponent()
          {    
               this.DataGrid1.SelectedIndexChanged += new System.EventHandler(this.DataGrid1_SelectedIndexChanged);
               this.Load += new System.EventHandler(this.Page_Load);

          }
          #endregion

          private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
          {
         
          }

        private void Sort_DataGrid(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
        {
            DataSet ds =SelectSource();
            DataGrid1.DataSource = SortDataSource (ds, e.SortExpression);
        }
       
        private DataTable SelectSource()
        {
            string account = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
            Label.Text = account;
            string strConn = "Driver={Microsoft Text Driver (*.txt; *.csv)};DSN=query;DefaultDir=W:\\Inetpub\\wwwroot\\temporaneo;DBQ=W:\\Inetpub\\wwwroot\\temporaneo;";
            string dove = "1700";
            OdbcConnection objCSV = new OdbcConnection(strConn);
            objCSV.Open();
            string connstring = "select * from `query1.csv`  WHERE [Codice Gestore] IN ("+dove+");";
                   
            OdbcCommand oCmd = new OdbcCommand(connstring, objCSV);
            OdbcDataAdapter adapter = new OdbcDataAdapter();
            adapter.SelectCommand = oCmd;
            DataSet ds = new DataSet();
            adapter.Fill (ds);
            if ( ds != null && ds.Tables.Count > 0  && ds.Tables[0] != null )
                return ds.Tables[0]
        }

        private DataView SortDataSource ( DataSet ds, string sortExpression )
        {
            DataView dv = new DataView(ds.Tables[0]);
            dv.Sort = sortExpression
            return dv;
           
        }



     }
}


B..M
0
 

Author Comment

by:nschwend
ID: 12212301
it says:
w:\inetpub\wwwroot\WebApplication1\WebForm1.aspx.cs(33): Cannot implicitly convert type 'System.Data.DataTable' to 'System.Data.DataSet'

when you declare
DataSet ds =SelectSource();


0
 

Author Comment

by:nschwend
ID: 12212366
sorry...
therer are more than 1 error... here is the list... thanx again for helping me.


w:\inetpub\wwwroot\WebApplication1\WebForm1.aspx.cs(75): 'WebApplication1.WebForm1.SelectSource()': not all code paths return a value
w:\inetpub\wwwroot\WebApplication1\WebForm1.aspx.cs(33): Cannot implicitly convert type 'System.Data.DataTable' to 'System.Data.DataSet'
w:\inetpub\wwwroot\WebApplication1\WebForm1.aspx.cs(37): The type or namespace name 'oCmd' could not be found (are you missing a using directive or an assembly reference?)
w:\inetpub\wwwroot\WebApplication1\WebForm1.aspx.cs(38): The type or namespace name 'objCSV' could not be found (are you missing a using directive or an assembly reference?)
w:\inetpub\wwwroot\WebApplication1\WebForm1.aspx.cs(71): Cannot implicitly convert type 'System.Data.DataTable' to 'System.Data.DataSet'



Nick
0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 12212489
Return DataSet from that function, instead of Table..
       private DataTable SelectSource()
        {
            ....
            ....
            if ( ds != null && ds.Tables.Count > 0  && ds.Tables[0] != null )
                return ds;
        }

** Point still goes to Martin.
0
 

Author Comment

by:nschwend
ID: 12213102
it still gives me the errors:

w:\inetpub\wwwroot\WebApplication1\WebForm1.aspx.cs(91): Cannot implicitly convert type 'System.Data.DataSet' to 'System.Data.DataTable'
w:\inetpub\wwwroot\WebApplication1\WebForm1.aspx.cs(33): Cannot implicitly convert type 'System.Data.DataTable' to 'System.Data.DataSet'
w:\inetpub\wwwroot\WebApplication1\WebForm1.aspx.cs(37): The type or namespace name 'oCmd' could not be found (are you missing a using directive or an assembly reference?)
w:\inetpub\wwwroot\WebApplication1\WebForm1.aspx.cs(38): The type or namespace name 'objCSV' could not be found (are you missing a using directive or an assembly reference?)
w:\inetpub\wwwroot\WebApplication1\WebForm1.aspx.cs(71): Cannot implicitly convert type 'System.Data.DataTable' to 'System.Data.DataSet'


why?
0
 
LVL 10

Expert Comment

by:jnhorst
ID: 12214608
I think your best bet would be to create a dataset in your project (using the IDE, not in code) and then create a data table that has the columns definitions from the text file you are retrieving.  You can do this by clicking Project >> Add New Item and select the Dataset icon in the dialog.  Give it a file name that describes the data.  You'll get a yellowish blank designer surface.  Right click this and select Add Element.  A box will appear with "element1" in the top row.  Change this to a name that you would give to a table in database if this data were in a database.  Then in the subsequent rows in this box, define each column with a name (left column) and the data type (right column).  This will create an xsd XML schema that ADO.NET will use to create a dataset and a datatable.

Once you have done this, save and build the project.  Then bring up the web form in design mode and go to the Data tab in the toolbox.  Double click the Dataset tool.  A dialog will appear with a drop down that will have your new dataset.  Click OK.  Your web form will have a tray at the bottom with an icon that represents an instance of the dataset.

Then in your code, use your command object to create an OdbcDataAdapter:

OdbcCommand oCmd = new OdbcCommand(connstring, objCSV);
OdbcDataAdapter da = New OdbcDataAdapter(oCmd);

Then call the data adapter's fill method, passing it the datatable.

da.Fill(yourDataset.theDataTable);

In design view, set the Datasource property of the data grid to the dataset.  It will appear in a dropdown in the property window.  Set the Datamember property to the data table name.  It should also appear in a dropdown.

This part is important, and easy to miss:  Execute the code to create the command and data adapter *before* checking the IsPostBack property.  If the page is not a post back, call data bind on the grid.

Page_Load(...)
{
     OdbcCommand oCmd = new OdbcCommand(connstring, objCSV);
     OdbcDataAdapter da = New OdbcDataAdapter(oCmd);
     da.Fill(yourDataset.yourDataTable);

     if (!this.IsPostBack)
     {
          theGrid.DataBind();
     }
}

Then in your SortCommand routine, do this:

yourDataset.yourDataTable.DefaultView.Sort = "ColumnName";
theGrid.DataBind();

Since you put the code that creates and calls Fill() on the data adapter outside of the if (!this.IsPostBack){} block, when you click on one of the column headers that fires a postback, you will retrieve the data from the text file into the datatable when Page_Load fires on the post back.  After Page_Load fires, the SortCommand routine will fire, at which time you set the Sort property of the datatable's DefaultView and then bind the grid to show your sorted data.

John
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 28

Expert Comment

by:mmarinov
ID: 12214969
i'm really sorry for these errors, but i couldn't test the code because i didn't have a compiler

correct this
private DataTable SelectSource()
        {
            string account = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
            Label.Text = account;
            string strConn = "Driver={Microsoft Text Driver (*.txt; *.csv)};DSN=query;DefaultDir=W:\\Inetpub\\wwwroot\\temporaneo;DBQ=W:\\Inetpub\\wwwroot\\temporaneo;";
            string dove = "1700";
            OdbcConnection objCSV = new OdbcConnection(strConn);
            objCSV.Open();
            string connstring = "select * from `query1.csv`  WHERE [Codice Gestore] IN ("+dove+");";
                   
            OdbcCommand oCmd = new OdbcCommand(connstring, objCSV);
            OdbcDataAdapter adapter = new OdbcDataAdapter();
            adapter.SelectCommand = oCmd;
            DataSet ds = new DataSet();
            adapter.Fill (ds);
            if ( ds != null && ds.Tables.Count > 0  && ds.Tables[0] != null )
                return ds.Tables[0]
        }


with

private DataSet SelectSource()
        {
            string account = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
            Label.Text = account;
            string strConn = "Driver={Microsoft Text Driver (*.txt; *.csv)};DSN=query;DefaultDir=W:\\Inetpub\\wwwroot\\temporaneo;DBQ=W:\\Inetpub\\wwwroot\\temporaneo;";
            string dove = "1700";
            OdbcConnection objCSV = new OdbcConnection(strConn);
            objCSV.Open();
            string connstring = "select * from `query1.csv`  WHERE [Codice Gestore] IN ("+dove+");";
                   
            OdbcCommand oCmd = new OdbcCommand(connstring, objCSV);
            OdbcDataAdapter adapter = new OdbcDataAdapter();
            adapter.SelectCommand = oCmd;
            DataSet ds = new DataSet();
            adapter.Fill (ds);
            if ( ds != null )
                return ds;
            else
                return null;
        }


and this
          private void Page_Load(object sender, System.EventArgs e)
          {
               // Put user code to initialize the page here
               if (!IsPostBack)
               {
                    DataSet ds = SelectSource();
                    DataGrid1.DataSource = ds;
                    DataGrid1.DataBind();

                         oCmd.Dispose();
                         objCSV.Close();
               }
          }
to

          private void Page_Load(object sender, System.EventArgs e)
          {
               // Put user code to initialize the page here
               if (!IsPostBack)
               {
                    DataSet ds = SelectSource();
                    DataGrid1.DataSource = SortDataSource (ds, "");
                    DataGrid1.DataBind();
               }
          }

also i've noticed that you have to add to the <asp:datagrid> tag OnSortCommand="Sort_DataGrid"

HTH
B..M
0
 

Author Comment

by:nschwend
ID: 12215038
Thanx again mmarinov.

you're really relly really kind, but (I know I'm boring)... it still doesn't work...

now it tells me that
the type or namespace name 'oCmd' could not be found line 37
the type or namespace name 'objCSV' could not be found line 38

and is referred to:

oCmd.Dispose();
objCSV.Close();


thanx again so much

Nick
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 12215055
you have to remove these lines from the Page_load event - check my previous posting
B..M
0
 

Author Comment

by:nschwend
ID: 12215076
ok... I didn't read the rest of your post because it was on the back of the page ;-)

but when I add the tag onSortCommand it gives me this error:

Compiler error message: CS0122: 'WebApplication1.WebForm1.Sort_DataGrid(object, System.Web.UI.WebControls.DataGridSortCommandEventArgs)' is inaccessible due to its protection level
....

and if I remove the tag it gives me a big error on

ERROR [HY000] [Microsoft] [ODBC Driver Text] General Error....(it's very long)
on the
objCSV.Open();
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 12215103
no you don't remove the tag
remove
oCmd.Dispose();
objCSV.Close();
from your page_load only

for the first page make this
private void Sort_DataGrid(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)

to
public void Sort_DataGrid(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)

B..M
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 12215106
if you still have troubles, please post your entire html and code behind codes and will edit them for you to be working

B..M
0
 

Author Comment

by:nschwend
ID: 12215128
Here is...

Thanx again and again...

Nick


<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
      <HEAD>
            <title>WebForm1</title>
            <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
            <meta content="C#" name="CODE_LANGUAGE">
            <meta content="JavaScript" name="vs_defaultClientScript">
            <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
      </HEAD>
      <body MS_POSITIONING="GridLayout">
            <form id="Form1" method="post" runat="server">
                  &nbsp;
                  <asp:label id="Label" style="Z-INDEX: 101; LEFT: 400px; POSITION: absolute; TOP: 56px" runat="server"
                        Width="216px">Label</asp:label><asp:datagrid id="DataGrid1" style="Z-INDEX: 102; LEFT: 16px; POSITION: absolute; TOP: 112px"
                        runat="server" ShowFooter="True" AllowSorting="True"  OnSortCommand="Sort_DataGrid">
                        <AlternatingItemStyle BorderColor="Silver" BackColor="#E0E0E0"></AlternatingItemStyle>
                  </asp:datagrid></form>
      </body>
</HTML>







using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.Odbc;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace WebApplication1
{
      /// <summary>
      /// Summary description for WebForm1.
      /// </summary>
      public class WebForm1 : System.Web.UI.Page
      {
            protected System.Web.UI.WebControls.Label Label;
            protected System.Data.DataSet ds;
            protected System.Web.UI.WebControls.DataGrid DataGrid1;
            protected System.Web.UI.WebControls.DataGrid myGrid;
            protected System.Web.UI.WebControls.DataGridSortCommandEventArgs ex;
         

     
            private void Page_Load(object sender, System.EventArgs e)
            {
                                    
                  // Put user code to initialize the page here
                  if (!IsPostBack)
                  {
                        DataSet ds = SelectSource();
                        DataGrid1.DataSource = SortDataSource(ds,"");
                        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);
            }
         
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {    
                  this.DataGrid1.SelectedIndexChanged += new System.EventHandler(this.DataGrid1_SelectedIndexChanged);
                  this.Load += new System.EventHandler(this.Page_Load);

            }
            #endregion

            private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
            {
         
            }

            private void Sort_DataGrid(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
            {
                  DataSet ds =SelectSource();
                  DataGrid1.DataSource = SortDataSource (ds, e.SortExpression);
            }
       
            private DataSet SelectSource()
            {
                  string account = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
                  Label.Text = account;
                  string strConn = "Driver={Microsoft Text Driver (*.txt; *.csv)};DSN=query;DefaultDir=W:\\Inetpub\\wwwroot\\temporaneo;DBQ=W:\\Inetpub\\wwwroot\\temporaneo;";
                  string dove = "1700";
                  OdbcConnection objCSV = new OdbcConnection(strConn);
                  objCSV.Open();
                  string connstring = "select * from `query1.csv`  WHERE [Codice Gestore] IN ("+dove+");";
                   
                  OdbcCommand oCmd = new OdbcCommand(connstring, objCSV);
                  OdbcDataAdapter adapter = new OdbcDataAdapter();
                  adapter.SelectCommand = oCmd;
                  DataSet ds = new DataSet();
                  
                  adapter.Fill (ds);
                  if ( ds != null)
                        return ds;
                  else
                        return null;
            }

            private DataView SortDataSource ( DataSet ds, string sortExpression )
            {
                  DataView dv = new DataView(ds.Tables[0]);
                  dv.Sort = sortExpression;
                  return dv;
           
            }



      }
}
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 12215146
the html code must be ok
for your code behind use this

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.Odbc;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace WebApplication1
{
     /// <summary>
     /// Summary description for WebForm1.
     /// </summary>
     public class WebForm1 : System.Web.UI.Page
     {
          protected System.Web.UI.WebControls.Label Label;
          protected System.Data.DataSet ds;
          protected System.Web.UI.WebControls.DataGrid DataGrid1;
     
          private void Page_Load(object sender, System.EventArgs e)
          {
                             
               // Put user code to initialize the page here
               if (!IsPostBack)
               {
                    DataSet ds = SelectSource();
                    DataGrid1.DataSource = SortDataSource(ds,"");
                    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);
          }
         
          /// <summary>
          /// Required method for Designer support - do not modify
          /// the contents of this method with the code editor.
          /// </summary>
          private void InitializeComponent()
          {    
               this.Load += new System.EventHandler(this.Page_Load);

          }
          #endregion

          public void Sort_DataGrid(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
          {
               DataSet ds =SelectSource();
               DataGrid1.DataSource = SortDataSource (ds, e.SortExpression);
          }
       
          private DataSet SelectSource()
          {
               string account = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
               Label.Text = account;
               string strConn = "Driver={Microsoft Text Driver (*.txt; *.csv)};DSN=query;DefaultDir=W:\\Inetpub\\wwwroot\\temporaneo;DBQ=W:\\Inetpub\\wwwroot\\temporaneo;";
               string dove = "1700";
               OdbcConnection objCSV = new OdbcConnection(strConn);
               objCSV.Open();
               string connstring = "select * from `query1.csv`  WHERE [Codice Gestore] IN ("+dove+");";
                   
               OdbcCommand oCmd = new OdbcCommand(connstring, objCSV);
               OdbcDataAdapter adapter = new OdbcDataAdapter();
               adapter.SelectCommand = oCmd;
               DataSet ds = new DataSet();

               objCSV.Close();
               oCmd = null;
               objCSV = null;              

               adapter.Fill (ds);
               if ( ds != null)
                    return ds;
               else
                    return null;
          }

          private DataView SortDataSource ( DataSet ds, string sortExpression )
          {
               DataView dv = new DataView(ds.Tables[0]);
               dv.Sort = sortExpression;
               return dv;
           
          }



     }
}


Regards,
B..M
0
 

Author Comment

by:nschwend
ID: 12215176
Thanx again...

It shows me the table but It won't sort anything...

I don't want to boring you...

I'm really sorry!

nick
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 12215204
so the next step is to put your columns in your datagrid, like

<asp:datagrid id="DataGrid1" style="Z-INDEX: 102; LEFT: 16px; POSITION: absolute; TOP: 112px" runat="server" ShowFooter="True" AllowSorting="True"  OnSortCommand="Sort_DataGrid">
    <AlternatingItemStyle BorderColor="Silver" BackColor="#E0E0E0"></AlternatingItemStyle>
    <Columns>
    <asp:BoundColumn DataField="IntegerValue"
                 SortExpression="IntegerValue"
                 HeaderText="Item"/>

    <asp:BoundColumn DataField="StringValue"
                 SortExpression="StringValue"
                 HeaderText="Description"/>

    <asp:BoundColumn DataField="CurrencyValue"
                 HeaderText="Price"
                 SortExpression="CurrencyValue"
                 DataFormatString="{0:c}" />

    </Columns>
</asp:datagrid>

this is just an example, but you have to put the correct values:

DataField - this is the column name from your dataset(datatable)
SortExpression - this is the column name that you want the datagrid to be sorted by when you click on the column's link


Regards,
B..M
0
 

Author Comment

by:nschwend
ID: 12215257
doesn't work, because with ODBC it fills automatically the table... I don't have to specify any columns and it grabs automatically the name and the data from the *.csv file and shows them into the datagrid.

No problem. I try not to implement it...

Thanx again

If you don't have something else to suggest to do, I'll give you the points and I close the forum because you have lost too much time helping me...

really thanx again...

Nick
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 12215317
i'll try something and will be back in 10 minutes

Regards,
B..M
0
 
LVL 28

Accepted Solution

by:
mmarinov earned 500 total points
ID: 12215453
here is the working example:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.Odbc;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace cs_web_test.DatGrid
{
    /// <summary>
    /// Summary description for csvDG.
    /// </summary>
    public class csvDG : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.Label Label;
        protected System.Web.UI.WebControls.DataGrid DataGrid1;
        private DataSet ds = null;
   
        private void Page_Load(object sender, System.EventArgs e)
        {
                             
            if (!IsPostBack)
            {
                ds = SelectSource();
                DataGrid1.DataSource = ds.Tables[0];
                DataGrid1.DataBind();
                for ( int index=0; index < DataGrid1.Columns.Count; index++ )
                {
                    DataGrid1.Columns[index].SortExpression = ds.Tables[0].Columns[index].ColumnName;
                }
            }
        }
        public void Sort_DataGrid(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
        {
            DataSet ds =SelectSource();
            DataGrid1.DataSource = SortDataSource (ds, e.SortExpression);
            DataGrid1.DataBind();
            for ( int index=0; index < DataGrid1.Columns.Count; index++ )
            {
                DataGrid1.Columns[index].SortExpression = ds.Tables[0].Columns[index].ColumnName;
            }

        }
       
        private DataSet SelectSource()
        {
            string account = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
            Label.Text = account;
            string strConn = "Driver={Microsoft Text Driver (*.txt; *.csv)};DSN=temporaneo;DefaultDir=C:\\Inetpub\\wwwroot\\;DBQ=C:\\Inetpub\\wwwroot\\;";
            string dove = "1700";
            OdbcConnection objCSV = new OdbcConnection(strConn);
            objCSV.Open();
            string connstring = "select * from `temporaneo.csv`";
                   
            OdbcCommand oCmd = new OdbcCommand(connstring, objCSV);
            OdbcDataAdapter adapter = new OdbcDataAdapter();
            adapter.SelectCommand = oCmd;
            DataSet ds = new DataSet();
               
            adapter.Fill (ds);
            if ( ds != null)
                return ds;
            else
                return null;
        }

        private DataView SortDataSource ( DataSet ds, string sortExpression )
        {
            DataView dv = new DataView(ds.Tables[0]);
            dv.Sort = sortExpression;
            return dv;
           
        }


        #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);
        }
       
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {    
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

        private void DataGrid1_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
        {
       
        }
    }
}


<asp:datagrid id="DataGrid1" style="Z-INDEX: 102; LEFT: 16px; POSITION: absolute; TOP: 112px"
                runat="server" ShowFooter="True" AllowSorting="True" OnSortCommand="Sort_DataGrid">
                <AlternatingItemStyle BorderColor="Silver" BackColor="#E0E0E0"></AlternatingItemStyle>
            </asp:datagrid>

Regards,
B..M
0
 

Author Comment

by:nschwend
ID: 12215549
Hey my man

IT WORKS!!!!!!!!



Thank you again and again... You are incredibly generous and kind....

sincerly

Nick
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 12215565
You are welcom nschwend

Regards,
B..M
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now