Click here to Skip to main content
15,922,584 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi Team,
I am trying to put a filter with input dates (From and To) before writing to excel as output.
I tried the below logic but it is writing all the data as it is in Grid to Excel. I am unable to go ahead.

Appreciates if someone can help me doing this?



The Grid displays as below :
Dates      Name     School Place
5/13/15    Shreyas    ABC   BLR
4/12/15    Amit      XYZ   MYS
6/24/15    Niraj     MES   BJP
11/10/15   Akshay    NIT   ENG
8/26/15    Natraj    GVC   BGK


Expected :
Examaple if the DateFrom : 5/13/15 and DateTo : 6/24/15.The output expected is :
Dates      Name     School Place
5/13/15    Shreyas    ABC   BLR
6/24/15    Niraj     MES   BJP


What I have tried:

void WriteToExcel(DateTimePicker DateFrom,DateTimePicker DateTo)
 {            
        ExcelWriter exlWtr = new ExcelWriter();    
        int rows = Grid.RowCount + 1;
        int cols = Grid.ColumnCount;
        if (rows == 0 || cols == 1)
             return;                 

        string[] colNames = new string[cols];
        //  Populate the Header names first
         int i = 0;
         for (int colItr = 0; colItr < cols; j++)
         {
			if (Grid.Columns[j].Visible)
            {
                  colNames[i] = Grid.Columns[j].Name;
                  i++;
            }
         }
          object[,] values = new object[rows, i];
          //Copy the  Column names
          for (int j = 0; j < i; j++)
          {
             values[0, j] = Grid.Columns[colNames[j]].HeaderText; 
          }
          //Get the data now
           foreach (DataGridViewRow row in Grid.Rows)
           {
               for (int j = 0; j < i; j++)
               {
                    //Grid[colNames[0], j].FormattedValue; 	// This displays the dates from the Grid
					//May be here i need to add the logic to filter with dates and write
                    values[row.Index + 1, j] = Grid[colNames[j], row.Index].FormattedValue;                   
               }
           }
           exlWtr.createFile(false);
           exlWtr.writeRange(1, 1, 1, values);  // Writes to Excel
           exlWtr.SetHorizontalAllignment(1, 1, Grid.Rows.Count + 1, i, "LEFT");
           exlWtr.SetVisibility(true);
          
  }
Posted
Updated 21-Feb-17 23:01pm

Google Search is your friend: c# export datagrid to excel[^]
 
Share this answer
 
Comments
manju 3 22-Feb-17 5:02am    
Thanks Graeme for the link provided.
Yes I did took help of google and tried the possible way I can and end up writing to excel all the data from Grid.
I thought any of the masters can help me out seeing the code which I tried and help me out where I am going wrong.

Thanks
Sharan
Thanks Graeme for the link provided.
Yes I did took help of google and tried the possible way I can and end up writing to excel all the data from Grid.
I thought any of the masters can help me out seeing the code which I tried and help me out where I am going wrong.

Thanks
Sharan
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900