当对行执行更新时(对于行中的单元格),Datagridview保持锁定/滞后

时间:2014-02-19 20:16:10

标签: c# datagridview updates datagridviewtextboxcell

我正在从Yahoo的“Stock API”更新我的单元格值。它的功能就像一个魅力,一切都在正确更新,但是当执行更新时,几乎所有的时间都是因为他们在一个间隔为3000毫秒的计时器中。我怎样摆脱这个问题?

我尝试暂停布局并恢复,但没有成功。

这是我自己的小教我自己的一个项目,所以请记住,我以前没有任何关于xml或datagridview的事情。

        private void timer2_Tick(object sender, EventArgs e)
    {

        try
        {
            if (!(dataGridView.Rows[0].Cells[0].Value == null)) // Updates the rows idividualy as long as first row is not empty
            {
                for (int i = 0; i < dataGridView.RowCount; i++)
                {
                    if (!(dataGridView.Rows[i].Cells[0].Value == null || dataGridView.Rows[i].Cells[0].Value.ToString() == "-")) // Makes sure that the row to update is not an empty row
                    {
                        String symbol;
                        symbol = Convert.ToString(dataGridView.Rows[i].Cells[0].Value);
                        String URLString2 = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22" + symbol + "%22)%0A%09%09&diagnostics=true&env=http%3A%2F%2Fdatatables.org%2Falltables.env";
                        dataGridView.ResumeLayout();
                        dataGridView.Update();
                        for (int t = 2; t < dataGridView.Columns.Count; t++) 
                        {
                            dataGridView.SuspendLayout();
                            XmlTextReader reader2 = new XmlTextReader(URLString2); // Makes the reader read from the string abow ( URL )
                            string NasdaqOpenTime = "09:00:00";

                            // if the market haven't been open for the day then theres no DaysLow value
                            if (dataGridView.Columns[t].HeaderText == "DaysLow" && DateTime.Now.CompareTo(DateTime.Parse(NasdaqOpenTime)) == -1)
                            {
                                dataGridView.Rows[i].Cells[dataGridView.Columns[t].Index].Value = "-"; 
                            }

                            // if the market haven't been open for the day then theres no DaysHigh value
                            if (dataGridView.Columns[t].HeaderText == "DaysHigh" && DateTime.Now.CompareTo(DateTime.Parse(NasdaqOpenTime)) == -1)
                            {
                                dataGridView.Rows[i].Cells[dataGridView.Columns[t].Index].Value = "-";
                            }
                            else
                            {
                                reader2.ReadToFollowing(dataGridView.Columns[t].HeaderText);  // Reada until it fins the elemnt Bid , then stops on it
                                reader2.ReadStartElement(dataGridView.Columns[t].HeaderText); // Recognizes Bid as start element (Bid)
                                dataGridView.Rows[i].Cells[dataGridView.Columns[t].Index].Value = reader2.ReadString(); // Reads the text in between (Declared as a string) actualy the bid value
                                reader2.ReadEndElement();  // Checks that the current nod is an end element (/Bid) if so then continue
                                reader2.ResetState();
                            }

                        }
                    }
                }
            }
        }
        catch (XmlException)
        {
        }
    }

1 个答案:

答案 0 :(得分:0)

最好在单独的线程中从Yahoo请求数据更新,因为这需要很长时间。您可以使用System.Threading.Timer来实现此目的。