Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
hi new to c#
i have created a datagridview in form one. When i double click a row in the datagridview, the program populates form2 with all the information in that row. How do i update the datagridview in form1 from form 2? I also cannot get the rowindex in form2.

Any help will be appreciated.

FORM1cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.VisualBasic;

namespace Datagridview1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        DataTable table = new DataTable();

        private void Form1_Load(object sender, EventArgs e)
        {
            //' Add columns to your datatable, 
            // with the name of the columns and their type 

            table.Columns.Add("Id", Type.GetType("System.Int32"));
            table.Columns.Add("First Name", Type.GetType("System.String"));
            table.Columns.Add("Last Name", Type.GetType("System.String"));
            table.Columns.Add("Age", Type.GetType("System.Int32"));

            // Add rows to the datatable with some data

            table.Rows.Add(1, "XXXX", "YYYYY", 21);
            table.Rows.Add(2, "SSDD", "hGSQ", 33);
            table.Rows.Add(3, "fgfgd", "jgfdd", 53);
            table.Rows.Add(4, "cvfghyghj", "sdrgtyh", 19);
            table.Rows.Add(5, "hghfd", "ghjgdf", 36);
            table.Rows.Add(6, "cvvdfgh", "juyrfdvc", 63);

            dataGridView1.DataSource = table;
        }

        public void btnInsert_Click(object sender, EventArgs e)
        {
           
        }
        int index;
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            // get the index of the selected datagridview row
            index = e.RowIndex;

            DataGridViewRow selectedRow = new DataGridViewRow();

            // display data from datagridview selected row to textboxes

            selectedRow = dataGridView1.Rows[index];

            textBox1.Text = selectedRow.Cells[0].Value.ToString();
            textBox2.Text = selectedRow.Cells[1].Value.ToString();
            textBox3.Text = selectedRow.Cells[2].Value.ToString();
            textBox4.Text = selectedRow.Cells[3].Value.ToString();
            label6.Text = index.ToString();       



        }
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            // the new row
            DataGridViewRow newDataRow = new DataGridViewRow();
            // get data from textboxes to the row
            newDataRow = dataGridView1.Rows[index];
            newDataRow.Cells[0].Value = textBox1.Text;
            newDataRow.Cells[1].Value = textBox2.Text;
            newDataRow.Cells[2].Value = textBox3.Text;
            newDataRow.Cells[3].Value = textBox4.Text;
        }
     

        private void btnDelete_Click(object sender, EventArgs e)
        {
            index = dataGridView1.CurrentCell.RowIndex;
            dataGridView1.Rows.RemoveAt(index);
          
        }

        public void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
           
            index = e.RowIndex;                                //      Add THIS TO POPULATE THE SECOND FORM
            Form2 pos = new Form2();
            DataGridViewRow selectedRow = new DataGridViewRow();
            selectedRow = dataGridView1.Rows[index];
            pos.textBox5.Text = this.dataGridView1.CurrentRow.Cells[0].Value.ToString();
            pos.textBox6.Text = this.dataGridView1.CurrentRow.Cells[1].Value.ToString();
            pos.textBox7.Text = this.dataGridView1.CurrentRow.Cells[2].Value.ToString();
            pos.textBox8.Text = this.dataGridView1.CurrentRow.Cells[3].Value.ToString();
            
            pos.ShowDialog();

        }    

      
        }
    }

FORM2cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Datagridview1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
           
            InitializeComponent();
            Form1 frm1 = new Form1();
        

        }
        int index;
        private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
           index= e.RowIndex;
            label1.Text = index.ToString();
          
            //  index = e.RowIndex;
            Form1 frm1 = new Form1();
            // the new row
            DataGridViewRow newDataRow = new DataGridViewRow();
            // get data from textboxes to the row
            newDataRow = dataGridView1.Rows[index];
            newDataRow.Cells[0].Value = textBox1.Text;
            newDataRow.Cells[1].Value = textBox2.Text;
            newDataRow.Cells[2].Value = textBox3.Text;
            newDataRow.Cells[3].Value = textBox4.Text;

        }
       
      
      
        public Form2(DataGridView dataGridView1)      // ADD THIS
        {
           
          //  DataGridViewRow row = (DataGridViewRow)dataGridView1.Rows[e.RowIndex];
        }

        private void Delete_Click(object sender, EventArgs e)
        {
            index = dataGridView1.CurrentCell.RowIndex;
            dataGridView1.Rows.RemoveAt(index);
        }
       // int index;
       
     //  public int index { get; }
    
        private void Update_Click(object sender, EventArgs e)
        {
         
            DataGridViewRow newDataRow = new DataGridViewRow();
            newDataRow.Cells[0].Value = textBox1.Text;
            newDataRow.Cells[1].Value = textBox2.Text;
            newDataRow.Cells[2].Value = textBox3.Text;
            newDataRow.Cells[3].Value = textBox4.Text;




        }
        DataGridView parentDGV= new DataGridView();  // THIS IS IMPORTANT
        public DataGridView dataGridView1;
    }

   

}

What I have tried:

hi
i have tried almost everything spend over 5 days searching the net.
have tried as per code above.
Posted
Updated 2-Jul-20 1:48am

1 solution

You need to check the return from the call to pos.ShowDialog(); in the double-click event. If the form returns a positive response (like OK, or Yes etc), then copy the values from the Form2 fields into the DataGridView row.

Quote:
I also cannot get the rowindex in form2.
Not sure what you mean by that.
 
Share this answer
 

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