将datagridview1中的行添加到datagridview2 [英] Add row from datagridview1 to datagridview2

查看:90
本文介绍了将datagridview1中的行添加到datagridview2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个来自的两个datagridviews。我需要从数据库获取数据到datagridview1(使用Select * from database ...)然后我想使用Selected Rows将数据从datagriwview添加到datagridview2。



First我想解决这个问题来获取Selected Row的ID,当我在datagridview中选择它在datagridview2中显示的行时,但当我选择另一行时,它在datagridview中更新,它不会添加为新行。我尝试了几种方法,但没有解决这个问题,有没有人帮我解决这个问题?谢谢



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 datagriwview to datagridview2 using Selected Rows.

First I wanted to solve this problem to get Selected Row's ID, when I select row in datagridview it shows in datagridview2, but when I select another row, it is updating in datagridview, it does not add as new row. I tried several ways but did not solve this problem, Is there anyone help me to solve this problem? 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;

推荐答案

如果我理解你的话......



最快的方法是使用 Linq [ ^ ]:

If i understand you well...

The quickest way is to use Linq[^]:
//get selected rows
var qry = from DataGridViewRow dgvr in DataGridView1.SelectedRows
    select dgvr;
DataGridView2.DataSource = qry;



注意:未经测试!只是一个想法。



第二个解决方案在这里:如何:获取Windows窗体DataGridView控件中的选定单元格,行和列 [ ^ ]。您需要遍历所选行的集合并使用StringBuilder类构建逗号分隔的id列表。最后,您需要将其作为CommandText传递:


Note: not tested! Just an idea.

Second solution is here: How to: Get the Selected Cells, Rows, and Columns in the Windows Forms DataGridView Control[^]. You need to loop through the collection of selected rows and using StringBuilder class build comma separated list of ids. Finally, you need to pass this as a CommandText:

//before loop
StringBuilder sb = new StringBuilder();
//inside loop
sb.Append(dataGridView1.SelectedCells[i].Value.ToString());
//outside the loop
command.CommandText = String.Concat("SELECT id, muayine_adi, sabit_qiymet FROM tibbi_xidmetler  WHERE id IN (", sb.ToString(),")");
//further code





注意:在使用第二个解决方案之前,我需要警告你 SQL注入 [ ^ ]。



Note: before you use second solution, i need to warn you about SQL Injection[^].


这是我使用它的另一种方式,但它既不起作用。如何检查datagridview2中是否已存在值?当我从datagridview1中选择行时,它会在datagridview2中显示,但是当我选择相同的行两次时,它会重复((((



It is another way I used it, but It did not work neither. How can I check if value already exists in datagridview2? When I select row from datagridview1 it shows in datagridview2, but when I select same row twice, it repeats ((((

if (dataGridView1.CurrentRow.Cells["Id"].Value != null)
                {
                    int Id = Convert.ToInt32(dataGridView1.CurrentRow.Cells["Id"].Value);

                    MySqlConnection start = new MySqlConnection(baglanti);
                    MySqlCommand command = start.CreateCommand();
                    command.CommandText = "SELECT muayine_alt_qrup_id FROM tibbi_xidmetler  WHERE id = '" + Id + "'";
                    start.Open();
                    MySqlDataAdapter oxu = new MySqlDataAdapter(command);
                    DataTable dt = new DataTable();
                    oxu.Fill(dt);
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        int idx = dataGridView2.Rows.Count - 1;
                        dataGridView2.Rows.Add(dt.Rows.Count);
                        for (int i = 0; i <= dt.Rows.Count - 1; i++)
                        {
                            int rVal = (idx + i) + 1;
                          //  dataGridView2.Rows[rVal].Cells["id"].Value = dt.Rows[i]["id"].ToString();
                            dataGridView2.Rows[rVal].Cells["muayine_alt_qrup_id"].Value = dt.Rows[i]["muayine_alt_qrup_id"];
                           // dataGridView2.Rows[rVal].Cells["sabit_qiymet"].Value = dt.Rows[i]["sabit_qiymet"].ToString();
                        }

                    }
                    start.Close();
                }


这篇关于将datagridview1中的行添加到datagridview2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