我创建了一个应用程序,它通过执行存储在SQL Server中的过程将数据加载到ListView中。该过程需要一些时间来执行,当我单击应用程序上的按钮执行该过程时,我的应用程序只是冻结,直到数据加载到ListView中。
当我按下按钮直到数据在列表视图中填充时,如何创建一个加载的启动画面?
我感谢任何反馈。
public async Task<ListView> GetDataAsync()
{
var dt = new ListView();
//var cn = @"Your Connection String";
//var cmd = @"SELECT * FROM Category";
//var da = new SqlDataAdapter(cmd, cn);
SqlConnection conn = new SqlConnection();
conn.Open();
SqlCommand cmd = new SqlCommand("[Loyalty_Swipe]", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandTimeout = 0;
SqlDataReader reader;
cmd.Parameters.AddWithValue("@StartDate", txtStartDate.Text);
cmd.Parameters.AddWithValue("@EndDate", txtEndDate.Text);
cmd.Parameters.AddWithValue("@CustomerID", txtCustID.Text);
reader = cmd.ExecuteReader();
metroListView1.Items.Clear();
await Task.Run(() =>
{
while (reader.Read())
{
metroListView1.Columns[0].Text = "Date";
metroListView1.Columns[1].Text = "Store Code";
ListViewItem listview = new ListViewItem(reader["Date"].ToString());
listview.SubItems.Add(reader["StoreCode"].ToString());
listview.SubItems.Add(reader["StoreName"].ToString());
listview.SubItems.Add(reader["TillNumber"].ToString());
listview.SubItems.Add(reader["TranNumber"].ToString());
listview.SubItems.Add(reader["Amount"].ToString());
listview.SubItems.Add(reader["Discount"].ToString());
listview.SubItems.Add(reader["ProcessedFlag"].ToString());
listview.SubItems.Add(reader["Processed Date"].ToString());
metroListView1.Items.Add(listview);
};
});
conn.Close();
pbLoad.Hide();
btnExport.Enabled = true;
return dt;
}