Click here to Skip to main content
15,884,974 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I made a DataSet, and bounded (DataSource/BindningSource) it to a DataGridView.

From here the user can type in (double) values which I can later use for my calculations.

I've added a button for saving and loading the tables. When i Load/Opens a table the values are displaying in the Form-DataGridView, but I don't manage to connect/set it to the DataSet.

Below is the main load/open code. Here without the Dataset; data_vault.Revenue.
C#
private void Open_Click(object sender, EventArgs e)
        {
            //open a table
            OpenFileDialog open_data = new OpenFileDialog();
            open_data.Title = "Open file";
            open_data.Filter = "XML Files (*.xml)|*.xml";

            if (open_data.ShowDialog() == DialogResult.OK)
            {
                //Leser filen
                StreamReader read_table = new StreamReader(File.OpenRead(open_data.FileName));

                //Nye rader i tabellen

                DataTable dtable = new DataTable();
                dtable.Columns.Add(new DataColumn("L2_n", typeof(double)));
                dtable.Columns.Add(new DataColumn("L1_n", typeof(double)));



                //Splitter tekstfilen
                List<string> s_list = new List<string>();

                string str = read_table.ReadLine();
                char[] spearator = { '.', ' ' };


                String[] strlist = str.Split(spearator);
                foreach (String s in strlist)
                {
                    s_list.Add(s);
                }


                int count = s_list.Count;
                int i = 0;

                //Setter inn verdier i tabell
                for (int j = 0; j < (count - 1) / 2; j++)
                {
                    DataRow RowValues = dtable.NewRow();
                    double d = Double.Parse(s_list[i]);


                    RowValues[0] = Double.Parse(s_list[i]);
                    i++;
                    RowValues[1] = Double.Parse(s_list[i]);
                    i++;

                    dtable.Rows.Add(RowValues);                    
                }

                dataGridView1.DataSource = dtable;
            }

        }


Later I'm collecting the values;
C#
 private void button1_Click(object sender, EventArgs e)
        {
            //OK button

            //Liste lengder
            int i = 0;
            foreach (var obj in data_vault.Revenue)
            {
                double l2_tall = (Double)dataGridView1["l2nDataGridViewTextBoxColumn", i].Value;


                double l1_tall = (Double)dataGridView1["l1nDataGridViewTextBoxColumn", i].Value;


                i++;
            }
Line straight_line = Line.CreateBound(l2_tall , l1_tall);
}


What I have tried:

I've tried to Add, clone, New Object and loop through to set det values. Also
OleDbDataAdapter -> adapter.Update(dtable); and SQL

..but probably with some errors.
Posted
Updated 9-Feb-20 23:20pm
v3
Comments
Richard MacCutchan 7-Feb-20 9:13am    
Why do you create a list from the data, instead of adding it direct to the DataTable? And why are the column names you use to refer to the DataGridView different to the column names in the DataTable?
camibo 7-Feb-20 9:25am    
Hi and thank you for your quick replay.

Maybe there is no good reason for that. :S
1. The list of strings are made to split the textfile previously saving the table. Adding it back to a table I can set to the DataSet (was the idea anyway).
2. The names of the columns are "Previewing" the same.
Richard MacCutchan 7-Feb-20 9:54am    
1. All that is needed is to split the string and then convert each number to a Double and store in a new DataRow. The List is not needed. By the way it is better to use Double.TryParse rather than Convert.ToDouble, as that will catch any bad string values.

2. According to your code above the names are not the same.
camibo 10-Feb-20 2:54am    
Good morning,
From what I can understand, that is what i am doing. I now have the DataRow with the correct stored values. all values are displaying as they should.

However I'm not sure I understand how to update the datatble so that I can use the values for further calculations later. I think I get a bit confused for what is the datatable holding the values in the datagridview. What is the name here for the datatable I should store the DataRow values in?


For the l2_test and l1_test list. These are only used for a set/get function I made and is not "interferring" with the code.
Richard MacCutchan 10-Feb-20 3:13am    
If you need to change any values then you must change them in the DataTable, as that is the source for the DataGridView. If you change them in the DataGridView then the source and display will by out of sync.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900