0

I am creating a project and I have use datagridview control. Ones I have click on rows it print on excel file and ones print the record in excel this record will be delete only for today. Next day i will run the project the deleted row I want to be restored. I am able to delete row but i will ReRun project recorde will be restored. Below is the code that I have delete the row temporarily.

Int32 rowToDelete = this.dataGridView1.Rows.GetFirstRow(DataGridViewElementStates.Selected);
if (rowToDelete > -1)
{
    this.dataGridView1.Rows.RemoveAt(rowToDelete);
}

And below is my code that i have to open excel and print the record.

Microsoft.Office.Interop.Excel.Application Excel = new Microsoft.Office.Interop.Excel.Application();
Workbook xlWorkBook1 = ExcelApp.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
xlWorkBook1 = Excel.Workbooks.Open("C:\\Users\\krupal\\Desktop\\" + Todaysdate + "\\Print Voucher.XLS", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
Microsoft.Office.Interop.Excel.Worksheet Wt = (Excel.Worksheet)xlWorkBook1.Worksheets.get_Item(1);
Excel.Visible = true;

I don't know it is possible through SQL or C#.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • Instead of removing it, set a date column of `VisibleAfter=DateTime.UtcNow.AddDays(1)` to set tomorrow's date, and set a filter of `VisibleAfter>DateTime.UtcNow` so no future dates rows are visible until they become past – Caius Jard Feb 04 '20 at 07:08
  • you can play with it any way you want. Be it sql or C#. – Atk Feb 04 '20 at 07:15
  • Thanks, @CaiusJard but when i have to put this code? –  Feb 04 '20 at 07:19

1 Answers1

0

Hope you are using a Dataset to bind records to Gridview. Before binding data to Gridview read the excel file data where you write these records. Get the records added there for the current date. Search for those records in Dataset and remove the records from the Dataset based on a key field value and bind it to the Gridview. This will prevent restoring the records to grid view again.

The following link has more details of deleting rows from data tables

Deleting specific rows from DataTable

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Melvin
  • 195
  • 1
  • 7
  • Thanks, @Melvin I have to undo the record only for today. The next day I want to be restored. and how it possible? –  Feb 04 '20 at 07:40