本文介绍了使用async / await填充DGV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 嗨我有使用async / await方法触发datagridview的问题。 按钮点击事件:HiI am have a litle problems firing my datagridview using the async / await method.Button click event:private async void button2_Click(object sender, EventArgs e) { datagridview1.Visible = true; //Progressbar Progressbar1 .Visible = true; Progressbar1 .Style = ProgressBarStyle.Marquee; var period2 = cb1.SelectedValue.ToString(); var period1 = cb2.SelectedValue.ToString(); /*A variable to hold the parameters*/ //var table = await loadTable((period1, period2)); await Task.Run(() => loadTable(period1, period2)); /*back to the UI */ //datagridview1.DataSource = datatable; //datagridview1.DataSource = table; Progressbar1 .Visible = false; } 我尝试使用两个// datagridview.DataSource行来获取DGV但没有成功。 我的任务方法:I haved tried fetching the DGV by using the using the two // datagridview.DataSource line without success.My Task method: private async Task Zugänge(string period1, string period2){ // string C = ConfigurationManager.ConnectionStrings["123"].ConnectionString; using (var con = new SqlConnection(C)) using (var cmd = new SqlCommand()) { cmd.Connection = con; cmd.CommandText = ("[dbo].[spInfo]"); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@Periode2", period2); cmd.Parameters.AddWithValue("@Periode1", period1); // open the connection con.Open(); SqlDataAdapter adapter = new SqlDataAdapter(cmd); DataTable datatable = new DataTable(); adapter.Fill(datatable ); await Task.Run(() => adapter.Fill(datatable )); }}推荐答案我添加了一个返回结束我的Task方法:I added a return to end of my Task method:private async Task Zugänge(string period1, string period2){ // string C = ConfigurationManager.ConnectionStrings["123"].ConnectionString; using (var con = new SqlConnection(C)) using (var cmd = new SqlCommand()) { cmd.Connection = con; cmd.CommandText = ("[dbo].[spInfo]"); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@Periode2", period2); cmd.Parameters.AddWithValue("@Periode1", period1); // open the connection // Here open the connection async: await con.OpenAsync(); SqlDataAdapter adapter = new SqlDataAdapter(cmd); DataTable datatable = new DataTable(); adapter.Fill(datatable ); await Task.Run(() => adapter.Fill(datatable ));return dt; }} 这篇关于使用async / await填充DGV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-15 05:25