1
DataGridviewのデータベースからデータを取得した後、最後のデータが繰り返されますが、datagridviewの最初のデータが欠落しています。 1122逃していると最後のデータPEC-5281が繰り返される:私は、私はファースト・データはこの出力を得た。このリンクhttps://www.mindstick.com/Articles/1148/datagrid-using-backgroundworker-c-sharpC#でbackgroundworkerを使用してdatagridviewでデータベースデータを取得する
private void backgroundWorker1_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.EmpId = reader["C_FICH"].ToString();
// Obj.EmpName = reader["C_SITE"].ToString();
Obj.AccessionNo = reader["accessionNo"].ToString();
Obj.Author = reader["author"].ToString();
Thread.Sleep(100);
// To Report progress.
backgroundWorker1.ReportProgress(i, Obj);
if (backgroundWorker1.CancellationPending)
{
// Set the e.Cancel flag so that the WorkerCompleted event
// knows that the process was cancelled.
e.Cancel = true;
backgroundWorker1.ReportProgress(0);
return;
}
i++;
}
conn.Close();
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public class RetriveTableData
{
public string AccessionNo;
public string Author;
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
if (!backgroundWorker1.CancellationPending)
{
//Gets the user state that is sent as part of ReportProgress() Method from DoWork Event
RetriveTableData Obj = (RetriveTableData)e.UserState;
//Add the data to the dataGridView1
dataGridView1.Rows.Add(Obj.AccessionNo.ToString(), Obj.Author.ToString());
progressBar1.Value = e.ProgressPercentage;
label1.Text = "Processing row.. " + e.ProgressPercentage.ToString() + " of " + TotalRecords;
}
}
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;
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Cancelled)
{
label1.Text = "Cancelled by User Intentionally...";
progressBar1.Value = 0;
}
// Check to see if an error occurred in the background process.
else if (e.Error != null)
{
label1.Text = e.Error.Message;
}
else
{
// BackGround Task Completed with out Error
label1.Text = " All Records Loaded...";
}
}
private void button1_Click(object sender, EventArgs e)
{
// statusStrip1.Visible = true;
// toolStripStatusLabel1.Visible = true;
dataGridView1.ColumnCount = 2;
dataGridView1.Columns[0].Name = "Access No.";
dataGridView1.Columns[0].Width = 150;
dataGridView1.Columns[1].Width = 150;
dataGridView1.RowHeadersWidth = 21;
dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing;
dataGridView1.ColumnHeadersHeight = 23;
dataGridView1.Columns[1].Name = "Author";
progressBar1.Maximum = GetTotalRecords();
if (!backgroundWorker1.IsBusy)
{
RetriveTableData TObj = new RetriveTableData();
dataGridView1.Rows.Clear();
// Start the BackGround Thread to Execute
backgroundWorker1.RunWorkerAsync(TObj);
// btStart.Enabled = false;
//btCancel.Enabled = true;
}
}
に従ってください。私の低レベルのために私は画像を投稿できなかったので、あなたは出力を見ることができるリンクを置く。
あなたはObjが1つしかありません。奇妙な方法でそれを取得しています。 –
どういう意味ですか?私は返信を理解できません –
whileループ内に新しいobjを作成する必要があります。 –