I am working in winforms. In that, I have a datagridview
. I have transferred the selected cell value to a new form, form2
But now I want to re-transfer the textbox value which is in form2
to the datagridview
cell.
How can i do that?
On the form2
, along with label1
there is button1
and a textbox
. I want that when the textbox
is filled and button1
is pressed, it will transfer the text from the textbox
to the cell which was selected.
I have used following code for this. The code of button_click
event...
But an error occurs as follwing.
" object reference not set to instance of an object "
You do recreate the main form inside of form 2, which is probably not what you need. Change the code to this:
private void editToolStripMenuItem_Click(object sender, EventArgs e)
{
form2 f2 = new form2();
f2.label1.Text = dataGridView1.SelectedCells[0].Value.ToString();
f2.ShowDialog();
dataGridView1.SelectedCells[0].Value = f2.textBox1.Text;
}
private void button1_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.OK;
}
Design Properties of DataGridView
dataGridView1.Modifiers = Public
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
using (var f = new Form2 { Owner = this})
{
f.valueFromSelectedCell = dataGridView1.SelectedCells[0].EditedFormattedValue.ToString();
f.ShowDialog();
}
}
}
public partial class Form2 : Form
{
public string valueFromSelectedCell { get; set; }
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
textBox1.Text = valueFromSelectedCell;
}
private void button1_Click(object sender, EventArgs e)
{
Form1 f = this.Owner as Form1;
var currentCell = f.dataGridView1.CurrentCell;
f.dataGridView1[currentCell.ColumnIndex, currentCell.RowIndex].Value = textBox1.Text;
Close();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.