Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi. I have 2 datagridview in one windows form, When I click row from datagridview1, it shows in datagridview2 (Select *from....). Also I have combobox column in datagridview2. My question is, when I select items in combobox, and click to new row in datagridview 1, datagridview2 combobox's is getting empty. it is my code:

C#
private void dataGridView1_CellDoubleClick_1(object sender, DataGridViewCellEventArgs e)
       {
           try
           {
               int id = Convert.ToInt32

    (dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["id"].Value);//3

               MySqlConnection start = new MySqlConnection(baglanti);
               MySqlCommand command = start.CreateCommand();
               command.CommandText = "SELECT id, muayine_adi, sabit_qiymet FROM tibbi_xidmetler  WHERE id = '" + id.ToString() + "'";
               start.Open();
               MySqlDataAdapter oxu = new MySqlDataAdapter(command);
               DataTable dt = new DataTable();
               DataSet ds = new DataSet();
               oxu.Fill(dt);
               if (dataGridView2.DataSource != null)
               {
                   DataTable pr = dataGridView2.DataSource as DataTable;
                   bool exists = false;
                   foreach (DataRow dr in pr.Rows)
                   {
                       if (dr.RowState != DataRowState.Deleted)
                       {
                           if (Convert.ToInt32(dr["id"]) == id)
                           {
                               exists = true;
                               break;
                           }
                       }

                   }

                   if (!exists)
                   {
                       pr.Merge(dt);
                       dataGridView2.DataSource = pr;
                   }

               }
               else
               {

                   dataGridView2.DataSource = dt;
               }

               oxuma_hekimler();

               if (dataGridView2.Columns["Sil"] == null)
               {
                   DataGridViewButtonColumn col1 = new DataGridViewButtonColumn();
                   col1.UseColumnTextForButtonValue = true;
                   col1.Text = "Sil";
                   col1.Name = "Sil";
                   dataGridView2.Columns.Add(col1);
               }

               dataGridView2.Columns[0].ReadOnly = true;
               dataGridView2.Columns[1].ReadOnly = true;
               dataGridView2.Columns[2].ReadOnly = true;
               dataGridView2.Columns[3].ReadOnly = false;

               DataGridViewColumn column = dataGridView2.Columns[3];
               column.Width = 200;
               DataGridViewColumn column1 = dataGridView2.Columns[1];
               column1.Width = 250;
               DataGridViewColumn column2 = dataGridView2.Columns[0];
               column2.Width = 40;
               DataGridViewColumn column3 = dataGridView2.Columns[2];
               column3.Width = 60;
               oxuma_sum();


           }
           catch (Exception ex)
           {

               MessageBox.Show(ex.Message);
           }
       }


and it is void to get combobox items


C#
void oxuma_hekimler()
       {
           MySqlConnection baglan = new MySqlConnection(baglanti);
           MySqlCommand command = baglan.CreateCommand();

           command.CommandText = "SELECT hekim_terifi.id, CONCAT (yeni_personal.adi,' ',yeni_personal.soyadi,' (',sobeler.sobe_adi,')') as melumat FROM hekim_terifi INNER JOIN yeni_personal ON yeni_personal.id=hekim_terifi.personal_id INNER JOIN hekim_sobe_tanim ON hekim_sobe_tanim.hekim_id=hekim_terifi.id INNER JOIN sobeler ON sobeler.id=hekim_sobe_tanim.sobe_id";


           baglan.Open();
           MySqlDataAdapter oxu = new MySqlDataAdapter(command);
           DataTable db = new DataTable();
           oxu.Fill(db);

           if (dataGridView2.Columns["combo1"] == null)
           {
               DataGridViewComboBoxColumn col = new DataGridViewComboBoxColumn();
               col.Width = 100;
               col.HeaderText = "Müayinə Həkimi...";
               col.DataSource = db;
               col.Name = "combo1";
               col.FlatStyle = FlatStyle.Standard;
               col.DataPropertyName = "combo1";
               col.DisplayMember = "melumat";
               col.ValueMember = "id";
               dataGridView2.Columns.Add(col);
           }

           int a = Convert.ToInt32(textBox1.Tag);

           foreach (DataGridViewRow row in dataGridView2.Rows)
           {
               (row.Cells[3] as DataGridViewComboBoxCell).Value = a;
           }

           baglan.Close();

       }


How can I solve this? I need help ;(
Posted
Updated 25-Dec-14 6:50am
v3
Comments
DamithSL 24-Dec-14 9:10am    
have you debug and check what is the difference in code execution in both first and second time?
Member 11284808 24-Dec-14 9:13am    
I am new and I don't know How can I do it
DamithSL 24-Dec-14 9:14am    
what is your visual studio version?
check Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
learn how to put Breakpoint and how to execute code line by line, also check the run time value of fields and datasets etc.. check the execution happening in both cases as expected or not. if not you can check the reason by runtime object values etc..
you will find the reason for your problem by yourself.
Member 11284808 24-Dec-14 13:08pm    
I will try, thanks
Member 11284808 25-Dec-14 2:17am    
I checked, but didn't figure it out :(

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900