//DXGRIDVIEW EXCEL ACMA HIZLI
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using Excel;
using System;
using System.Data;
using System.IO;
using System.Windows.Forms;
//F:\googledrive\programming\csharp içinde iki adet dll var Excel.dll ve ICSharpCode.SharpZipLib.dll referans olarak ekle
public void SET_GC_DATASOURCE(DataTable DT, string GVNAME){
try
{
if (DT == null || DT.Rows.Count <= 0)
throw new Exception("EXCEL VERİLERİ OKUNAMADI.");
gridControl1.DataSource = DT;
gridControl1.RefreshDataSource();
gridControl1.Refresh();
INITIALIZE_GRID(gridControl1, gridView1, new GRIDOPTIONS() { ALLOWSORT = true, CLEARSORTINFO = true, ENABLEGROUPPANELMENU = true, HORIZONTALSCROOLVISIBILITY = true, SHOWGROUPPANEL = true }, GVNAME);
//for (int i = 0; i < COLUMNS.Length / 2; i++)
//{
// _gv_EKSTRE.Columns[i].Caption = (string)COLUMNS[i, 0];
// _gv_EKSTRE.Columns[i].Visible = (bool)COLUMNS[i, 1];
//}
}
catch (Exception E)
{
MessageBox.Show(E.Message, "HATA!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
public string READ_FILE(string title){
try
{
string[] fileformats = new string[] { "xls", "xlsx" };
OpenFileDialog openFileDialogEKSTRE = new OpenFileDialog();
openFileDialogEKSTRE.Title = title;
//openFileDialogEKSTRE.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);//@"C:\Users\BilMark Yazılım\Desktop";//AppDomain.CurrentDomain.BaseDirectory;
string filterText = string.Empty;
for (int i = 0; i < fileformats.Length; i++)
{
filterText += "(*." + fileformats[i] + "*)|*." + fileformats[i];
if (i != fileformats.Length - 1)
filterText += "|";
}
openFileDialogEKSTRE.Filter = "Excel Files|*.xls;*.xlsx;";
//openFileDialogEKSTRE.FileName = "1001.xlsx";
openFileDialogEKSTRE.FilterIndex = 0;
openFileDialogEKSTRE.RestoreDirectory = true;
if (openFileDialogEKSTRE.ShowDialog() == DialogResult.OK)
{
return openFileDialogEKSTRE.FileName;
}
else return "closed";
}
catch (Exception E)
{
return null;
}
}
public DataTable CONVERT_EXCEL_TO_DATATABLE(bool ISXLSX, bool ISFIRSTROWCOLUMNNAME, string PATH, int RETURNEDTABLENR){
FileStream stream = File.Open(PATH, FileMode.Open, FileAccess.Read);
IExcelDataReader excelReader = null;
if (ISXLSX)
excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
else
excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
excelReader.IsFirstRowAsColumnNames = ISFIRSTROWCOLUMNNAME;
DataSet result = excelReader.AsDataSet();
////5. Data Reader methods
//while (excelReader.Read())
//{
// //excelReader.GetInt32(0);
//}
excelReader.Close();
return result.Tables[RETURNEDTABLENR];}
public void INITIALIZE_GRID(GridControl GC, GridView GV, GRIDOPTIONS GO, string XMLLAYOUTNAME){
try
{
RESTORE_GRIDVIEW_LAYOUTS(GV, XMLLAYOUTNAME);
//GV.RestoreLayoutFromXml(AppDomain.CurrentDomain.BaseDirectory + "\\Layouts\\" + XMLLAYOUTNAME + ".xml");
if (GO.CLEARSORTINFO)
GV.SortInfo.Clear();
GV.ClearColumnsFilter();
GV.FocusedRowHandle = 0;
}
catch { }
GV.OptionsBehavior.Editable = GO.EDITABLE;
GV.OptionsBehavior.AllowAddRows = GO.ALLOWADDROW ? DevExpress.Utils.DefaultBoolean.True : DevExpress.Utils.DefaultBoolean.False;
GV.OptionsBehavior.AllowDeleteRows = GO.ALLOWDELETEROW ? DevExpress.Utils.DefaultBoolean.True : DevExpress.Utils.DefaultBoolean.False;
GV.OptionsCustomization.AllowFilter = GO.ALLOWFILTER;
GV.OptionsCustomization.AllowQuickHideColumns = GO.ALLOWQUICKHIDECOLUMNS;
GV.OptionsCustomization.AllowSort = GO.ALLOWSORT;
GV.OptionsMenu.EnableColumnMenu = GO.ENABLECOLUMNMENU;
GV.OptionsMenu.EnableFooterMenu = GO.ENABLEFOOTERMENU;
GV.OptionsMenu.EnableGroupPanelMenu = GO.ENABLEGROUPPANELMENU;
GV.OptionsSelection.MultiSelect = GO.MULTISELECT;
if (GO.MULTISELECTMODECELLSELECT)
GV.OptionsSelection.MultiSelectMode = GridMultiSelectMode.CellSelect;
else if (GO.MULTISELECTMODECHECKBOXSELECT)
GV.OptionsSelection.MultiSelectMode = GridMultiSelectMode.CheckBoxRowSelect;
else if (GO.MULTISELECTMODEWORSELECT)
GV.OptionsSelection.MultiSelectMode = GridMultiSelectMode.RowSelect;
GV.OptionsView.ColumnAutoWidth = GO.COLUMNAUTOWIDTH;
GV.OptionsView.ShowAutoFilterRow = GO.SHOWAUTOFILTERROW;
GV.OptionsView.ShowGroupPanel = GO.SHOWGROUPPANEL;
GV.OptionsView.ShowFilterPanelMode = GO.SHOWFILTERPANELMODE ? DevExpress.XtraGrid.Views.Base.ShowFilterPanelMode.ShowAlways : DevExpress.XtraGrid.Views.Base.ShowFilterPanelMode.Default;}
public void RESTORE_GRIDVIEW_LAYOUTS(GridView GV, string XMLLAYOUTNAME){
try
{
GV.RestoreLayoutFromXml(AppDomain.CurrentDomain.BaseDirectory + "\\Layouts\\" + XMLLAYOUTNAME + ".xml");
}
catch { }}
public class GRIDOPTIONS{
#region OPTIONSBEHAVIOR
public bool EDITABLE { get; set; }
public bool ALLOWADDROW { get; set; }
public bool ALLOWDELETEROW { get; set; }
#endregion
#region OPTIONSCUSTOMIZATION
public bool ALLOWFILTER { get; set; }
public bool ALLOWSORT { get; set; }
public bool ALLOWQUICKHIDECOLUMNS { get; set; }
#endregion
#region OPTIONSMENU
public bool ENABLECOLUMNMENU { get; set; }
public bool ENABLEGROUPPANELMENU { get; set; }
public bool ENABLEFOOTERMENU { get; set; }
#endregion
#region OPTIONSSELECTION
public bool MULTISELECT { get; set; }
public bool MULTISELECTMODECELLSELECT { get; set; }
public bool MULTISELECTMODECHECKBOXSELECT { get; set; }
public bool MULTISELECTMODEWORSELECT { get; set; }
#endregion
#region OPTIONSVIEW
public bool COLUMNAUTOWIDTH { get; set; }
public bool SHOWGROUPPANEL { get; set; }
public bool SHOWAUTOFILTERROW { get; set; }
public bool SHOWFILTERPANELMODE { get; set; }
#endregion
#region BEHAVIOR
public bool HORIZONTALSCROOLVISIBILITY { get; set; }
#endregion
public bool CLEARSORTINFO { get; set; }
public bool CLEARFILTERINFO { get; set; }}
private void simpleButton1_Click(object sender, EventArgs e){
gridControl1.DataSource = null;
gridView1.Columns.Clear();
string FILEPATH = READ_FILE("gridView1");
SET_GC_DATASOURCE(CONVERT_EXCEL_TO_DATATABLE(true, true, FILEPATH, 0), "gridView1");}
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using Excel;
using System;
using System.Data;
using System.IO;
using System.Windows.Forms;
//F:\googledrive\programming\csharp içinde iki adet dll var Excel.dll ve ICSharpCode.SharpZipLib.dll referans olarak ekle
public void SET_GC_DATASOURCE(DataTable DT, string GVNAME){
try
{
if (DT == null || DT.Rows.Count <= 0)
throw new Exception("EXCEL VERİLERİ OKUNAMADI.");
gridControl1.DataSource = DT;
gridControl1.RefreshDataSource();
gridControl1.Refresh();
INITIALIZE_GRID(gridControl1, gridView1, new GRIDOPTIONS() { ALLOWSORT = true, CLEARSORTINFO = true, ENABLEGROUPPANELMENU = true, HORIZONTALSCROOLVISIBILITY = true, SHOWGROUPPANEL = true }, GVNAME);
//for (int i = 0; i < COLUMNS.Length / 2; i++)
//{
// _gv_EKSTRE.Columns[i].Caption = (string)COLUMNS[i, 0];
// _gv_EKSTRE.Columns[i].Visible = (bool)COLUMNS[i, 1];
//}
}
catch (Exception E)
{
MessageBox.Show(E.Message, "HATA!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
public string READ_FILE(string title){
try
{
string[] fileformats = new string[] { "xls", "xlsx" };
OpenFileDialog openFileDialogEKSTRE = new OpenFileDialog();
openFileDialogEKSTRE.Title = title;
//openFileDialogEKSTRE.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);//@"C:\Users\BilMark Yazılım\Desktop";//AppDomain.CurrentDomain.BaseDirectory;
string filterText = string.Empty;
for (int i = 0; i < fileformats.Length; i++)
{
filterText += "(*." + fileformats[i] + "*)|*." + fileformats[i];
if (i != fileformats.Length - 1)
filterText += "|";
}
openFileDialogEKSTRE.Filter = "Excel Files|*.xls;*.xlsx;";
//openFileDialogEKSTRE.FileName = "1001.xlsx";
openFileDialogEKSTRE.FilterIndex = 0;
openFileDialogEKSTRE.RestoreDirectory = true;
if (openFileDialogEKSTRE.ShowDialog() == DialogResult.OK)
{
return openFileDialogEKSTRE.FileName;
}
else return "closed";
}
catch (Exception E)
{
return null;
}
}
public DataTable CONVERT_EXCEL_TO_DATATABLE(bool ISXLSX, bool ISFIRSTROWCOLUMNNAME, string PATH, int RETURNEDTABLENR){
FileStream stream = File.Open(PATH, FileMode.Open, FileAccess.Read);
IExcelDataReader excelReader = null;
if (ISXLSX)
excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
else
excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
excelReader.IsFirstRowAsColumnNames = ISFIRSTROWCOLUMNNAME;
DataSet result = excelReader.AsDataSet();
////5. Data Reader methods
//while (excelReader.Read())
//{
// //excelReader.GetInt32(0);
//}
excelReader.Close();
return result.Tables[RETURNEDTABLENR];}
public void INITIALIZE_GRID(GridControl GC, GridView GV, GRIDOPTIONS GO, string XMLLAYOUTNAME){
try
{
RESTORE_GRIDVIEW_LAYOUTS(GV, XMLLAYOUTNAME);
//GV.RestoreLayoutFromXml(AppDomain.CurrentDomain.BaseDirectory + "\\Layouts\\" + XMLLAYOUTNAME + ".xml");
if (GO.CLEARSORTINFO)
GV.SortInfo.Clear();
GV.ClearColumnsFilter();
GV.FocusedRowHandle = 0;
}
catch { }
GV.OptionsBehavior.Editable = GO.EDITABLE;
GV.OptionsBehavior.AllowAddRows = GO.ALLOWADDROW ? DevExpress.Utils.DefaultBoolean.True : DevExpress.Utils.DefaultBoolean.False;
GV.OptionsBehavior.AllowDeleteRows = GO.ALLOWDELETEROW ? DevExpress.Utils.DefaultBoolean.True : DevExpress.Utils.DefaultBoolean.False;
GV.OptionsCustomization.AllowFilter = GO.ALLOWFILTER;
GV.OptionsCustomization.AllowQuickHideColumns = GO.ALLOWQUICKHIDECOLUMNS;
GV.OptionsCustomization.AllowSort = GO.ALLOWSORT;
GV.OptionsMenu.EnableColumnMenu = GO.ENABLECOLUMNMENU;
GV.OptionsMenu.EnableFooterMenu = GO.ENABLEFOOTERMENU;
GV.OptionsMenu.EnableGroupPanelMenu = GO.ENABLEGROUPPANELMENU;
GV.OptionsSelection.MultiSelect = GO.MULTISELECT;
if (GO.MULTISELECTMODECELLSELECT)
GV.OptionsSelection.MultiSelectMode = GridMultiSelectMode.CellSelect;
else if (GO.MULTISELECTMODECHECKBOXSELECT)
GV.OptionsSelection.MultiSelectMode = GridMultiSelectMode.CheckBoxRowSelect;
else if (GO.MULTISELECTMODEWORSELECT)
GV.OptionsSelection.MultiSelectMode = GridMultiSelectMode.RowSelect;
GV.OptionsView.ColumnAutoWidth = GO.COLUMNAUTOWIDTH;
GV.OptionsView.ShowAutoFilterRow = GO.SHOWAUTOFILTERROW;
GV.OptionsView.ShowGroupPanel = GO.SHOWGROUPPANEL;
GV.OptionsView.ShowFilterPanelMode = GO.SHOWFILTERPANELMODE ? DevExpress.XtraGrid.Views.Base.ShowFilterPanelMode.ShowAlways : DevExpress.XtraGrid.Views.Base.ShowFilterPanelMode.Default;}
public void RESTORE_GRIDVIEW_LAYOUTS(GridView GV, string XMLLAYOUTNAME){
try
{
GV.RestoreLayoutFromXml(AppDomain.CurrentDomain.BaseDirectory + "\\Layouts\\" + XMLLAYOUTNAME + ".xml");
}
catch { }}
public class GRIDOPTIONS{
#region OPTIONSBEHAVIOR
public bool EDITABLE { get; set; }
public bool ALLOWADDROW { get; set; }
public bool ALLOWDELETEROW { get; set; }
#endregion
#region OPTIONSCUSTOMIZATION
public bool ALLOWFILTER { get; set; }
public bool ALLOWSORT { get; set; }
public bool ALLOWQUICKHIDECOLUMNS { get; set; }
#endregion
#region OPTIONSMENU
public bool ENABLECOLUMNMENU { get; set; }
public bool ENABLEGROUPPANELMENU { get; set; }
public bool ENABLEFOOTERMENU { get; set; }
#endregion
#region OPTIONSSELECTION
public bool MULTISELECT { get; set; }
public bool MULTISELECTMODECELLSELECT { get; set; }
public bool MULTISELECTMODECHECKBOXSELECT { get; set; }
public bool MULTISELECTMODEWORSELECT { get; set; }
#endregion
#region OPTIONSVIEW
public bool COLUMNAUTOWIDTH { get; set; }
public bool SHOWGROUPPANEL { get; set; }
public bool SHOWAUTOFILTERROW { get; set; }
public bool SHOWFILTERPANELMODE { get; set; }
#endregion
#region BEHAVIOR
public bool HORIZONTALSCROOLVISIBILITY { get; set; }
#endregion
public bool CLEARSORTINFO { get; set; }
public bool CLEARFILTERINFO { get; set; }}
private void simpleButton1_Click(object sender, EventArgs e){
gridControl1.DataSource = null;
gridView1.Columns.Clear();
string FILEPATH = READ_FILE("gridView1");
SET_GC_DATASOURCE(CONVERT_EXCEL_TO_DATATABLE(true, true, FILEPATH, 0), "gridView1");}