【问题标题】:Add row from datagridview1 to datagridview2将行从 datagridview1 添加到 datagridview2
【发布时间】:2014-12-18 10:38:25
【问题描述】:

我有两个数据网格视图合二为一。我需要将数据从数据库获取到 datagridview1(使用 Select *from database...),然后我想使用 Selected Rows 将数据从 datagriwview 添加到 datagridview2。

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

private void dataGridView1_CellDoubleClick_1(object sender, DataGridViewCellEventArgs e)
    {


        int id = Convert.ToInt32

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

        try
        {

            MySqlConnection conn = new MySqlConnection(connection);
            MySqlCommand command = start.CreateCommand();
            command.CommandText = "SELECT id, muayine_adi, sabit_qiymet FROM tibbi_xidmetler  WHERE id = '" + id.ToString() + "'";

            conn.Open();
            MySqlDataAdapter oxu = new MySqlDataAdapter(command);
            DataTable dt = new DataTable();

                oxu.Fill(dt);
                dataGridView2.DataSource = dt;


        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

【问题讨论】:

    标签: c# datagridview


    【解决方案1】:

    解释很简单:每次双击 datagridview1 的单元格时,都会用新的数据表替换旧的数据表。如果你想追加结果,你可以这样做:

    MySqlConnection conn = new MySqlConnection(connection);
    MySqlCommand command = start.CreateCommand();
    command.CommandText = "SELECT id, muayine_adi, sabit_qiymet FROM tibbi_xidmetler  WHERE id = '" + id.ToString() + "'";
    conn.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;
    

    【讨论】:

    • 非常感谢,它正在工作,但是当我单击行两次时,它没有更新,它仍然插入同一行:(
    【解决方案2】:

    由于您在 datagridview1 中拥有所有信息,您只需将所选行的内容复制到 datagridrow2 的新行中即可。

    DataGridView 基于包含 DataTables 的 DataSet。 DataTable 包含行。 您不能将一行从一个表移动到另一个表。 相反,您必须创建一个新行并插入到 DataGridView2 的 DataTable 中

    【讨论】:

    • 我可以复制所选行的内容吗?
    • 我会使用一个小循环,复制每个“字段” row2[n] = row1[n];
    • 我是 C# 编程新手,你能给我举个例子吗?
    • 可以在Senthilkumar的解决方案中找到copy部分
    【解决方案3】:
    private void dataGridView1_CellDoubleClick_1(object sender, DataGridViewCellEventArgs e)
            {
    
                    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 id, muayine_adi, sabit_qiymet 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_adi"].Value = dt.Rows[i]["muayine_adi"].ToString();
                                dataGridView2.Rows[rVal].Cells["sabit_qiymet"].Value = dt.Rows[i]["sabit_qiymet"].ToString();
                            }
    
                        }
                        start.Close();
                    }
            }
    

    【讨论】:

    • 谢谢,但是行没有更新,我仍然可以添加两次相同的行:(
    • 我没有得到你。我是 C# 编程新手
    • 好的,你可以检查相同的ID是否重复,然后你可以添加
    • 在你的数据库中你维护整数或 varchar 格式来检查
    • id 是整数,第二个字段是 varchar,第三个字段是浮点数
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-03
    • 2015-05-31
    • 1970-01-01
    • 1970-01-01
    • 2020-09-18
    • 1970-01-01
    • 2014-01-27
    相关资源
    最近更新 更多