0

Im new on C# i made an application which shows employees names, their city and their number etc. The datagridview bring data form online mysql database. When I click datagridview, the datas on datagridview is writen the textbox above... So i can delete or update an employee or add new one... Everything is ok till here...

But When i filtered datagridview data from a textbox (filter works meanwhlie) the datagridview double click event doesnt work and the application closed...

int secilen = dataGridView1.SelectedCells[0].RowIndex;

txtid.Text = dataGridView1.Rows[secilen].Cells[0].Value.ToString();

txtDb.Text = dataGridView1.Rows[secilen].Cells[1].Value.ToString();

txtsicil.Text = dataGridView1.Rows[secilen].Cells[2].Value.ToString();

txtBitTar.Text = dataGridView1.Rows[secilen].Cells[3].Value.ToString();

cmbEgtAd.Text = dataGridView1.Rows[secilen].Cells[4].Value.ToString();

and here my filter coddes

baglanti.Open();

MySqlCommand komutbulx = new MySqlCommand("Select * from Tbl_Yerinde Where sicilid like '%" + txtsicil.Text + "%'", baglanti);

DataTable dtx = new DataTable();

MySqlDataAdapter dax = new MySqlDataAdapter(komutbulx);

dax.Fill(dtx);
dataGridView1.DataSource = dtx;

baglanti.Close();

Aa far as i understand the problem is the index bring from datagridwiev. When its filtered brought index is not true... But how can I solve this?

  • I assure you that events work with filtered views. If the app closes without an exception, your code is likely swallowing an exception. Finally, never, ever create SQL by gluing data into a string, use SQL parameters always. – Ňɏssa Pøngjǣrdenlarp Nov 11 '19 at 17:16
  • The app close with an exception. When i disable the filter codes double click work again. Let say there are 300 employee i search a employee whos name is Zax... The Z letter the last letter, so Zax's real index about 300 but because of filter Zax showed at first row. When i clicked it try to return first index but the index come 300... So when I click datagridview it closes with 'System. ArgumentOutOfRangeException' – sercan hilmi çoban Nov 11 '19 at 18:10
  • Duplicate of [What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?](https://stackoverflow.com/questions/20940979/what-is-an-indexoutofrangeexception-argumentoutofrangeexception-and-how-do-i-f) – Ňɏssa Pøngjǣrdenlarp Nov 11 '19 at 19:26

0 Answers0