从第二个表单填充DataGridView [英] Populate DataGridView from second form

查看:106
本文介绍了从第二个表单填充DataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些正确的方向来解决这个小问题。



我有两种形式。在Form1上,我有空的dataGridView1和一个名为btnAdd的按钮。当用户单击按钮时,将显示Form2(From2.ShowDialog())。在这个form2我有dataGridView2和一个按钮btn2。 Datagridview绑定到一个SQL表(它显示id和名称列)。当用户在dataGridView中选择一行并单击btn2时,我需要将该行添加到dataGridView1中。我该如何最好地完成这个。谢谢你的帮助。



这是一些代码。

  //我显示新表单
private void btnAdd_Click(object sender,EventArgs e)
{
Form2 form2 = new Form2();
From2.Text =some title text;
form2.ShowDialog(this);
}

//在form2中,我将数据绑定到dataGridView2,需要将
//所选项添加到dataGridView1
public Form2()
{
InitializeComponent();
getData();
}

private void getData()
{
try
{
String connectionString =我的连接字符串;
SqlConnection connection = new SqlConnection(connectionString);
DataTable data = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(我的SQL查询,连接);
da.Fill(data);
dataGridView2.DataSource = data;
}
catch(SqlException ex)
{
MessageBox.Show(ex.ToString());
}
}

private void btn2_Click(object sender,EventArgs e)
{
if(dataGridView2.SelectedCells.Count> 0)
{
int i = dataGridView2.SelectedCells [0] .RowIndex;
DataGridViewRow r = dataGridView2.Rows [i];
//需要将选定的行添加到dataGridView1
}
}


解决方案

您可以使用所有者属性

  Form2 form2 = new Form2(dataGridView1); 
From2.Text =some title text;
form2.Owner = this;
form2.ShowDialog(this);

而在form2上;

 ((Form1)this.Owner).YOURMETHODTOADDROW(您的参数)
/ pre>

实现YOURMETHODTOADDROW将行添加到网格,然后刷新grid1


I need some steering in right direction on how to solve this little problem.

I have two forms. On Form1 I have empty dataGridView1 and a button named btnAdd. When user clicks on the button, Form2 is shown (From2.ShowDialog()). On this form2 I have dataGridView2 and a button btn2. Datagridview is bound to an SQL table (it shows id and name columns). When user selects a row in dataGridView and clicks btn2, I need to add that row into dataGridView1. How would I best accomplish this. Thanks for your help.

Here is some code I have.

// I show new form
private void btnAdd_Click(object sender, EventArgs e)
{
  Form2 form2 = new Form2();
  From2.Text = "some title text";
  form2.ShowDialog(this);
}

// In form2 I bind data to dataGridView2 and need to add
// selected item to dataGridView1
public Form2()
{
  InitializeComponent();
  getData();
}

private void getData()
{
  try
  {
    String connectionString = "my connection string";
    SqlConnection connection = new SqlConnection(connectionString);
    DataTable data = new DataTable();
    SqlDataAdapter da = new SqlDataAdapter("my SQL query", connection);
    da.Fill(data);
    dataGridView2.DataSource = data;
  }
  catch (SqlException ex)
  {
    MessageBox.Show(ex.ToString());
  }
}

private void btn2_Click(object sender, EventArgs e)
{
  if (dataGridView2.SelectedCells.Count > 0)
  {
    int i = dataGridView2.SelectedCells[0].RowIndex;
    DataGridViewRow r = dataGridView2.Rows[i];
    //Need to add selected row to dataGridView1
  }
}

解决方案

You can use Owner property

Form2 form2 = new Form2(dataGridView1);
        From2.Text = "some title text";
form2.Owner=this;
        form2.ShowDialog(this);

And on form2 ;

((Form1)this.Owner).YOURMETHODTOADDROW(your parameter)

implement YOURMETHODTOADDROW to add row to grid then refresh your grid1

这篇关于从第二个表单填充DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