Как обновить строку в datagridview в form1 из текстовых полей в form2
привет новичок в c#
я создал datagridview в форме один. Когда я дважды щелкаю строку в datagridview, программа заполняет form2 всей информацией в этой строке. Как обновить datagridview в форме 1 из формы 2? Я также не могу получить rowindex в form2.
Любая помощь будет оценена по достоинству.
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.