public void cargarDataGridViewConciliacionAutomatica(DataGridView dgv, IEnumerable<Object> lista, String p_modoEdicion, bool crea_encabezados) { try { Trace.TraceInformation(dgv.ToString()); Trace.TraceInformation(lista.ToString()); if (crea_encabezados) { //dgv.Rows.Clear(); dgv.Columns.Clear(); foreach (var item in lista) { Type t = item.GetType(); PropertyInfo[] pi = t.GetProperties(); foreach (PropertyInfo p in pi) { DataGridViewColumn columna = new DataGridViewColumn(); DataGridViewCell cell = new DataGridViewTextBoxCell(); columna.CellTemplate = cell; columna.Name = p.Name; columna.HeaderText = p.Name; columna.ReadOnly = true; columna.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells; switch ( columna.Name ) { case "NIVEL": columna.Visible = false; break; case "IdArchivoTarjetaDetalle": columna.Visible = false; break; } dgv.Columns.Add(columna); } break; } DataGridViewCheckBoxColumn doWork = new DataGridViewCheckBoxColumn(); doWork.Name = "CONCILIAR"; doWork.HeaderText = "CONCILIAR"; doWork.FalseValue = "0"; doWork.TrueValue = "1"; dgv.Columns.Add(doWork); } //crea_encabezados var i=0; foreach (object item in lista) { this.progressBar1.BringToFront(); dgv.Refresh(); //System.Threading.Thread.Sleep(10); Application.DoEvents(); this.progressBar1.Increment(i++); var row = dgv.Rows.Add(); dgv.Rows[row].HeaderCell.Value = i.ToString(); Type t = item.GetType(); PropertyInfo[] pi = t.GetProperties(); foreach (PropertyInfo p in pi) { Console.WriteLine(p.Name + " " + p.GetValue(item, null)); dgv.Rows[row].Cells[p.Name].Value = p.GetValue(item, null); } if (p_modoEdicion == "SI") { dgv.Rows[row].Cells["CONCILIAR"].Value = true; } switch (dgv.Rows[row].Cells["NIVEL"].Value.ToString()) { case "-1": dgv.Rows[row].Cells["CONCILIAR"].Value = false; dgv.Rows[row].ReadOnly = true; dgv.Rows[row].DefaultCellStyle.BackColor = Color.White; dgv.Rows[row].DefaultCellStyle.ForeColor = Color.Black; break; case "1": dgv.Rows[row].DefaultCellStyle.BackColor = Color.DarkGreen; dgv.Rows[row].DefaultCellStyle.ForeColor = Color.White; dgv.Rows[row].Cells["CONCILIAR"].Value = true; break; case "2": dgv.Rows[row].DefaultCellStyle.BackColor = Color.LightBlue; break; case "3": dgv.Rows[row].DefaultCellStyle.BackColor = Color.Orange; break; case "4": dgv.Rows[row].DefaultCellStyle.BackColor = Color.OrangeRed; break; default: dgv.Rows[row].Cells["CONCILIAR"].Value = false; dgv.Rows[row].ReadOnly = true; dgv.Rows[row].DefaultCellStyle.BackColor = Color.White; dgv.Rows[row].DefaultCellStyle.ForeColor = Color.Black; break; } dgv.Rows[row].Cells["FECHA_PAGO"].Value = dgv.Rows[row].Cells["FECHA_PAGO"].Value .ToString().Remove(10); } dgv.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders); } catch (Exception ex) { var st = new StackTrace(ex, true); var frame = st.GetFrame(0); Trace.TraceError("Error Linea " + frame.GetFileLineNumber().ToString() + " columna " + frame.GetFileColumnNumber().ToString()); Trace.TraceError(ex.ToString()); throw; } }
/// <summary> /// Exports a grid to Excel /// </summary> /// <param name="dGv">The DataGridView to export to Excel</param> /// <param name="saveName">The name that the excel file will be saved as</param> /// <returns>If the grid was successfully exported and file saved</returns> public static bool ExportToExcel(DataGridView dGv, string saveName) { dgvToExport = dGv; string strExtension = ".xls"; string fullname = ""; int rowIndex = 1; int row = 0; string st1 = ""; if (dgvToExport != null) { try { saveName = checkFileExists(saveName, strExtension, pathExports); fullname = pathExports + saveName + strExtension; StreamWriter sw = new StreamWriter(fullname, false); sw.AutoFlush = true; for (int col = 0; col < dgvToExport.Columns.Count; col++) { if (dgvToExport.Columns[col].Visible == true) { if (col == 0) { sw.Write(dgvToExport.Columns[col].HeaderText.ToLower() + "\t"); } else { sw.Write(dgvToExport.Columns[col].HeaderText + "\t"); } } } for (row = 0; row < dgvToExport.Rows.Count; row++) { if (rowIndex <= dgvToExport.Rows.Count) rowIndex++; st1 = "\n"; for (int col = 0; col < dgvToExport.Columns.Count; col++) { if (dgvToExport.Columns[col].Visible == true) { if (dgvToExport.Rows[row].Cells[col].Value == null) { st1 += "NULL"; } else { st1 += dgvToExport.Rows[row].Cells[col].Value.ToString(); } st1 = st1 + "\t"; } } sw.Write(st1); } sw.Close(); FileInfo fil = new FileInfo(fullname); if (fil.Exists == true) { if (MessageBox.Show("Grid has been exported to file" + Environment.NewLine + fil.FullName + Environment.NewLine + "Do you want to open file now?", "Export To Excel", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { CCFBGlobal.openDocumentOutsideCCFB(fil.FullName); } return true; } } catch (Exception ex) { FileInfo file = new FileInfo(pathExports + saveName + strExtension); appendErrorToErrorReport(dgvToExport.ToString(), ex.GetBaseException().ToString()); MessageBox.Show("ERROR: Could not export the grid.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return false; } } return false; }