Serving Information Simply

Tuesday, 14 August 2012

Exporting the data into PDF OR MS-world OR MS Excel in alphabetical Order!.

Hi Guys,
Last day i did a task in which i had to sort the data into alphabetical order while exporting that data into PDF or MS-world or MS Excel.
The problem was that, the grid from which data is being  exported should not be sort into alphabetical order  only Exported data must be sort.
So Today i am presenting the solution of that issue ,that will absolutely help you if you get such type of problem i will only provide an example how to solve such type of issue.

Solution - 1. Suppose you have a  dataset named DSexportData.
Sqlconnection con = new sqlconnection();
con = configurationmanager.connectionstring["Abhinav-DB"].connectionstring;
String StrQuery = Select * from TableName;
sqlcommand cmd = new sqlcommand(StrQuery,con);
dataset DsExport = new dataset();
sqldataadapter da = new Sqldataadapter(cmd);

//**Here you will assign DSexportData to the gridview named  ExportgridView **//

ExportgridView .datasource = DSexportData ;
ExportgridView .databind();

// ** Here is logic of Sorting the data that is Exporting into PDF OR MS-world OR MS Excel.**//

Step 1- Create the object of datatable.
          datatable dt = new datatable();

Step 2- assign dataset data to the Created object of datatable.
          dt = DSexportData .tables[0];

Step3 - creat an object of DataView Class and pass your datatable to dataview.
          DataView _Dv = new Dataview(dt);

Step 4.- Now sort the dataview accoring to your prefered column name.

         _Dv.Sort = "PreferedColumn_Name Asc";
Step 5.  Assign _Dv to to the Datatable
         dt = _dv.Totable();

Step 6 - now Merge your datatable to the dataset .

Note :- you will have to dispose or clear your dataset before merging sorted datatable with
        that  dataset.
Ex :-
        DSexportData .Clear();
        DSexportData .Merge(Dt);

Step 7 - now Bind your gridview with dataset

ExportgridView .datasource = DsExport;
ExportgridView .databind();

Thank's IF you got an issue let me know ......!keep visiting..

1 comment:

  1. it really help me thank you keep blogging.