多线程与DataGridView的问题,请帮忙
partial class Form2
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.GetDataB = new System.Windows.Forms.Button();
this.DataDGV = new System.Windows.Forms.DataGridView();
this.QueryTB = new System.Windows.Forms.TextBox();
this.ResultL = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.DataDGV)).BeginInit();
this.SuspendLayout();
//
// GetDataB
//
this.GetDataB.Location = new System.Drawing.Point(11, 12);
this.GetDataB.Name = "GetDataB";
this.GetDataB.Size = new System.Drawing.Size(75, 23);
this.GetDataB.TabIndex = 0;
this.GetDataB.Text = "获取数据";
this.GetDataB.UseVisualStyleBackColor = true;
this.GetDataB.Click += new System.EventHandler(this.button1_Click);
//
// DataDGV
//
this.DataDGV.BackgroundColor = System.Drawing.SystemColors.ActiveCaptionText;
this.DataDGV.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.DataDGV.Location = new System.Drawing.Point(1, 53);
this.DataDGV.Name = "DataDGV";
this.DataDGV.RowTemplate.Height = 23;
this.DataDGV.Size = new System.Drawing.Size(556, 412);
this.DataDGV.TabIndex = 1;
//
// QueryTB
//
this.QueryTB.Location = new System.Drawing.Point(109, 12);
this.QueryTB.Name = "QueryTB";
this.QueryTB.Size = new System.Drawing.Size(438, 21);
this.QueryTB.TabIndex = 2;
this.QueryTB.Text = "select * from Person.Address";
//
// ResultL
//
this.ResultL.AutoSize = true;
this.ResultL.Location = new System.Drawing.Point(107, 38);
this.ResultL.Name = "ResultL";
this.ResultL.Size = new System.Drawing.Size(77, 12);
this.ResultL.TabIndex = 3;
this.ResultL.Text = "获取数据 条";
//
// Form2
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(559, 468);
this.Controls.Add(this.ResultL);
this.Controls.Add(this.QueryTB);
this.Controls.Add(this.DataDGV);
this.Controls.Add(this.GetDataB);
this.Name = "Form2";
this.Text = "Form2";
this.Load += new System.EventHandler(this.Form2_Load);
((System.ComponentModel.ISupportInitialize)(this.DataDGV)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button GetDataB;
private System.Windows.Forms.DataGridView DataDGV;
private System.Windows.Forms.TextBox QueryTB;
private System.Windows.Forms.Label ResultL;
}
public partial class Form2 : Form
{
delegate void QueryD(string str);
Data data = null;
DataView dv = null;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string conditionStr = QueryTB.Text;
//data.GetData(conditionStr);
QueryD qD = new QueryD(data.GetData);
IAsyncResult result = qD.BeginInvoke(conditionStr, null, null);
qD.EndInvoke(result); this.ResultL.Text ="获取数据 "+ dv.Table.Rows.Count.ToString()+" 条";
}
private void Form2_Load(object sender, EventArgs e)
{
data = new Data();
dv = new DataView();
dv = data.DT.DefaultView;
this.DataDGV.DataSource = dv;
}
}
public class Data
{
SqlConnection sqlCon;
SqlCommand selCom;
SqlDataAdapter sqlDA;
DataTable dt;
public DataTable DT
{
get
{
return dt;
}
}
public Data()
{
dt = new DataTable();
sqlCon = new SqlConnection("Data Source=.;Initial Catalog=AdventureWorks;Integrated Security=SSPI;Persist Security Info=False");
selCom = new SqlCommand();
selCom.Connection = sqlCon;
sqlDA = new SqlDataAdapter(selCom);
}
public void GetData(string str)
{
try
{
selCom.CommandText = str;
dt.Clear();
sqlDA.Fill(dt);
}
catch
{
}
}
}
在一个线程里执行是没问题的,但异步执行时有如下问题:
(1)在调试状态下:点击获取按钮,dv中有数据,但没有绑定到DataGridView中,(在同个线程会自动绑定)
(2)在执行不调试状态下:点击获取按钮,dv中有数据,也会绑定到DataGridView中,但与在同个线程执行时的情况不同,如:记录很多是会有滚动条,在异步线程里执行没有滚动条,并且移动到别的区域(不知道怎么表达,就是选择单元格时会出现)会出现停止状态(好像出现异常)
请问是怎么回事
[解决办法]
既然是多线程,当然会出现绑定延迟的状态,另外注意DataGridView是使用UI线程,
在绑定的时候建议回到UI线程中进行