English 中文(简体)
• 如何在Gridview使用一些单元的翻新?
原标题:How to use Threading in Change some Cell in Gridview?
  • 时间:2009-09-22 07:24:01
  •  标签:

我如何利用Threading来改变Gudview的一些单元? 我从数据库中查询,并用很多时间进行询问。 因此,速度非常缓慢,我想更快地利用校对来装数据。 而且,当胎面完成时,可以改变格里德人的看法中的数据?

问题回答
using System.Threading;
using System.Threading.Tasks;

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        dataGridView1.DataSource = new List<Test>() { new Test { Name = "Original Value" } };
    }

    // Start the a new Task to avoid blocking the UI Thread
    private void button1_Click(object sender, EventArgs e)
    {
        Task.Factory.StartNew(this.UpdateGridView);
    }
    // Blocks the UI
    private void button2_Click(object sender, EventArgs e)
    {
        UpdateGridView();
    }

    private void UpdateGridView()
    {
        //Simulate long running operation
        Thread.Sleep(3000);
        Action del = () =>
            {
                dataGridView1.Rows[0].Cells[0].Value = "Updated value";
            };
        // If the caller is on a different thread than the one the control was created on
        // http://msdn.microsoft.com/en-us/library/system.windows.forms.control.invokerequired%28v=vs.110%29.aspx
        if (dataGridView1.InvokeRequired)
        {
            dataGridView1.Invoke(del);
        }
        else
        {
            del();
        }
    }
}




相关问题
热门标签