Read from text file and show in table C# form - c#

I want to read data from a text file and show in a table (much like the DataGrid / DataGridView classes). The txt file must be local and not sql based (like the mentioned classes).
Doing this in C# windows form.
Any ideas?
If you have any questions I will edit this question as needed.
EDIT:
This seems to work:
this.DataGridView1.Rows(1).Cells(1).Value = stringReader;

Open up a filestream and parse your values into a DataTable... then set your grid's datasource to the datatable. Try that out and update if you have any problems :)

Related

Write to an Excel file while it's open

I'm developing a WindowsForm application where I am storing an accumulator on a destinated variable. Actually, my code is working but what I want to do now is to see the accumulator integer on an Excell cell in real-time.
So I would like to know if there is a way to write/export values from my Form to my Excel file while it is open. The idea is to see the cells being refreshed every time I send the data.
Actually, I am storing the values but with the only condition that the file is closed, so I'm curious if this is possible?
This are the actual lines of code that I have:
SLDocument sl = new SLDocument (#"D:\Excel.xlsx"); sl.SetCellValue("A2","Accumulator");
sl.SetCellValue("A3", myaccumulator);
sl.Save();
I just fixed the problem in case that one of you want to handle with data as this way, i just created other File and programmed a Macro with vba to export the data of the closed File, so now I can see the cells being refreshed every time with one Click button.
Thanks for your help, regards to everyone.

Flow Document, Dataset, or data grid view

I have looked everywhere and I can't seem to find how to make a simple table in C# winforms. Some say a Data set, a Data grid view, or a flow document. I want to just simply transfer values (scores, in my case.) to an XML doc and display the values on a table in a form. I already found out how to transfer values to a.txt file using the Stream Reader class. Is there a similar way I could do the same with data and a table? Do I have to make a SQL Server? I didn't know this would be so difficult to find an answer to... Thank you, this really helps.
Have you tried ListView? You can parse your txt file using StreamReader, and keep the scores in list i.e.: List<score> scores. Then update your ListView, in a foreach cycle, for example:
scoresListView.Clear();
foreach(var item in scores)
{
scoresListView.Add(item);
}
Unfortunately, ListView in WinForm does not allow design-time data binding. There's a good article about this: http://www.codeproject.com/Articles/10008/Data-binding-a-ListView

Hidden DataGridView column doesn't display data when made visible again

I have a DataGridView that is bound to and XML file, the XML file may contain a certain field, but may not. If the field is there I want to show the relevant DataGridView column, if not I want to hide it.
I've achieved this by setting the columns visible property. The issue I have is, if I load an XML file without the field (so the column is made not visible), then load an XML file with the field, the column is visible again but the data doesn't appear for this column.
I'm using the below code to load the XML files.
xmlDataGridView.AutoGenerateColumns = false;
xmlDataSet.ReadXml(_tempAccessXMLFile);
xmlDataGridView.DataSource = xmlDataSet;
xmlDataGridView.DataMember = "key";
xmlDataGridView.Refresh();
Any help would be appreciated a lot. I'm sure I'm missing something obvious. Thanks,
(Also, apologies if this isn't a well formatted or unclear, I'm not very good at wording things)
If the column name loads in grid but data is not displayed then try using BindingSource. I have tested it using books.xml sample XML File and works fine
xmlDataGridView.AutoGenerateColumns = false;
XmlReader xmlFile = XmlReader.Create(_tempAccessXMLFile, new XmlReaderSettings());
dataSet.ReadXml(xmlFile);
BindingSource bs = new BindingSource();
bs.DataSource = dataSet.Tables["book"];
xmlDataGridView.DataSource =bs;
xmlDataGridView.Refresh();
I've managed to find a solution, of sorts. I don't think the issue was anything to do with hiding the columns, but was simply because there was no data for the columns when the data was first bound. I've just added dummy records for the hidden fields to the file which didn't have them. This works for me, the data appears as I need.
Thanks

C# : Resetting DataGridView for new DataTable

Ok so I'm trying to make a simple SQL CE Viewer application just to view my local databases for another application, I have it setup so that I can select what database I want to open and then it automatically populates a combo box with all of the tables in the database. When I select a table it populates a DataGridView with the records in the table.
My problem is switching between tables. I can't seem to get the DataGridView to remove everything from the previous table and re-populate the DataGridView with the new table information. Of course each table has different columns and rows and such.
I've googled and searched on here and every suggestion I find doesn't seem to work. It populates the DataGridView with the first table just fine, but when I select another it basically adds the columns and rows into whatever was there....
How can I get the DataGridView to completely clear for new data?
And please don't tell me to use dataGridView1.DataSource = null; tried that, doesn't work.
Well, I checked it in one of my programs and just changing DataSource property is working fine. I didn't have to use datagridview.Refresh(). Maybe it depends on the kind of DataSource, which you are using to set datagridview data?
Write this line after you have done populating the DataSource with the new data
dataGridView1.Refresh();
You don't need to clear the DataGridView directly. Its always handled by modifying the DataSource of the DataGridView.
If there is nothing else in the form, you can simply call InitializeComponent() again. But I think #Josh is right.

save contents of a datagridview or similar data table control

I have a design question that has to do with saving the contents of my datagridview. I don't have a binding source or anything, what I want is to save its contents to a file (can be of any type or form), but then give the user the ability to open that file and load the saved grid contents back in. While the application is open, the user is adding/editing/deleting columns or whatever. I've seen some questions on saving the contents as an Excel table. Is this the best way to go?
Maybe the datagridview isn't designed for this, but if there's something better, please let me know.
put all your data in a datatable and then bind it to the datagridview when you are ready to save it do this
DataTable dt = ((DataView)this.dataGridView1.DataSource).Table;
dt.WriteXml(#"C:\test\text.xml");
http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/cd4d2fb0-97a4-4b17-96fc-9ca6992456cc/

Categories