it is showing me an error while I am using foreach loop
Error 1 Cannot convert type 'Telerik.WinControls.UI.GridViewDataColumn' to 'System.Windows.Forms.DataGridViewColumn' E:\myApp\myApp\Students\frmEditStudentList.cs 286 13 myApp
Last column contains checkbox to select the record for delete purpose. Now I want to export this data in Excel. for this i wrote the following code
private void tbExcel_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
foreach (DataGridViewColumn column in dGVDep.Columns)
{
dt.Columns.Add(column.HeaderText, column.ValueType);
}
foreach (DataGridViewRow row in dGVDep.Rows)
{
dt.Rows.Add();
foreach (DataGridViewCell cell in row.Cells)
{
dt.Rows[dt.Rows.Count - 1][cell.ColumnIndex] = cell.Value.ToString();
}
}
string folderPath = @"C:\Reports\";
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
using (XLWorkbook wb = new XLWorkbook())
{
wb.Worksheets.Add(dt, "StudentList");
wb.Worksheet(1).Cells("A1:C1").Style.Fill.BackgroundColor = XLColor.DarkGreen;
for (int i = 1; i <= dt.Rows.Count; i++)
{
string cellRange = string.Format("A{0}:C{0}", i + 1);
if (i % 2 != 0)
{
wb.Worksheet(1).Cells(cellRange).Style.Fill.BackgroundColor = XLColor.GreenYellow;
}
else
{
wb.Worksheet(1).Cells(cellRange).Style.Fill.BackgroundColor = XLColor.Yellow;
}
}
wb.Worksheet(1).Columns().AdjustToContents();
wb.SaveAs(folderPath + "StudentList- " + DateTime.Now.ToShortDateString() + ".xlsx");
}
MessageBox.Show("Student Data Exported Successfully", "Alhamd Solution", MessageBoxButtons.OK, MessageBoxIcon.Information);*/
}