Click here to Skip to main content
15,916,412 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

SQL
// GridView2.Rows.add(GridView1.Rows(GridView1.SelectedIndex))  doesnt work in wpf
            dgCurrentExercise.Items.Add(dgExercises.SelectedItem);
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;
       }



C#
private void cmbMuscleTarget_SelectionChanged(object sender, SelectionChangedEventArgs e)
       {
           string muscle;
           muscle = cmbMuscleTarget.SelectedItem.ToString();
           dgExercises.ItemsSource = bl.GetExercisesByMuscleTarget(muscle);
Posted
Updated 1-Oct-11 13:29pm
v3

1 solution

* 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;
            
        }
    }
}
 
Share this answer
 
Comments
Member 12719002 7-Sep-16 11:31am    
please send a code in wpf ,during cell double click entire row content move from one datagrid to another datagrid without using button.please please give the xaml and xaml.cs code.

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