StreamReader reader = File.OpenText("mystations.txt");
            string line = reader.ReadLine();
            while (line != null)
            {
                


                // string[] stationtxt = line.Split('\t');

                string name = line.Split('\t')[0];
                string url = line.Split('\t')[1];
                string desc = line.Split('\t')[2];
                Image img = Image.FromFile("c:/pictures/"+ name + ".jpg");
                line = reader.ReadLine();
                dgLogo.Image = new Bitmap(img);
                dataGridView1.Rows.Add(name, url, desc, img); //(stationtxt);

            }
            reader.Close();
            reader.Dispose();

I'am able to edit the values in the datagrid but when i reload the form it display the original values of course.

i want to catch the edited values and save it again to the tab delimited text file.
and also be able to delete a row from the text file and datagrid.
the image column is there just for display its not editable.

if someone can provide me a sample or point me in the right direction

If you split your data into a list of array of string, you can do a bit of googling and find how to use a list as a data source, then when they finish, just tab join the data back and write it out.

believe me a read a lot of guides and the worst are from msft msdn website there samples never works.

i have the logic in my head. the problem is converting that to code, in where event to add it. datagrid have a lot lot of events,
for example i have to added to oncellchange event, onclick event, on edit event.

all codes have very different methods and it get confusing.

-thanks BTW, I think you reply to my other post, right?

I had this working fairly quickly when I tried it, I dont have the machine to hand I did it on, but I took a csv file and chucked it into a list of array of string and then added it.

As for your other post - which one?

As for your other post - which one?

the one i have problem lunch a batch file from the code.

I took a csv file and chucked it into a list of array of string and then added it.

ok the problem is how to get the new edit values from the datagrid i cant use the Arraylist because of the image column in the datagrid with the array i dont find how the get the images show up i end up doing in it like the code i show in the other post.

so my doubt is not how to add the text file to the datagrid I did that already. is how to get the new edited values once the user edit them directly in the datagrid. then get all the values from the datagrid and save them back to the 'tab' delimited text file.

You dont, or rather you shouldn't need to it should all be done for you..

remember am not using any database or dataset. I know this is automatically if you are connect to a database.

i am reading from a text file and passing that values to the datagrid for easy viewing and (possible and hope so) easy editing.

or do you mean this could be done even if i am not using a database?

You didnt read the link I posted then, as thats exactly what that does. You can use lists as data sources.

I know I read it but if you see my code I fill the datagrid individually because of the image column if I use the list how I fill the images column because my textfile only contain the name of the image file, the image is in another folder and the datagrid column is a DataGridViewImageColumn.

i was trying the code from the page you post when i encounter the problem of the image.

You should be able to make an image part of the row.. without any major issues.. Then it would update properly

(men/girl) am so confuse right now !!

i really dont know or get how to implement that to my code !

Normally you would create a class which included the picture, and then have a list of the class, but a picture wouldnt ever save in a csv file without potential issue so, if its just a icon to cover a given predetermind set of options (such as read, write, execute or such) then you could use enums and so on.

ok you say that i have to :

1. parse the text file to an Array or ArrayList ?

2. convert that Array or ArrayList to a dataSource

3. use that dataSource to populate the datagrid

4. use the INotifyPropertyChanged Interface of the page you post, to update accordingly the dataSource , Array or ArrayList and the text file.


questions? how I fill the image column alone, or i have to put the images as part of the array or arrayList, also known that the images files depend on the name of a value in the text file that is the name of the file plus the .jpg extension as you saw in my code.

1. Array
2. You dont "convert"
3. yes
4. it updates the list, not the file, you still need to write that at the end

Thats why the image is in the class you were supposed to be making from the last post I did.. It will then populate the grid.

thanks a lot really. but be easy on me
i am trying to learn C# and am surprise i was able to make an application already this are just things am still adding to it but the application is running and working. I like to learn by doing projects and working with the program itself I am waiting for a book to read a little but mostly for reference, there are 2 many to choose for. I will have to go to Borders and check them out first.

A few month back I take a class on C++ and like it so i pick a newer and modern language no so distant.

here is the application i did and still working on it, the bottom part is the one I am trying to make it work.
http://thegreenbutton.com/forums/ShowThread.aspx?PostID=287192#287192

