Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

c# add selected row from datagridview1 to datagridvierw2

$
0
0

I have two datagridviews in one from. I need to get data from database to datagridview1 (using Select *from database...) then I want to add data from datagriwview1 to datagridview2 using Selected Rows.
 
First I wanted to solve this problem to get Selected Row's ID, and It works fine, but, when I select same row it adds twice. How can I check if Selected row already exists in datagridview2. Is there anyone to solve this problem and to give me an example? I am new (( Thanks

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();
            oxu.Fill(dt);
            if (dataGridView2.DataSource != null)
            {
                DataTable pr = dataGridView2.DataSource as DataTable;
                pr.Merge(dt);
                dataGridView2.DataSource = pr;
            }
         else
                dataGridView2.DataSource = dt;



Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>