I am trying to add a feature into my application where users can choose the sheets from the comboBox. But I am hitting some bumps, I need some help! I was able to read the the excel file previously as I already sheet the default sheetname. But now I am able to get the sheetname into my comboBox, but I can't seem to read the excel file now? Please help me
public static DataTable ExcelToDataTable (string fileName)
{
using (var stream = File.Open(fileName, FileMode.Open, FileAccess.Read))
{
using (var reader = ExcelReaderFactory.CreateReader(stream))
{
var result = reader.AsDataSet(new ExcelDataSetConfiguration()
{
UseColumnDataType = true,
ConfigureDataTable = (data) => new ExcelDataTableConfiguration()
{
UseHeaderRow = true
}
});
DataTableCollection table = result.Tables;
DataTable resultTable = table["Sheet1"];
return resultTable;
}
}
}
private void btnOpen_Click(object sender, EventArgs e)
{
try
{
using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "Excel 2003 Worksheet|*.xls|Excel 2007 Worksheet|*.xlsx", ValidateNames = true, Multiselect = false })
{
if (ofd.ShowDialog() == DialogResult.OK)
{
dataGridView1.DataSource = ExcelToDataTable(ofd.FileName);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
And Now
public static DataSet ExcelToDataTable (string fileName)
{
using (var stream = File.Open(fileName, FileMode.Open, FileAccess.Read))
{
using (var reader = ExcelReaderFactory.CreateReader(stream))
{
var result = reader.AsDataSet(new ExcelDataSetConfiguration()
{
UseColumnDataType = true,
ConfigureDataTable = (data) => new ExcelDataTableConfiguration()
{
UseHeaderRow = true
}
});
return result;
}
}
}
private void btnOpen_Click(object sender, EventArgs e)
{
try
{
using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "Excel 2003 Worksheet|*.xls|Excel 2007 Worksheet|*.xlsx", ValidateNames = true, Multiselect = false })
{
if (ofd.ShowDialog() == DialogResult.OK)
{
comboBox1.Items.Clear();
foreach (DataTable dt in ExcelToDataTable(ofd.FileName).Tables)
{
comboBox1.Items.Add(dt.TableName);
}
DataTableCollection table = ExcelToDataTable(ofd.FileName).Tables;
DataTable resultTable = ExcelToDataTable(ofd.FileName).Tables[comboBox1.SelectedIndex];
dataGridView1.DataSource = resultTable
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
May I know what went wrong? I am getting an error of Cannot find table -1 but I can see the sheetname in the excel just not the content
At first use a DataSet property in your form:
private DataSet ExcelDateSet { get; set; }
And add a method to read a table from it like this:
private DataTable GetExcelDataTable(string sheetName)
{
if (string.IsNullOrEmpty(sheetName) || !ExcelDateSet.Tables.Contains(sheetName))
{
// Notify user to select a sheet-name from your ComboBox!
// or you can return first-sheet info as default like this:
// return ExcelDateSet.Tables[0];
}
return ExcelDateSet.Tables[sheetName];
}
then set its value in your btnOpen_Click like this:
ExcelDataSet = ExcelToDataTable(ofd.FileName);
dataGridView1.DataSource = GetExcelDataTable(comboBox1.SelectedText);
dataGridView1.DataBind();
Related
resultgrid and IsfirstrowAsColumnNames
private void Form4_Load(object sender, EventArgs e, object ResultGrid )
{
using (OpenFileDialog dialog = new OpenFileDialog()
{ Filter = "Excel workbook|*xls", ValidateNames = true })
{
if (dialog.ShowDialog()==DialogResult.OK)
{
FileStream fileStream = File.Open(dialog.FileName, FileMode.Open, FileAccess.Read);
IExcelDataReader reader = ExcelReaderFactory.CreateBinaryReader(fileStream);
reader.IsFirstRowAsColumnNames()() = false;
ds = reader.AsDataSet();
ResultGrid.DataSource() = ds.Tables[0];
}
}
}
On a windows form I have two unbound data grid view columns each with their own header and I am trying read a csv file in a datatable and bind it to the grid.
Only I don't know the logic involved. I don't know how to bind the data to the 2 data grid view columns. There is a lot of samples out there of how to bind csv data to a grid view but they deal with headers that get read from inside a file and not unbound ones except for this but that's with asp. Display data on grid view column programmatically
Here is the data from the csv file.
Address,76 Douglas St Wakecorn
Property name,Wakecorn University
Building,C Block
Room,C2.18
Here is the code for the class that reads it.
public class Setting
{
private DataTable _dt { get; set; }
public DataTable ProcessSettingFileCMD(string filePath)
{
if (_dt == null)
{
populateControlWithCSVData(filePath);
}
}
private void populateControlWithCSVData(string filePath)
{
_dt = new DataTable();
string[] lines = File.ReadAllLines(filePath);
if(lines.Length > 0)
{
for(int row = 1; row < lines.Length; row++)
{
string[] dataWords = lines[row].Split(',');
DataRow dr = _dt.NewRow();
foreach (string word in lines)
{
dr[word] = dataWords[row++];
}
_dt.Rows.Add(dr);
}
}
}
}
Here is the form
private void mnuOpen_Click(object sender, EventArgs e)
{
openFD.InitialDirectory = "C:\\";
openFD.ShowDialog();
_dt = _objSetting.ProcessSettingFileCMD(openFD.FileName);
if (_dt.Rows.Count > 0)
{
gvSettings.DataSource = _dt;
}
}
The error being returned is here on dr[word] = dataWords[row++];
System.ArgumentException: 'Column 'Address,76 Douglas St Wakecorn'
does not belong to table .
As per the guidance and options provided I have taken out the columns made in the designer and instead instanced them when reading a CSV file.
Also I have used a CsvFileReader child class to bind it to a string list and then to a data table. Highly useful! http://www.blackbeltcoder.com/Articles/files/reading-and-writing-csv-files-in-c
Here is the rewritten code.
Settings Form.
public partial class frmSettings : Form
{
protected string FileName;
protected bool Modified;
Setting _objSetting = new Setting();
OpenFileDialog openFD = new OpenFileDialog();
DataTable _dt = new DataTable();
public frmSettings()
{
InitializeComponent();
}
private void mnuOpen_Click(object sender, EventArgs e)
{
Cursor = Cursors.WaitCursor;
try
{
openFD.Title = "Open a CSV File";
if (SaveIfModified())
{
if (openFD.ShowDialog(this) == DialogResult.OK)
{
_dt = _objSetting.ProcessSettingFileCMD(openFD.FileName);
if (_dt.Rows.Count > 0)
{
gvSettings.DataSource = _dt;
}
}
}
FileName = openFD.FileName;
Modified = false;
}
catch (Exception ex)
{
Debug.WriteLine(String.Format("Error reading from {0}.\r\n\r\n{1}", FileName, ex.Message));
}
finally
{
Cursor = Cursors.Default;
}
}
}
Here is the Settings class again.
public class Setting
{
private DataTable _dt;
DataColumn _dclColumnDescription = new DataColumn("Description", typeof(string));
DataColumn _dclColumnData = new DataColumn("Data", typeof(string));
public DataTable ProcessSettingFileCMD(string filePath)
{
if (_dt == null)
{
populateControlWithCSVData(filePath);
}
}
private void populateControlWithCSVData(string filePath)
{
_dt = new DataTable();
List<string> columns = new List<string>();
using (var reader = new CsvFileReader(filePath))
{
_dt.Columns.Add(_dclColumnDescription);
_dt.Columns.Add(_dclColumnData);
while (reader.ReadRow(columns))
{
_dt.Rows.Add(columns.ToArray());
}
}
}
}
This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 5 years ago.
I am using the ExcelDataReader extension to import excel files to C# datagridview. I am able to open the file and get it's sheets, but whenever I try to click on the sheet, My program breaks and says it's datatable is null. Please help me.
I am not sure if it's the var that is causing the problem, or that I didn't update the excel data into the datatable
namespace Dashboard {
public partial class Import_Excel : Form {
public Import_Excel() {
InitializeComponent();
}
DataSet result;
private void btnLoad_Click(object sender, EventArgs e) {
using (OpenFileDialog ofd = new OpenFileDialog()) {
if (ofd.ShowDialog() == DialogResult.OK) {
using (var stream = File.Open(ofd.FileName, FileMode.Open, FileAccess.Read)) {
using (var reader = ExcelReaderFactory.CreateReader(stream)) {
var result = reader.AsDataSet(new ExcelDataSetConfiguration() {
UseColumnDataType = true,
ConfigureDataTable = (data) => new ExcelDataTableConfiguration() {
UseHeaderRow = true
}
});
cboSheet.Items.Clear();
DataTableCollection table = result.Tables;
foreach (DataTable dt in result.Tables) {
cboSheet.Items.Add(dt.TableName);
}
reader.Close();
}
}
}
}
}
private void cboSheet_SelectedIndexChanged(object sender, EventArgs e) {
dataGridView1.DataSource = result.Tables[cboSheet.SelectedIndex];
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) {
}
}
}
If method cboSheet_SelectedIndexChanged happens before btnLoad_Click() than result will be null thus result.Tables throws NullReferenceException
private void cboSheet_SelectedIndexChanged(object sender, EventArgs e)
{
if(result != null) {
dataGridView1.DataSource = result.Tables[cboSheet.SelectedIndex];
}
}
Or just use: dataGridView1.DataSource = result?.Tables[cboSheet.SelectedIndex]; (will set it to null)
I am working on an csv changing tool for magento.
This tool has to change some values of a csv file.
I have already imported the csv into a gridview.
My question is how do you Read a value of a single cell and change it from a gridview.
I am using the windows form c#
i want something like this:
imput:
EAN,Product code,name
363738492,MT-01234,iphone case
153289234,MT-89854,samsung case
876253483,PO-43466,network cable
output:
EAN,sku,name
363738492,MT-01234,iphone case
153289234,MT-89854,samsung case
876253483,PO-43466,network cable
this is my working import code:
private void Import_Click(object sender, EventArgs e)
{
OpenFileDialog fdlg = new OpenFileDialog();
fdlg.Title = "Select file";
fdlg.InitialDirectory = #"c:\";
fdlg.FileName = txtFileName.Text;
fdlg.Filter = "Text and CSV Files(*.txt, *.csv)|*.txt;*.csv|Text Files(*.txt)|*.txt|CSV Files(*.csv)|*.csv|All Files(*.*)|*.*";
fdlg.FilterIndex = 1;
fdlg.RestoreDirectory = true;
if (fdlg.ShowDialog() == DialogResult.OK)
{
txtFileName.Text = fdlg.FileName;
Import();
Application.DoEvents();
}
}
public static DataTable GetDataTable(string strFileName)
{
ADODB.Connection oConn = new ADODB.Connection();
oConn.Open("Provider=Microsoft.Jet.OleDb.4.0; Data Source = " + System.IO.Path.GetDirectoryName(strFileName) + "; Extended Properties = \"Text;HDR=YES;FMT=Delimited\";", "", "", 0);
string strQuery = "SELECT * FROM [" + System.IO.Path.GetFileName(strFileName) + "]";
ADODB.Recordset rs = new ADODB.Recordset();
System.Data.OleDb.OleDbDataAdapter adapter = new System.Data.OleDb.OleDbDataAdapter();
DataTable dt = new DataTable();
rs.Open(strQuery, "Provider=Microsoft.Jet.OleDb.4.0; Data Source = " + System.IO.Path.GetDirectoryName(strFileName) + "; Extended Properties = \"Text;HDR=YES;FMT=Delimited\";",
ADODB.CursorTypeEnum.adOpenForwardOnly, ADODB.LockTypeEnum.adLockReadOnly, 1);
adapter.Fill(dt, rs);
return dt;
}
private void Import()
{
if (txtFileName.Text.Trim() != string.Empty)
{
try
{
DataTable dt = GetDataTable(txtFileName.Text);
dgvGv.DataSource = dt.DefaultView;
dgvGv2.DataSource = dt.DefaultView;
dgvGv3.DataSource = dt.DefaultView;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
this is my working export code:
private void button1_Click(object sender, EventArgs e)
{
// Don't save if no data is returned
if (dgvGv.Rows.Count == 0)
{
return;
}
StringBuilder sb = new StringBuilder();
// Column headers
string columnsHeader = "";
for (int i = 0; i < dgvGv.Columns.Count; i++)
{
columnsHeader += dgvGv.Columns[i].Name + ",";
}
sb.Append(columnsHeader + Environment.NewLine);
// Go through each cell in the datagridview
foreach (DataGridViewRow dgvRow in dgvGv.Rows)
{
// Make sure it's not an empty row.
if (!dgvRow.IsNewRow)
{
for (int c = 0; c < dgvRow.Cells.Count; c++)
{
// Append the cells data followed by a comma to delimit.
sb.Append(dgvRow.Cells[c].Value + ",");
}
// Add a new line in the text file.
sb.Append(Environment.NewLine);
}
}
// Load up the save file dialog with the default option as saving as a .csv file.
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "CSV files (*.csv)|*.csv";
if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
// If they've selected a save location...
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(sfd.FileName, false))
{
// Write the stringbuilder text to the the file.
sw.WriteLine(sb.ToString());
}
}
// Confirm to the user it has been completed.
MessageBox.Show("CSV file saved.");
}
feel free to comment for any questions.
/Select CSV File from drop down then click Import from /In Button Clink Event
private void btnimportexcel_Click(object sender, EventArgs e)
{
string source = string.Empty;
source = cmbImportsource.Text;
if (!string.IsNullOrEmpty(source ))
{
string smsfilename=string .Empty ;
OpenFileDialog of = new OpenFileDialog();
DialogResult dlresult;
of.InitialDirectory = Environment.SpecialFolder.Desktop.ToString ();
switch (source)
{
case "EXCEL":
of.Filter = "Excel File(*.xlsx,*.xls)|*.xlsx;*.xls|All Files(*.*)|*.*";
of.Title = "Select Excel File...";
dlresult = of.ShowDialog();
if (dlresult == DialogResult.OK )
{
smsfilename = of.FileName;
if (System.IO.File.Exists(smsfilename))
{
getRecordFromXcel(smsfilename);
}
}
break;
case "CSV":
//"Text and CSV Files(*.txt, *.csv)|*.txt;*.csv|Text Files(*.txt)|*.txt|CSV Files(*.csv)|*.csv|All Files(*.*)|*.*";
of.Filter = "CSV Files(*.csv)|*.csv|All Files(*.*)|*.*";
of.Title = "Select Excel File...";
dlresult = of.ShowDialog();
if (dlresult == DialogResult.OK )
{
smsfilename = of.FileName;
if (System.IO.File.Exists(smsfilename))
{
getRecordFromCSV(smsfilename);
}
}
break;
case "TEXT FILE":
break;
}
}
//When File Selected
private void getRecordFromCSV(string file)
{
String textLine = string.Empty;
String[] splitLine;
bool columncreater = false;
try
{
StreamReader objReaders = new StreamReader(file);
dataGridView1.DataSource = null;
dataGridView1.Columns.Clear();
dataGridView1.Rows.Clear();
int datagridrowindex =-1;
do
{
textLine = objReaders.ReadLine();
datagridrowindex= datagridrowindex + 1;
if (textLine != "")
{
splitLine = textLine.Split(',');
//if (splitLine[0] != "" || splitLine[1] != "")
//{
// dataGridView1.Rows.Add(splitLine[0]);
//}
if (columncreater ==false )
{
for (int i = 0; i < splitLine.Length; i++)
{
dataGridView1.Columns.Add("C" + i + "", "C" + i + "");
}
columncreater = true;
}
dataGridView1.Rows.Add(splitLine);
int cc = dataGridView1.Columns.Count;
int rr = dataGridView1.Rows.Count;
}
} while (objReaders.Peek() != -1);
}
catch (Exception ex)
{
}
}
i am using telerik control(data grid view) in my project. but when i want to add new row in data grid, the text of previous rows (bindingSourceService.DataSource = dtservice , bindingSourceUnit.DataSource = dtunit) disappear.
my datagridview has 3 combobox column.
what is wrong? please help me.
my codes:
public void FactorLoad(object sender, EventArgs e)
{
try
{
var cb = new CategoriBll();
DataTable dtcategori = cb.GetAllDataCategori();
bindingSourceCategouri.DataSource = dtcategori;
}
catch (Exception ex)
{
ExceptionkeeperBll.LogFileWrite(ex);
}
}
private void DataGridViewFactorCellValueChanged(object sender, GridViewCellEventArgs e)
{
try
{
var row = DataGridViewFactor.CurrentRow;
if ((row != null) && (row.Cells[0].IsCurrent))
{
var categoryId = Convert.ToInt32(DataGridViewFactor.CurrentRow.Cells[0].Value);
var sb = new CategoriOptionBll();
DataTable dtservice = sb.ServiceGetById(categoryId);
bindingSourceService.DataSource = dtservice;
}
if ((row != null) && (row.Cells[1].IsCurrent))
{
var serviceId = Convert.ToInt32(DataGridViewFactor.CurrentRow.Cells[1].Value);
var cbi = new CostBll();
var dtunit = cbi.CostById(serviceId);
bindingSourceUnit.DataSource = dtunit;
}
}
catch (Exception ex)
{
ExceptionkeeperBll.LogFileWrite(ex);
}
}
private void DataGridViewFactorCellEditorInitialized(object sender, GridViewCellEventArgs e)
{
try
{
var row = DataGridViewFactor.CurrentRow;
if ((row != null) && (row.Cells[0].IsCurrent))
{
var categoryId = Convert.ToInt32(DataGridViewFactor.CurrentRow.Cells[0].Value);
var sb = new CategoriOptionBll();
DataTable dtservice = sb.ServiceGetById(categoryId);
bindingSourceService.DataSource = dtservice;
}
if ((row != null) && (row.Cells[1].IsCurrent))
{
var serviceId = Convert.ToInt32(DataGridViewFactor.CurrentRow.Cells[1].Value);
var cbi = new CostBll();
var dtunit = cbi.CostById(serviceId);
bindingSourceUnit.DataSource = dtunit;
}
}
catch (Exception ex)
{
ExceptionkeeperBll.LogFileWrite(ex);
}
}
It will disappear because there will be only 1 value in the data source. Try adding value to the data source rather then assigning single value.