1. Array
2. You dont "convert"
3. yes
4. it updates the list, not the file, you still need to write that at the end

1.you make an array
2.you bind the array to a datasource
3 and bind that datasource to the datagrid
4 it update the array ?

the class part i get lost I know what a class is but what am suppose to declared or define there about the images ?

- from where you load it and/or to where you save them ?
- and stuff like that

thanks again!

Did you try following the MS example I linked? Which had a working list of a "class" that updated? (worry about the image part separately but basiclaly its just another part of the class)

ok I really try to fallow that example with no avail . I dont understand a few lines or what they does because the complete code dont apply to me and the most simple dont work that is fill the datagrid.

I create an ArrayList and declare a binding source then declare that binding source as my datasource for my array

then bound the datagrid with the bounding source.

OK, but perhaps your current way is why its not working - the example is very similar to what you wanted, in fact its where I grew mine from, so it does work. If you dont understand it, post the exact bits you dont understand and what you thought it should do as otherwise its going to be impossible to move you on from this.

really I can get this to work am trying first to make a datatable and binding with the datagrid . but I get confuse on what are the steps so someone please correct me

1. you create a data table
2. you create a binding source based on the data table
3. you bind the datagridview with the binding source
??

this is the mess of the code i was trying to make the table and the binding, ect...

BindingSource bindingSource = new BindingSource();
            DataSet ds = new DataSet();
            DataTable dt = ds.Tables.Add("Data");
            bindingSource.DataSource = ds;
            dataGridView1.DataSource = bindingSource;
           StreamReader reader = File.OpenText("mystations.txt");
            string line = reader.ReadLine();
            
            
            
            
            DataRow dr;
            
            Regex r = new Regex("\t");
            line = reader.ReadLine();
            while (line != null)
            {
                    
           foreach(Match m in r.Matches(line))
           {
               dr = dt.NewRow();
               dt.Rows.Add("Name", typeof(string));
               dt.Rows.Add("URL", typeof(string));
               dt.Rows.Add("Desc", typeof(string));
               dt.Rows.Add("Logo", typeof(string));
               
               dt.Rows.Add(dr);
               
           }
           
            
            }
            
          
            reader.Close();
            reader.Dispose();

          

        }

could someone help fix this and/or explain me the process..

thanks!

someone ?

StreamReader reader = File.OpenText("mystations.txt");
            string line = reader.ReadLine();
            while (line != null)
            {
                


                // string[] stationtxt = line.Split('\t');

                string name = line.Split('\t')[0];
                string url = line.Split('\t')[1];
                string desc = line.Split('\t')[2];
                Image img = Image.FromFile("c:/pictures/"+ name + ".jpg");
                line = reader.ReadLine();
                dgLogo.Image = new Bitmap(img);
                dataGridView1.Rows.Add(name, url, desc, img); //(stationtxt);

            }
            reader.Close();
            reader.Dispose();

I'am able to edit the values in the datagrid but when i reload the form it display the original values of course.

i want to catch the edited values and save it again to the tab delimited text file.
and also be able to delete a row from the text file and datagrid.
the image column is there just for display its not editable.

if someone can provide me a sample or point me in the right direction

I did not read the others reply so ... don't be harsh if someone already sugested this or pointed you in a difrent part ..

the problem is that you don't save the file after edit.. a simple and efective way it will be to add on the datagrid the LostFocus event, and in there you can rewrite the entire file , .. don't forget to flush before the LostFocus end's .. and that should be simple and eficient. of course you could use a datasource but that will be slow as far as i see it... and in this case.. a challange. :) .

so the lostfocus event need to do the following...

ClearFile(Filename);
foreach (row in GridRow)
{
foreach (cell in rowCells )
{
File.Write(value+"\t");
}
File.Write("\n");
}
File.Flush()

// really don't forget to flush your streams :D ..

this is not real code... of course.. but it should be something along that way...
if this post does not help in any way.. well ... i don't mind posting it ... :P

commented: thanks for everything +1

well this seems far easier than create all the data source and the data table and the binding with the datagrid just to edit a text file.

