C# WinForm DataGridView取当前行和窗体之间传值或调方法
DataGridView获取当前行代码
int rows = this.dgv_Info.CurrentCell.RowIndex;//获取当前选中的行
DataGridView获取当前选中行的某列值
string code = this.dataGridView.CurrentRow.Cells["列名"].Value.ToString();
窗体之间传值
本次示例效果如下:
Form1为父窗体(包含textBox1、button1)
Form2为子窗体(包含textBox2、button2)
父窗体给子窗体传值
==================
1.点击Form1的button1 打开Form2
父窗体给子窗体传值 可以调用重载子窗体的构造函数 直接传入相关数值
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2(this.textBox1.Text);
frm2.Show();
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public Form2(string strTextBox1Text)
{
InitializeComponent();
this.textBox2.Text = strTextBox1Text;
}
}
2.点击Form1的button1 打开Form2
并调用子窗体Form2的公开属性或方法 将Form1的textBox1的值设置给Form2的textBox2
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.TextBox2Text = this.textBox1.Text;
frm2.Show();
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public string TextBox2Text
{
set { this.textBox2.Text = value; }
get { return this.textBox2.Text; }
}
}
3.点击Form1的button1 打开Form2
在Form2_Load调用父窗体Form1的公开属性或方法 将Form1的textBox1的值设置给Form2的textBox2
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public string TextBox1Text
{
set { this.textBox1.Text = value; }
get { return this.textBox1.Text; }
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show(this);//或 frm2.ShowDialog(this);
////或者
//Form2 frm2 = new Form2();
//frm2.Owner = this;
//frm2.Show();//或 frm2.ShowDialog();
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
Form1 frm1 = (Form1)this.Owner;
this.textBox2.Text = frm1.TextBox1Text;
}
}
子窗体给父窗体传值
==================
4.点击Form1的button1 打开Form2
再点击Form2的button2
在button2_Click事件中 通过this.Owner将Form2的textBox2的值设置给Form1的textBox1
并关闭Form2
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show(this);//或 frm2.ShowDialog(this);
////或者
//Form2 frm2 = new Form2();
//frm2.Owner = this;
//frm2.Show();//或 frm2.ShowDialog();
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
Form1 frm1 = (Form1)this.Owner;
//注意 如果textBox1是放在panel1中的 则先找panel1 再找textBox1
((TextBox)frm1.Controls["textBox1"]).Text = this.textBox2.Text;
this.Close();
}
}
5.点击Form1的button1 打开Form2
再点击Form2的button2
在button2_Click事件中 通过this.Owner及调用父窗体Form1的公开属性或方法
将Form2的textBox2的值设置给Form1的textBox1
并关闭Form2
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public string TextBox1Text
{
set { this.textBox1.Text = value; }
get { return this.textBox1.Text; }
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show(this);//或 frm2.ShowDialog(this);
////或者
//Form2 frm2 = new Form2();
//frm2.Owner = this;
//frm2.Show();//或 frm2.ShowDialog();
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
Form1 frm1 = (Form1)this.Owner;
frm1.TextBox1Text = this.textBox2.Text;
this.Close();
}
}
Form1为父窗体(包含textBox1、button1)
Form2为子窗体(包含textBox2、button2)
父窗体给子窗体传值
==================
1.点击Form1的button1 打开Form2
父窗体给子窗体传值 可以调用重载子窗体的构造函数 直接传入相关数值
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2(this.textBox1.Text);
frm2.Show();
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public Form2(string strTextBox1Text)
{
InitializeComponent();
this.textBox2.Text = strTextBox1Text;
}
}
2.点击Form1的button1 打开Form2
并调用子窗体Form2的公开属性或方法 将Form1的textBox1的值设置给Form2的textBox2
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.TextBox2Text = this.textBox1.Text;
frm2.Show();
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public string TextBox2Text
{
set { this.textBox2.Text = value; }
get { return this.textBox2.Text; }
}
}
3.点击Form1的button1 打开Form2
在Form2_Load调用父窗体Form1的公开属性或方法 将Form1的textBox1的值设置给Form2的textBox2
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public string TextBox1Text
{
set { this.textBox1.Text = value; }
get { return this.textBox1.Text; }
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show(this);//或 frm2.ShowDialog(this);
////或者
//Form2 frm2 = new Form2();
//frm2.Owner = this;
//frm2.Show();//或 frm2.ShowDialog();
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
Form1 frm1 = (Form1)this.Owner;
this.textBox2.Text = frm1.TextBox1Text;
}
}
子窗体给父窗体传值
==================
4.点击Form1的button1 打开Form2
再点击Form2的button2
在button2_Click事件中 通过this.Owner将Form2的textBox2的值设置给Form1的textBox1
并关闭Form2
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show(this);//或 frm2.ShowDialog(this);
////或者
//Form2 frm2 = new Form2();
//frm2.Owner = this;
//frm2.Show();//或 frm2.ShowDialog();
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
Form1 frm1 = (Form1)this.Owner;
//注意 如果textBox1是放在panel1中的 则先找panel1 再找textBox1
((TextBox)frm1.Controls["textBox1"]).Text = this.textBox2.Text;
this.Close();
}
}
5.点击Form1的button1 打开Form2
再点击Form2的button2
在button2_Click事件中 通过this.Owner及调用父窗体Form1的公开属性或方法
将Form2的textBox2的值设置给Form1的textBox1
并关闭Form2
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public string TextBox1Text
{
set { this.textBox1.Text = value; }
get { return this.textBox1.Text; }
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show(this);//或 frm2.ShowDialog(this);
////或者
//Form2 frm2 = new Form2();
//frm2.Owner = this;
//frm2.Show();//或 frm2.ShowDialog();
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
Form1 frm1 = (Form1)this.Owner;
frm1.TextBox1Text = this.textBox2.Text;
this.Close();
}
}
两个窗体之间调用方法
比如A窗体是父窗体有DataGridView,B窗体是添加窗体,当添加完成后A窗体的DataGridView刷新绑定。
首先在A窗体SHOW出B窗体的时候要加上
A.Owner = this;
在B窗体调用A窗体绑定DataGridView的方法
A a = (A)this.Owner;
a.DataGridView的绑定方法