I have displayed data in the datagridview with the check box
now I want to export it into excel. how to export it in excel using closed XML library
my code is following
CheckBox headerCheckBox = new CheckBox();
private void DisplayData()
{
dGV.AllowUserToAddRows = false;
dGV.Columns.Clear();
con = new SqlDbConnect();
con.SqlQuery(@"select distinct se.MarksID,SessionName,ExamType,ClassName,SectionName,SubjectName,se.AdmissionNo,sr.SName,sr.RollNo,Max,Mark from tblSetMarks as se
inner join tblSession as ses on se.SessionID=ses.SessionID
inner join tblSetExam as st on se.SetExamID=st.SetExamID
inner join tblAssignSubjects as si on se.SubjectID=si.ASID
inner join tblDefSubject as des on si.SubjectID=des.SubjectID
inner join tblStdReg as sr on se.AdmissionNo= sr.AdmissionNo
inner join tblDefClass as dc on sr.ClassID=dc.ClassID
inner join tblDefSection as ds on sr.SectionID= ds.SectionID
where se.SessionID=@SesId and se.SetExamID=@EId and se.ClassID=@CId and se.SectionID=@SId and se.SubjectID=@SbId order by AdmissionNo asc;");
con.Cmd.Parameters.Add(new SqlParameter("@SesId", this.cmbSession.SelectedValue.ToString()));
con.Cmd.Parameters.Add(new SqlParameter("@EId", this.cmbExam.SelectedValue.ToString()));
con.Cmd.Parameters.Add(new SqlParameter("@CId", this.cmbClass.SelectedValue.ToString()));
con.Cmd.Parameters.Add(new SqlParameter("@SId", this.cmbSection.SelectedValue.ToString()));
con.Cmd.Parameters.Add(new SqlParameter("@SbId", this.cmbSubjects.SelectedValue.ToString()));
paging.SelectCommand = con.Cmd;
sBuilder = new SqlCommandBuilder(paging);
ds = new DataSet();
paging.Fill(ds, "tblSetMarks");
sTable = ds.Tables["tblSetMarks"];
dGV.DataSource = ds.Tables["tblSetMarks"].DefaultView;
//dGV.ReadOnly = true;
Point headerCellLocation = this.dGV.GetCellDisplayRectangle(0, -1, true).Location;
headerCheckBox.Location = new Point(headerCellLocation.X + 8, headerCellLocation.Y + 1);
headerCheckBox.BackColor = System.Drawing.Color.White;
headerCheckBox.Size = new Size(18, 18);
headerCheckBox.Click += new EventHandler(HeaderCheckBox_Clicked);
dGV.Controls.Add(headerCheckBox);
DataGridViewCheckBoxColumn checkBoxColumn = new DataGridViewCheckBoxColumn();
checkBoxColumn.HeaderText = "";
checkBoxColumn.Width = 30;
checkBoxColumn.Name = "checkBoxColumn";
dGV.Columns.Insert(0, checkBoxColumn);
dGV.Columns[1].Width = 80;
dGV.Columns[2].Width = 100;
dGV.Columns[3].Width = 100;
dGV.Columns[4].Width = 100;
dGV.Columns[5].Width = 100;
dGV.Columns[6].Width = 200;
dGV.Columns[7].Width = 100;
dGV.Columns[8].Width = 200;
dGV.Columns[9].Width = 80;
dGV.Columns[10].Width = 80;
dGV.Columns[11].Width = 80;
dGV.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dGV.Columns[1].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
dGV.RowsDefaultCellStyle.BackColor = System.Drawing.Color.GhostWhite;
dGV.AlternatingRowsDefaultCellStyle.BackColor = System.Drawing.Color.GhostWhite;
dGV.CellBorderStyle = DataGridViewCellBorderStyle.Single;
dGV.DefaultCellStyle.SelectionBackColor = System.Drawing.Color.ForestGreen;
dGV.DefaultCellStyle.SelectionForeColor = System.Drawing.Color.White;
dGV.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
dGV.Columns[1].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
dGV.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dGV.AllowUserToResizeColumns = true;
}
private void HeaderCheckBox_Clicked(object sender, EventArgs e)
{
dGV.EndEdit();
foreach (DataGridViewRow row in dGV.Rows)
{
DataGridViewCheckBoxCell checkBox = (row.Cells["checkBoxColumn"] as DataGridViewCheckBoxCell);
checkBox.Value = headerCheckBox.Checked;
}
}
private void DataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0 && e.ColumnIndex == 0)
{
bool isChecked = true;
foreach (DataGridViewRow row in dGV.Rows)
{
if (Convert.ToBoolean(row.Cells["checkBoxColumn"].EditedFormattedValue) == false)
{
isChecked = false;
break;
}
}
headerCheckBox.Checked = isChecked;
}
}