ClearFile(Filename);
foreach (row in GridRow)
{
foreach (cell in rowCells )
{
File.Write(value+"\t");
}
File.Write("\n");
}
File.Flush()

I know this is like a guide but what you mean by ClearFile(FileName);

and why put this in the lost focus event ?
when the lost focus event trigger?

This would defeat the whole point of having data sources and is rather like forcing a square thing in a round hole.

However, as it seems someones unwilling to work with the dataset he setup, I guess theres little choice.

the event will be triggered when you change the focus from the datagrid to another object , be it a other window tab or something else... like pushing a button .. so then all the changes you make are saved in the file.. when you exit the grid { not the cell } ... there are always alternative with the grid's lost focus event to save modified data... like CellEndEdit ... :) that can be a even better alternative... :)

the ClearFiles .. can be a function that clears the file of all the data... or you can do than in the StreamWriter by forcing a overwrite.... just remeber... to open /close/open/close in corect order. don't close/close or open/open :D ..


I agree with LizR that this will defeat the concept of datasources ... but there are times when you need them and times when you don't need them... if he diden't used them so far... then there is no need for them.

He had though that was the point :)

He had though that was the point

I want to but if see my last post i try to setup a few dataset datatable bindingsource all that with no avail. and really I dont know what else to do.

You didnt bother to post the actual problem with it, you just said it didnt work, no one can help unless you say what "didnt work" what did happen.. what code you had..

miculnegru

this is the code i got from what you said, everything working great but it doesnt write the text file like it suppose to, like :
row 1 <value>tab<value>tab<value>
row2 <value>tab<value>tab<value>
it write everything, with tab delimited, in one line.

also how if i want only to write the values from only 3 of the 4 columns?

thanks.

private void dataGridView1_Leave(object sender, EventArgs e)
        {
            StreamWriter file = new StreamWriter("test.txt");
           
              foreach (DataGridViewRow rows in dataGridView1.Rows)
                {
                    foreach (DataGridViewCell cell in rows.Cells)
                    {
                  
                        file.Write(cell.Value+"\t");
                        file.Write("\n");
                      
                       
                    }
                    
                    
                }
            
        file.Close();
        file.Dispose();

        }

LizR You didnt bother to post the actual problem with it, you just said it didnt work, no one can help unless you say what "didnt work" what did happen.. what code you had..

I posted it earlier and I dodn't post any specific error because it doesnt have any, the datagrid just wont populate. beside that nothing the program compile and run.

BindingSource bindingSource = new BindingSource();
            DataSet ds = new DataSet();
            DataTable dt = ds.Tables.Add("Data");
            bindingSource.DataSource = ds;
            dataGridView1.DataSource = bindingSource;
           StreamReader reader = File.OpenText("mystations.txt");
            string line = reader.ReadLine();
            
            
            
            
            DataRow dr;
            
            Regex r = new Regex("\t");
            line = reader.ReadLine();
            while (line != null)
            {
                    
           foreach(Match m in r.Matches(line))
           {
               dr = dt.NewRow();
               dt.Rows.Add("Name", typeof(string));
               dt.Rows.Add("URL", typeof(string));
               dt.Rows.Add("Desc", typeof(string));
               dt.Rows.Add("Logo", typeof(string));
               
               dt.Rows.Add(dr);
               
           }
           
            
            }
            
          
            reader.Close();
            reader.Dispose();

          

        }

ok i fix the problems with saving the text file in a wrong format the line

file.WriteLine();
//or file.Write("\n");

was in the wrong foreach.

now I am trying to avoid saving one column, I try to set dg.Column(col).Visible to false but it still save the column.

private void dataGridView1_Leave(object sender, EventArgs e)
        {
            StreamWriter file = new StreamWriter("test.txt");
           
              foreach (DataGridViewRow rows in dataGridView1.Rows)
                {
                  
                    foreach (DataGridViewCell cell in rows.Cells)
                    {
                     if (cell.Visible == true )
                        file.Write(cell.Value+"\t");                 
                       
                    }
                       file.Write("\n");
                }
        file.Close();
        file.Dispose();
        }

this is for the collumn with the visible set to false ... it will work .. ;) ..
i have to go now will reply later...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.