//创建一个类
public class TxtClass1
{
//创建了一个委托的对象
public delegate void CarInfo(string CarName, string CarType, string CarYear, string CarColor, string CarBaoyang);
//创建一个对象的类型;
public CarInfo ca;
//创建了一个对象的方法;
public void CarInfos()
{
//进行循环三次
for (int i = 0; i <1000000000; i++)
{
string CarName = "陕A001" + i.ToString().PadLeft(0,'3');//
string CarType = "宝马";
string CarYear = "5";
string CarColor = "银白色";
string CarBaoyang = "是";
if(ca!=null)
{
//将声明对象的值传递给ca;
ca(CarName, CarType, CarYear, CarColor, CarBaoyang);
}
}
}
}
//需用到命名空间
using System.Threading;
using System.Threading.Tasks;
namespace TextProjext
{
public partial class ContextForm2 : Form
{
public ContextForm2()
{
InitializeComponent();
}
//声明一个方法,将方法对象的值传递给dataGridView
public void CarSession(string CarName, string CarType, string CarYear, string CarColor, string CarBaoyang)
{
if (!button1.InvokeRequired)
{
//button1.Text = CarName;
this.dataGridView1.Rows.Add(1);
int RowsCount = this.dataGridView1.Rows.Count - 1;
this.dataGridView1.Rows[RowsCount].Cells[0].Value = CarName;
this.dataGridView1.Rows[RowsCount].Cells[1].Value = CarType;
this.dataGridView1.Rows[RowsCount].Cells[2].Value = CarYear;
this.dataGridView1.Rows[RowsCount].Cells[3].Value = CarColor;
this.dataGridView1.Rows[RowsCount].Cells[4].Value = CarBaoyang;
}
else
{
this.Invoke(new TxtClass1.CarInfo(CarSession), new object[] { CarName, CarType,CarYear, CarColor, CarBaoyang });
}
}
TxtClass1 car = new TxtClass1();
private void button1_Click(object sender, EventArgs e)//获取对象的值全部显示到dataGridView里面;
{
car.ca = new TxtClass1.CarInfo(CarSession);
new Thread(car.CarInfos).Start();//线程开始
}
public TxtClass1.CarInfo bigcar;
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
//判断dataGridView里面行的集合是否大于0;
if (dataGridView1.SelectedRows.Count > 0)
{
bigcar(this.dataGridView1.SelectedRows[0].Cells[0].Value.ToString(),this.dataGridView1.SelectedRows[0].Cells[1].Value.ToString(),this.dataGridView1.SelectedRows[0].Cells[2].Value.ToString(),this.dataGridView1.SelectedRows[0].Cells[3].Value.ToString(),this.dataGridView1.SelectedRows[0].Cells[4].Value.ToString());
}
}
}
}