I'm trying to batch record using windows form. But When I click button to save, it works very slowly especially in this code block. Do you have a solution?
int i = 0;
List ChkedRow = new List();
if (dataGridView1.Rows.Count > 0)
for (i = 0; i <= dataGridView1.RowCount - 1; i++)
{
if (Convert.ToBoolean(dataGridView1.Rows[i].Cells["oid"].Value) == true)
{
ChkedRow.Add(i);
}
}
if (ChkedRow.Count == 0)
{
MessageBox.Show("Geziye katilacak ögrenci seçiminde bulunmadiniz!");
return;
}
Amit MohantyPosted Aug 4, 2023, 2:12 PM
The slowness you're experiencing could be due to the fact that you are iterating through all the rows in the DataGridView, which can be time-consuming if the DataGridView has a large number of rows. One way to potentially improve the performance is to use LINQ to filter the selected rows more efficiently.
Mehmet FatihPosted Aug 4, 2023, 3:35 PM
Thnaks Amit. This is better than mine. I am trying to record multiple datas at the same time.The rest of the codes is below. When I click the button, my prgress bar reacts very late.
label5.Visible = true;
progressBar1.Visible = true;
backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.RunWorkerAsync();
foreach (int j in ChkedRow)
{
try
{
var val1 = dataGridView1.Rows[j].Cells["tcno"].Value;
var val2 = dataGridView1.Rows[j].Cells["ono"].Value;
var val3 = dataGridView1.Rows[j].Cells["isim"].Value;
var val4 = dataGridView1.Rows[j].Cells["soyisim"].Value;
var val5 = dataGridView1.Rows[j].Cells["cinsiyet"].Value;
var val6 = dataGridView1.Rows[j].Cells["dtarihi"].Value;
var val7 = dataGridView1.Rows[j].Cells["sinifi"].Value;
var val8 = dataGridView1.Rows[j].Cells["unvan"].Value;
var val9 = dataGridView1.Rows[j].Cells["tel"].Value;
var val10 = dataGridView1.Rows[j].Cells["vtel"].Value;
var val11 = dataGridView1.Rows[j].Cells["kbaskani"].Value;
using (var conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source = gezievrak2541.accdb; Jet OLEDB:Database Password = Fatih2541; Mode = ReadWrite"))
{
conn.Open();
using (var cmd = new OleDbCommand("select * from gezilistemiz25 where adi = '" + val3 + "' and soyadi= '" + val4 + "' ", conn))
{
using (OleDbDataReader dr = cmd.ExecuteReader())
{
if (dr.Read())
{
while (dr.Read())
{
MessageBox.Show(" '" + val3 + " " + val4 + "' isimli ögrenciler veritabaninda kayitlidir. Mükerrer kayit yapilamaz. Lütfen kontrol ediniz.");
}
}
else
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
var cmdText = @"INSERT INTO gezilistemiz25 (tcno,ono,adi,soyadi,cinsiyet,dtarihi,sinifi,unvani,tel,vtel,kbaskani) VALUES(@tcno,@ono,@adi,@soyadi,@cinsiyet,@dtarihi,@sinifi,@unvani,@tel,@vtel,@kbaskani)";
var komut = new OleDbCommand(cmdText, conn);
komut.Parameters.AddWithValue("@tcno", val1);
komut.Parameters.AddWithValue("@ono", val2);
komut.Parameters.AddWithValue("@isim", val3);
komut.Parameters.AddWithValue("@soyisim", val4);
komut.Parameters.AddWithValue("@sinifi", val5);
komut.Parameters.AddWithValue("@cinsiyet", val6);
komut.Parameters.AddWithValue("@unvan", val7);
komut.Parameters.AddWithValue("@dtarihi", val8);
komut.Parameters.AddWithValue("@tel", val9);
komut.Parameters.AddWithValue("@vtel", val10);
komut.Parameters.AddWithValue("@kbaskani", val11);
komut.ExecuteNonQuery();
komut.Dispose();
komut.Parameters.Clear();
conn.Close();
conn.Dispose();
}
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
Mehmet FatihPosted Aug 4, 2023, 4:07 AM
Thanks for your intrest Cr Bhargavi. When I tried your corrected code, it saved all of the checked and unchecked rows without giving message ("Geziye katilacak ögrenci seçmediniz!");
Cr BhargaviPosted Aug 4, 2023, 1:29 AM
List ChkedRow = new List();
foreach (DataGridViewRow row in dataGridView1.SelectedRows)
{
ChkedRow.Add(row.Index);
}
if (ChkedRow.Count == 0)
{
MessageBox.Show("Geziye katilacak ögrenci seçmediniz!");
return;
}
Additionally, you can improve performance by setting the `MultiSelect` property of the `dataGridView1` to `false` if you only expect users to select a single row at a time. This way, you can use `dataGridView1.SelectedRows[0]` instead of the `foreach` loop to get the selected row's index directly.
if (dataGridView1.SelectedRows.Count == 0)
{
MessageBox.Show("Geziye katilacak ögrenci seçmediniz!");
return;
}
int selectedIndex = dataGridView1.SelectedRows[0].Index;
// Use the selectedIndex as needed.