使用后台工作程序从gridview中的sqldatabase检索数据

时间:2017-07-03 06:45:05

标签: c# .net datagridview

使用后台工作程序从gridview中的sqldatabase检索数据。跳过已添加数据并添加了相同类型的额外数据,使用断点或消息框显示正确的数据,但不在datagridview中!任何人都可以指出下面代码中的错误在哪里?任何帮助表示赞赏

     private void button8_Click(object sender, EventArgs e)
    {
        //gridviewMain();
        toolStripStatusLabel1.Visible = true;
        progressBar1.Maximum = GetTotalRecords();
        progressBar1.Visible = true;
        dataGridView1.ColumnCount = 9;
        dataGridView1.Columns[0].Name = "Accession No";
        dataGridView1.Columns[1].Name = "Author";
        dataGridView1.Columns[2].Name = "Title";
        dataGridView1.Columns[3].Name = "Edition";
        dataGridView1.Columns[4].Name = "ClassNo";
        dataGridView1.Columns[5].Name = "BookNo";
        dataGridView1.Columns[6].Name = "Subject";
        dataGridView1.Columns[7].Name = "Department";
        dataGridView1.Columns[8].Name = "Status";
        dataGridView1.Columns[8].Visible = false;
        if (!bgwFillDGV.IsBusy)
        {
            RetriveTableData TObj = new RetriveTableData();
            dataGridView1.Rows.Clear();
            bgwFillDGV.RunWorkerAsync(TObj);

        }
    }
    private int GetTotalRecords()
    {
        SqlConnection con;
        SqlCommand cmd;
        try
        {
            using (con = new SqlConnection(connectionString))
            {
                cmd = new SqlCommand("SELECT COUNT(*) FROM tblBook", con);
                con.Open();
                TotalRecords = int.Parse(cmd.ExecuteScalar().ToString());
                con.Close();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        return TotalRecords;
    }
 public class RetriveTableData
    {
        public string AccessionNo;
        public string Author;
        public string Title;
        public string Edition;
        public string ClassNo;
        public string BookNo;
        public string Subject;
        public string Department;
        public string Status;
    }

    private void bgwFillDGV_DoWork(object sender, DoWorkEventArgs e)
    {
        RetriveTableData Obj = (RetriveTableData)e.Argument;
        string SqlcmdString = "SELECT * from tblBook";
        SqlDataReader reader;
        int i = 1;
        try
        {
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                Sqlcmd = new SqlCommand(SqlcmdString, conn);
                conn.Open();
                reader = Sqlcmd.ExecuteReader();

                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        //int.Parse(reader["NO_IND"].ToString());
                        Obj.AccessionNo = reader["accessionNo"].ToString();
                        Obj.Author = reader["author"].ToString();
                        Obj.Title = reader["title"].ToString();
                        Obj.Edition = reader["edition"].ToString();
                        Obj.ClassNo = reader["classNo"].ToString();
                        Obj.BookNo = reader["bookNo"].ToString();
                        Obj.Subject = reader["subject"].ToString();
                        Obj.Department = reader["department"].ToString();
                        Obj.Status = reader["status"].ToString();
                        System.Threading.Thread.Sleep(100);
                        // To Report progress.0
                        MessageBox.Show(Obj.AccessionNo.ToString());
                        bgwFillDGV.ReportProgress(i, Obj);

                        if (bgwFillDGV.CancellationPending)
                        {
                            // Set the e.Cancel flag so that the WorkerCompleted event
                            // knows that the process was cancelled.
                            e.Cancel = true;

                            bgwFillDGV.ReportProgress(0);
                            return;
                        }
                        i++;
                    }
                    conn.Close();
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    private void bgwFillDGV_ProgressChanged(object sender, 
                   ProgressChangedEventArgs e)
    {
        if (!bgwFillDGV.CancellationPending)
        {

            RetriveTableData Obj = (RetriveTableData)e.UserState;
            dataGridView1.Rows.Add(Obj.AccessionNo.ToString(), Obj.Author.ToString(), Obj.Title.ToString(), Obj.Edition.ToString(), Obj.ClassNo.ToString(), Obj.BookNo.ToString(), Obj.Subject.ToString(), Obj.Department.ToString(),Obj.Status.ToString());
            progressBar1.Value = e.ProgressPercentage;
            toolStripStatusLabel1.Text = "Processing row.. " + e.ProgressPercentage.ToString() + " of " + TotalRecords;
        }
    }
     private DataTable objToDataTable(RetriveTableData obj)
    {
        RetriveTableData objmkt = new RetriveTableData();
        sTable3.Columns.Add("AccessionNo",typeof(string));
        sTable3.Columns.Add("Author", typeof(string));
        sTable3.Columns.Add("Title", typeof(string));
        sTable3.Columns.Add("Edition", typeof(string));
        sTable3.Columns.Add("ClassNo", typeof(string));
        sTable3.Columns.Add("BookNo", typeof(string));
        sTable3.Columns.Add("Subject", typeof(string));
        sTable3.Columns.Add("Department", typeof(string));
        sTable3.Columns.Add("Status", typeof(string));
        foreach (PropertyInfo info in 
      typeof(RetriveTableData).GetProperties())
        {
           sTable3.Rows.Add(info.Name);
        }
        sTable3.AcceptChanges();
        return sTable3;
    }

enter image description here

0 个答案:

没有答案