I am showing data through datagridview. on a button click i have to extract data from datagridview and populate a list.
please see the screen shot how my data is looking.
i have two for loop to extract data. .here period data is coming horizontally.
see this code. by this code i am storing data into List<T>
private void button1_Click_1(object sender, EventArgs e)
{
string section = "", lineitem = "", xfundcode = "", period = "";
double periodvalue = 0;
List<Data> _data = new List<Data>();
for (int r = 0; r <= dataGridView1.Rows.Count - 1; r++)
{
section = dataGridView1.Rows[r].Cells[0].Value.ToString();
lineitem = dataGridView1.Rows[r].Cells[1].Value.ToString();
xfundcode = dataGridView1.Rows[r].Cells[2].Value.ToString();
for (int c = 3; c <= dataGridView1.Columns.Count - 1; r++)
{
period = dataGridView1.Columns[c].HeaderText;
periodvalue = Convert.ToDouble(dataGridView1.Rows[r].Cells[c].Value.ToString());
_data.Add(new Data
{
Section = section,
LineItem = lineitem,
XFundCode = xfundcode,
Period = period,
PeriodValue = periodvalue
});
}
}
}
i have huge number of rows & column. the approach i am following to accumulate data is taking long time for huge data. please guide how to refactor my code sample to speed up the data accumulation.
here can i use LINQ to accumulate data from datagridview because LINQ is faster.
can i use Parallel For to accumulate data from datagridview because it is faster.
please discuss with some approach which will be faster and accurate too.
Thanks