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)
{
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"));
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)
{
index = e.RowIndex;
DataGridViewRow selectedRow = new DataGridViewRow();
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)
{
DataGridViewRow newDataRow = new DataGridViewRow();
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;
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();
Form1 frm1 = new Form1();
DataGridViewRow newDataRow = new DataGridViewRow();
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)
{
}
private void Delete_Click(object sender, EventArgs e)
{
index = dataGridView1.CurrentCell.RowIndex;
dataGridView1.Rows.RemoveAt(index);
}
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();
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.