滚动时通过线程更新DataGridView

时间:2010-09-22 06:22:36

标签: c# multithreading datagridview delegates watin

我遇到了我的这个问题,如果有人为我解决了这个问题,那将会很有帮助

我想做的是:

1)在表单加载事件中初始化DataTable数据表并将其defaultview分配给datagridview dgvresult

2)点击一个按钮启动一个STA线程(我实际上正在使用WatIN IE,因此需要创建线程STA),该方法调用一个方法,该方法创建一个相同的DataTable dt作为在步骤1中创建的数据表,然后添加这个数据表有300行。

3)调用一个委托,它将这个dt与datatable合并,从而更新dgvresult

以下是我刚才描述的步骤的代码片段:

static class Program
{
    /// <summary>
    /// The main entry point for the application.This method is made STAThread as I need to intialize WatIN IE in the form load of frmMain
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new frmMain());
    }
}

/// frmmain code

/// <summary>
/// Delegate which Binds updated Datatable to gridview 
/// </summary>
/// <param name="dt">The Datatable to be merged with main datatable</param>

delegate void Bind_DataTable_to_GridView_Delegate(DataTable dt);

private void bind_DataTable_to_GridView(DataTable dt)
{
    if (dgvResult.InvokeRequired)
    {
        Bind_DataTable_to_GridView_Delegate del = new Bind_DataTable_to_GridView_Delegate(bind_DataTable_to_GridView);
        dgvResult.Invoke(del, new object[] { dt });
    }
    else
    {
        datatable.Merge(dt);
        dgvResult.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;

        //Autosizes the gridview 
        foreach (DataGridViewColumn dgvcol in dgvResult.Columns)
        {
            dgvcol.SortMode = DataGridViewColumnSortMode.NotSortable;
        }
    }
}

WatiN.Core.IE ie;     

private void frmMain_Load(object sender, EventArgs e)
{
    //intialize WatIN IE
    ie = new IE(URLs.mainURL);

    //initialization of columns in datatable
    DataColumn datacolumn1 = new DataColumn("Words");

    //Adding of columns in datatable
    datatable.Columns.Add(datacolumn1);            

    //Making the datatable permanent
    datatable.AcceptChanges();

    //Assigning default view of datatble as dgvResult's datasource
    dgvResult.DataSource = datatable.DefaultView;

    foreach (DataGridViewColumn  dgvcol in dgvResult.Columns)
    {
        dgvcol.SortMode = DataGridViewColumnSortMode.NotSortable;
    }
}

private void btnGenerateWords_Click(object sender, EventArgs e)
{
    try
    {
        if (datatable.Rows.Count > 0)
        {
            //Initializes the GenerateWords Thread           
            GenerateWords = new Thread(GenW);

            //Sets the apartment state to Static
            GenerateWords.SetApartmentState(ApartmentState.STA);

            //Starts the GenerateWords Thread           
            GenerateWords.Start();
        }

    }
    catch(Exception ex)
    {
        throw;
    }
}

#region function of GenerateWords thread
/// <summary>
/// function of GenerateWords thread
/// </summary>

void GenW()
{
    DataColumn datacolumn1 = new DataColumn("Words");

    //Adding of columns in datatable
    DataTable dt = new DataTable();
    dt.Columns.Add(datacolumn1);

    //At this point datatable has say 20 rows
    for (int dtindex = 0; dtindex < datatable.Rows.Count; dtindex++)
    {
        /*Code Which adds successfully 300 fresh rows to dt*/

        //sends datasource of dgvresult as dt
        bind_DataTable_to_GridView(dt);
        dt.Clear();
    }
 }

现在,当我在datagridview中有很多行(假设为1000)

时会出现问题

现在,如果在UI中我没有对datagridview做任何事情,这段代码可以正常工作而没有错误。

但是如果我在调用bind方法的时候继续滚动datgridview,那么应用程序会给出错误并因为我无法更新datgridview而终止(或者可能是由于调用datagridview失败)

这是错误:

  

您的应用程序中发生了未处理的异常。对象引用
  没有设置为对象。

System.NullReferenceException: Object reference not set to an instance of an object.<br>
  at System.Windows.Forms.DataGridViewTextBoxCell.PaintPrivate(Graphics
     

graphics,Rectangle clipBounds,Rectangle cellBounds,Int32 rowIndex,   DataGridViewElementStates cellState,Object formattedValue,String   errorText,DataGridViewCellStyle cellStyle,   DataGridViewAdvancedBorderStyle advancedBorderStyle,   DataGridViewPaintParts paintParts,Boolean computeContentBounds,   Boolean computeErrorIconBounds,Boolean paint)
        在System.Windows.Forms.DataGridViewTextBoxCell.Paint(图形图形,矩形clipBounds,Rectangle cellBounds,Int32 rowIndex,   DataGridViewElementStates cellState,Object value,Object   formattedValue,String errorText,DataGridViewCellStyle cellStyle,   DataGridViewAdvancedBorderStyle advancedBorderStyle,   DataGridViewPaintParts paintParts)
        在System.Windows.Forms.DataGridViewCell.PaintWork(图形图形,矩形clipBounds,矩形cellBounds,Int32 rowIndex,   DataGridViewElementStates cellState,DataGridViewCellStyle cellStyle,   DataGridViewAdvancedBorderStyle advancedBorderStyle,   DataGridViewPaintParts paintParts)
        在System.Windows.Forms.DataGridViewRow.PaintCells(图形图形,矩形clipBounds,矩形rowBounds,Int32 rowIndex,   DataGridViewElementStates rowState,Boolean isFirstDisplayedRow,   Boolean isLastVisibleRow,DataGridViewPaintParts paintParts)
        在System.Windows.Forms.DataGridViewRow.Paint(图形图形,矩形clipBounds,矩形rowBounds,Int32 rowIndex,   DataGridViewElementStates rowState,Boolean isFirstDisplayedRow,   Boolean isLastVisibleRow)
        在System.Windows.Forms.DataGridView.PaintRows(Graphics g,Rectangle boundingRect,Rectangle clipRect,Boolean   singleHorizo​​ntalBorderAdded)
        在System.Windows.Forms.DataGridView.PaintGrid(Graphics g,Rectangle gridBounds,Rectangle clipRect,Boolean   singleVerticalBorderAdded,Boolean singleHorizo​​ntalBorderAdded)
在   System.Windows.Forms.DataGridView.OnPaint(PaintEventArgs e)
        在System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e,   Int16层,布尔disposeEventArgs)
在   System.Windows.Forms.Control.WmPaint(Message&amp; m)
        在System.Windows.Forms.Control.WndProc(Message&amp; m)
        在System.Windows.Forms.DataGridView.WndProc(Message&amp; m)
        在System.Windows.Forms.Control.ControlNativeWindow.OnMessage(消息&amp; m)
        在System.Windows.Forms.Control.ControlNativeWindow.WndProc(消息&amp; m)
        在System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)

我通过VS检查了它,它在这一行的Program.cs中给出了这个错误:Application.Run(new frmMain());

如何解决这个问题?

0 个答案:

没有答案