Issue in export data in excel using Telerik UI for Window form

Last Reply 3 days ago By dharmendr

Posted 18 days ago

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

I wrote the following code to get the data from database.

        private void DisplayData()
        {
            dGVDep.Visible = true;
            dGVDep.Columns.Clear();
            con = new AB.SqlDbConnect();
            con.SqlQuery("Select StuID,AdmissionNo,ReferenceNo,AdmissionDate,AcademicName,ClassName,SectionName,RollNo,SName,FName,Fee,Fee_Discount,DOB,Gender,BloodGroup,Religion,SCNIC,SPhone,ParentID,FCNIC,FPhone,FJob,MName,MCNIC,MPhone,MJob,Address,OfficeAddress,GuardianName,GCNIC,GPhone,GAddress,Qualification,Income,Email,AddNote,BookNo,BookID,BookDate,Active_Status FROM tblStdReg as s inner join tblAcademicYear as a on s.YearID=a.YearID inner join tblDefClass as dc on s.ClassID=dc.ClassID inner join tblDefSection as sec on s.SectionID=sec.SectionID where Entry_Date='Save' Order by StuID desc;"); //where s.YearID=@YId and s.ClassId=@CId and s.SectionID=@SId
            /*con.Cmd.Parameters.Add(new SqlParameter("@YId", cmbYear.SelectedValue.ToString()));
            con.Cmd.Parameters.Add(new SqlParameter("@CId", cmbClass.SelectedValue.ToString()));
            con.Cmd.Parameters.Add(new SqlParameter("@SId", cmbSection.SelectedValue.ToString()));*/
            paging.SelectCommand = con.Cmd;
            sBuilder = new SqlCommandBuilder(paging);
            ds = new DataSet();
            paging.Fill(ds, "tblStdReg");
            sTable = ds.Tables["tblStdReg"];
            dGVDep.DataSource = ds.Tables["tblStdReg"].DefaultView;
            lblTotal.Text = ds.Tables["tblStdReg"].Rows.Count.ToString();
            if (sTable.Rows.Count < 1)
            {
                MessageBox.Show("No Record Found", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            con.conClose();
            Telerik.WinControls.UI.GridViewCheckBoxColumn checkBoxColumn = new Telerik.WinControls.UI.GridViewCheckBoxColumn();
            checkBoxColumn.DataType = typeof(int);
            checkBoxColumn.Name = "checkBoxColumn";
            checkBoxColumn.FieldName = "Select";
            checkBoxColumn.HeaderText = "Select?";
            dGVDep.MasterTemplate.Columns.Add(checkBoxColumn);
            /* for (int i = 0; i < dGVDep.Rows.Count; i++)
             {
                 dGVDep.Rows[i].Cells[0].ReadOnly = true;
             }*/
            dGVDep.Columns[0].Width = 70;
            dGVDep.Columns[1].Width = 100;
            dGVDep.Columns[2].Width = 100;
            dGVDep.Columns[3].Width = 100;
            dGVDep.Columns[4].Width = 100;
            dGVDep.Columns[5].Width = 100;
            dGVDep.Columns[6].Width = 100;
            dGVDep.Columns[7].Width = 100;
            dGVDep.Columns[8].Width = 100;
            dGVDep.Columns[9].Width = 100;
            dGVDep.Columns[10].Width = 100;
            dGVDep.Columns[11].Width = 100;
            dGVDep.Columns[12].Width = 100;
            dGVDep.Columns[13].Width = 100;
            dGVDep.Columns[14].Width = 100;
            dGVDep.Columns[15].Width = 100;
            dGVDep.Columns[16].Width = 100;
            dGVDep.Columns[17].Width = 100;
            dGVDep.Columns[18].Width = 100;
            dGVDep.Columns[19].Width = 100;
            dGVDep.Columns[20].Width = 100;

            dGVDep.Columns[21].Width = 100;
            dGVDep.Columns[22].Width = 100;
            dGVDep.Columns[23].Width = 100;
            dGVDep.Columns[24].Width = 100;
            dGVDep.Columns[25].Width = 100;
            dGVDep.Columns[26].Width = 100;
            dGVDep.Columns[27].Width = 100;
            dGVDep.Columns[28].Width = 100;
            dGVDep.Columns[29].Width = 100;
            dGVDep.Columns[30].Width = 100;
            dGVDep.Columns[31].Width = 100;
            dGVDep.Columns[32].Width = 100;
            dGVDep.Columns[33].Width = 100;
            dGVDep.Columns[34].Width = 100;
            dGVDep.Columns[35].Width = 100;
            dGVDep.Columns[36].Width = 100;
            dGVDep.Columns[37].Width = 100;
            dGVDep.Columns[38].Width = 100;
            dGVDep.Columns[39].Width = 100;
            dGVDep.Columns[40].Width = 100;
        }

Last column contains check box 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);*/
        }

 

You are viewing reply posted by: dharmendr 3 days ago.
Posted 3 days ago

Issue is not with closed xml.

Issue is getting value from Telerik.WinControls.UI GridView column and row.

It is not possible to create sample for this.

Please contact the Telerik support team.