通过螺纹滚动时更新的DataGridView [英] Updating DataGridView via a thread when scrolling
问题描述
我坚持我的这个问题,如果有人给我
$ B $解决了这个
问题将是很大的帮助b
我所试图做的是:
1)Intialize在窗体加载事件一个DataTable数据表和它的默认视图分配给
datagridview的dgvresult
2)在点击一个按钮启动一个STA线程(其实我跟华廷IE因此
工作需要使线程STA)这就要求它创建一个相同的数据表的方法。
DT为DataTable的步骤1中创建和。然后将300行此数据表
3)调用它合并了这个DT与数据表中,因此在这里嘛更新dgvresult
委托是我的问题:
下面是我刚才所描述的步骤的代码片段:
静态类节目
{
///<总结>
///的主要入口点application.This方法制成STAThread因为我需要并初始化华廷IE在frmMain
///<的形式负荷; /总结>
[STAThread]
静态无效的主要()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(假);
Application.Run(新frmMain());
}
}
/// frmmain代码
///<总结>
///代表结合最新的数据表到gridview的
///< /总结>
///< PARAM NAME =DT>该数据表与主数据表<合并; /参数>
委托无效Bind_DataTable_to_GridView_Delegate(DataTable的DT);
私人无效bind_DataTable_to_GridView(DataTable的DT)
{
如果(dgvResult.InvokeRequired)
{
Bind_DataTable_to_GridView_Delegate德尔=新Bind_DataTable_to_GridView_Delegate(bind_DataTable_to_GridView );
dgvResult.Invoke(德尔,新的对象[] {DT});
}
,否则
{
datatable.Merge(DT);
dgvResult.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells; //自动调整大小在GridView
的foreach(在dgvResult.Columns的DataGridViewColumn dgvcol)
{
dgvcol.SortMode = DataGridViewColumnSortMode.NotSortable;
}
}
}
WatiN.Core.IE即;
私人无效frmMain_Load(对象发件人,EventArgs五)
{
// intialize华廷IE
即=新的IE(URLs.mainURL);
// DataTable中
的DataColumn datacolumn1列初始化=新的DataColumn(字);
//添加DataTable中$ B $列b datatable.Columns.Add(datacolumn1)的;
//使DataTable中永久
datatable.AcceptChanges();
//分配的datatble默认视图dgvResult的DataSource
dgvResult.DataSource = datatable.DefaultView;
的foreach(在dgvResult.Columns的DataGridViewColumn dgvcol)
{
dgvcol.SortMode = DataGridViewColumnSortMode.NotSortable;
}
}
私人无效btnGenerateWords_Click(对象发件人,EventArgs五)
{
试
{
如果(datatable.Rows.Count大于0)
{
//初始化GenerateWords线程
GenerateWords =新主题(GenW);
//设置单元状态为静态
GenerateWords.SetApartmentState(ApartmentState.STA);
//启动线程GenerateWords
GenerateWords.Start();
}
}
#endregion
#地区的GenerateWords线程
///<功能;总结>的GenerateWords螺纹
///<
///功能; /总结>
无效GenW()
{
的DataColumn datacolumn1 =新的DataColumn(字);
//添加DataTable中
DataTable的DT =新的DataTable()列;
dt.Columns.Add(datacolumn1);
//此时表具有说20行
为(INT dtindex = 0; dtindex< datatable.Rows.Count; dtindex ++)
{
/ *添加了成功300新鲜行代码dt的* /
//发送dgvresult的数据源作为DT
bind_DataTable_to_GridView(DT);
dt.Clear();
}
}
}
现在问题出现了,当我在datagridview的许多行(让我们假设1000)
现在,如果在UI我不跟datagridview的事情该代码工作正常没有错误。
但是,如果我保持滚动datgridview当绑定方法被调用的应用程序提供了错误,只是终止,由于其未能及时更新我猜想datgridview(或者因故障在调用的datagridview)
下面是错误: -
当我运行exe我得到这个: -
未处理的异常已发生在你的应用程序。
对象引用未设置为一个对象。
System.NullReferenceException:对象不设置到对象的实例。在System.Windows.Forms.DataGridViewTextBoxCell.PaintPrivate(图形图像,矩形clipBounds,矩形cellBounds,的Int32的rowIndex,DataGridViewElementStates cellState
,对象FormattedValue的,字符串ERRORTEXT,的DataGridViewCellStyle cellStyle,DataGridViewAdvancedBorderStyle advancedBorderStyle,DataGridViewPaintParts paintParts,布尔computeContentBounds,布尔computeErrorIconBounds布尔油漆)
在System.Windows.Forms.DataGridViewTextBoxCell.Paint(图形图像,矩形clipBounds,矩形cellBounds,的Int32的rowIndex,DataGridViewElementStates cellState,对象的值,对象FormattedValue的,字符串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,布尔isFirstDisplayedRow,布尔isLastVisibleRow,DataGridViewPaintParts paintParts)
在System.Windows.Forms.DataGridViewRow.Paint(图形显卡,矩形clipBounds ,矩形rowBounds,的Int32的rowIndex,DataGridViewElementStates的RowState,布尔isFirstDisplayedRow,布尔isLastVisibleRow)
在System.Windows.Forms.DataGridView.PaintRows(图形克,矩形boundingRect,矩形clipRect,布尔singleHorizontalBorderAdded)
的系统。 Windows.Forms.DataGridView.PaintGrid(图形克,矩形gridBounds,矩形clipRect,布尔singleVerticalBorderAdded,布尔singleHorizontalBorderAdded)在System.Windows.Forms.DataGridView.OnPaint(PaintEventArgs的E)
。在System.Windows.Forms.Control的。 PaintWithErrorHandling(PaintEventArgs的E,Int16的层,布尔disposeEventArgs)在System.Windows.Forms.Control.WmPaint(消息和;米)
在System.Windows.Forms.Control.WndProc(消息和M)
在System.Windows.Forms.DataGridView.WndProc(消息和M)
在System.Windows.Forms的.Control.ControlNativeWindow.OnMessage(消息和M)
在System.Windows.Forms.Control.ControlNativeWindow.WndProc(消息和M)
在System.Windows.Forms.NativeWindow.Callback(IntPtr的的HWND, INT32味精,IntPtr的WPARAM,IntPtr的LPARAM)
我检查过了VS,它给出了这个错误Program.cs的在这条线: - Application.Run(新frmMain())
;
如何解决这个问题?
任何帮助将不胜感激。
谢谢您:)
你应该尝试
this.Invoke(德尔,新的对象[] {DT}); //应该工作
而不是
dgvResult.Invoke(德尔,新的对象[] {DT}); //错误
在bind_DataTable_to_GridView()
I am stuck with this problem of mine and it will be great help if someone solves this
problem for me
What I am trying to do is :
1) Intialize a DataTable datatable in form load event and assign its defaultview to a
datagridview dgvresult
2) On click of a button start an STA thread (I am actually working with WatIN IE hence the
need for making the thread STA ) which calls a method which creates an identical DataTable
dt as datatable created in step 1 and then adds 300 rows to this datatable.
3) calls a delegate which merges this dt with datatable and thus updates the dgvresult Well here is my problem :
Here is the code Snippet for the steps I just described :
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();
}
}
#endregion
#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();
}
}
}
Now the problem arises when I have many rows in datagridview(lets assume 1000)
Now if in UI I dont do anything with datagridview this code works fine without errors
But if I keep scrolling the datgridview when the bind method is called the application gives error and just terminates due to its failure to update the datgridview I suppose ( or maybe due to failure in invoking the datagridview )
Here is the error :-- When I run the exe I get this :-
Unhandled exception has occured in your application.
Object reference not set to an object.
System.NullReferenceException: Object reference not set to an instance of an object.
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)
at System.Windows.Forms.DataGridViewTextBoxCell.Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, Int32 rowIndex, DataGridViewElementStates cellState, Object value, Object formattedValue, String errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
at System.Windows.Forms.DataGridViewCell.PaintWork(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, Int32 rowIndex, DataGridViewElementStates cellState, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
at System.Windows.Forms.DataGridViewRow.PaintCells(Graphics graphics, Rectangle clipBounds, Rectangle rowBounds, Int32 rowIndex, DataGridViewElementStates rowState, Boolean isFirstDisplayedRow, Boolean isLastVisibleRow, DataGridViewPaintParts paintParts)
at System.Windows.Forms.DataGridViewRow.Paint(Graphics graphics, Rectangle clipBounds, Rectangle rowBounds, Int32 rowIndex, DataGridViewElementStates rowState, Boolean isFirstDisplayedRow, Boolean isLastVisibleRow)
at System.Windows.Forms.DataGridView.PaintRows(Graphics g, Rectangle boundingRect, Rectangle clipRect, Boolean singleHorizontalBorderAdded)
at System.Windows.Forms.DataGridView.PaintGrid(Graphics g, Rectangle gridBounds, Rectangle clipRect, Boolean singleVerticalBorderAdded, Boolean singleHorizontalBorderAdded) at System.Windows.Forms.DataGridView.OnPaint(PaintEventArgs e)
at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs) at System.Windows.Forms.Control.WmPaint(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.DataGridView.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
I checked it through the VS , It gives this error in Program.cs in this line :- Application.Run(new frmMain())
;
How to get around this problem?
Any help will be appreciated. Thank You :)
you should try
this.Invoke(del, new object[] { dt }); //Should Work
instead of
dgvResult.Invoke(del, new object[] { dt }); //Error
in bind_DataTable_to_GridView()
这篇关于通过螺纹滚动时更新的DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!