从一个数据网格中选择一行,然后插入到另一个数据网格中 [英] Select a row from one datagrid and insert into another datagrid

查看:57
本文介绍了从一个数据网格中选择一行,然后插入到另一个数据网格中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

真的很挣扎.我有数据网格dg1和dg2.
是否可以从dg1中选择一行并用dg1中的选定行更新dg2中的行,请单击一个按钮.这两个数据网格都绑定到数据库.

如果有人可以向正确的方向推动我,那就是kwl

Hi all,

Really been struggling. I have datagrids dg1 and dg2.
Is it possible to select a row from dg1 and update a row in dg2 with the selected row in dg1, wen a button is clicked. Both datagrids are binded to a database.

If someone can push me in the right direction, that be kwl

// GridView2.Rows.add(GridView1.Rows(GridView1.SelectedIndex))  doesnt work in wpf
            dgCurrentExercise.Items.Add(dgExercises.SelectedItem);

nd这只是


nd this just doesnt


public DataView GetExercisesByMuscleTarget(string muscle)
       {
           DataSet ds = new DataSet("MyDataSet");

           using (SqlConnection conn = new SqlConnection(connectionString))
           {

               SqlCommand cmd = conn.CreateCommand();
               cmd.CommandType = CommandType.StoredProcedure;
               cmd.CommandText = "procExerciseMuscleTarget";

               cmd.Parameters.Add(new SqlParameter("@muscleTarget", SqlDbType.NVarChar, 20));
               cmd.Parameters["@muscleTarget"].Value = muscle;

               SqlDataAdapter da = new SqlDataAdapter(cmd);
               da.Fill(ds);

           }
           return ds.Tables[0].DefaultView;
       }







private void cmbMuscleTarget_SelectionChanged(object sender, SelectionChangedEventArgs e)
       {
           string muscle;
           muscle = cmbMuscleTarget.SelectedItem.ToString();
           dgExercises.ItemsSource = bl.GetExercisesByMuscleTarget(muscle);

推荐答案

*添加控件dg1(datagridview),dg2(datagridview),btnCopy2Dg2(按钮)

*单击按钮时,将dg1的选定行复制到dg2,希望这对您有帮助

* add controls dg1(datagridview),dg2(datagridview),btnCopy2Dg2 (button)

* when you click on the button the selected row of dg1 is copied to dg2 and hope this helps

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
  
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        DataSet ds1= new DataSet() ;
        DataSet ds2= new DataSet() ;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                SqlConnection oConn = new SqlConnection("Data Source=SOLAP;Database=Sample;user=sa;password=admin1990");
                oConn.Open();

                //LOAD GRID1
                SqlCommand oCmd = new SqlCommand("select companyname,address from customers", oConn);  
                SqlDataAdapter oda1 = new SqlDataAdapter(oCmd);
                oda1.Fill(ds1);
                dg1.DataSource = ds1.Tables[0].DefaultView;

                //LOAD GRID2
                SqlCommand oCmd2 = new SqlCommand("select companyname,address from customerscopy", oConn);
                SqlDataAdapter oda2 = new SqlDataAdapter(oCmd2);
                oda2.Fill(ds2);
                dg2.DataSource = ds2.Tables[0].DefaultView;

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

            }
        }

        private void btnCopy2Dg2_Click(object sender, EventArgs e)
        {

            DataGridViewSelectedRowCollection oSoruceRow = dg1.SelectedRows;

            var x=ds2.Tables[0].Rows.Add();
            x[0] = oSoruceRow[0].Cells[0].Value  ;
            x[1] = oSoruceRow[0].Cells[1].Value;
            
        }
    }
}


这篇关于从一个数据网格中选择一行,然后插入到另一个数据网格中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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