双击DataGridView行并在C#中的另一个DataGridView中显示列

时间:2017-01-31 10:29:07

标签: c# winforms datagridview

所以,我正在尝试构建一个代码,我可以在单击F1之后以datagridview的形式显示textbox,具体取决于datagridview。然后我想在该行中双击并在主datagridview中显示我选择的新表单private void dataGridView1_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e) { DataGridViewRow dr = dataGridView1.SelectedRows[0]; this.Hide(); frmPrincipal frm = new frmPrincipal(); try { con = new SqlConnection(cs.DBConnP); con.Open(); cmd = new SqlCommand(@"SELECT RTRIM(CL.Cargs) AS 'Cargs', RTRIM(S.Abvs) AS 'Abvs', RTRIM(CL.Linha) AS 'Linha', RTRIM(CL.Qty) AS 'Quantity' FROM CargsCab CC (NOLOCK) INNER JOIN CargsLin CL (NOLOCK) ON CC.Cargs = CL.Cargs INNER JOIN Stock S (NOLOCK) ON CL.Code = S.Code INNER JOIN Marks M (NOLOCK) ON S.Marks = M.Marks WHERE CC.Date >= GETDATE() - 120 AND CL.State NOT IN ('F', 'A') AND S.TypeEmb = 'P' AND CC.Type = 'OCS' AND CL.Cargs LIKE '%" + dr.Cells[0].Value.ToString() + "%' ORDER BY CL.Cargs, S.Marks DESC, S.Abvs", con); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds, "CargCab"); da.Fill(ds, "CargLin"); da.Fill(ds, "Stock"); da.Fill(ds, "Mark"); frm.dataGridView1.DataSource = ds.Tables["CargCab"].DefaultView; frm.dataGridView1.DataSource = ds.Tables["CargLin"].DefaultView; frm.dataGridView1.DataSource = ds.Tables["Stock"].DefaultView; frm.dataGridView1.DataSource = ds.Tables["Mark"].DefaultView; con.Close(); } catch (Exception ex) { MessageBox.Show("Error\nDetalhes: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } 。这是我得到的代码:

    \Mail::send($view ,$data,function($message) use($user, $subject, $email){
              $message->from('ewomaukah@yahoo.com','witskids');
              $message->to($email, $user->firstname );
              $message->subject( $subject);
        });

我怎么解决?

1 个答案:

答案 0 :(得分:0)

如果我理解你,你想从弹出表单中选择数据回到你的主表单。 我更喜欢这样做:

在创建新表单时在主窗体上,附加到它的事件处理程序

FormClosing

在您需要在新表单中添加的事件调用方法上 例如

var selectedData= MyForm.GetData();

这是可用的,因为表格仍在关闭但尚未处理。

您的方法“GetData”当然必须从datagridView中收集选定的数据。 在stackoverflow上多次覆盖了这一点,因此您可以轻松找到它。

更正方式:

  public partial class Form1 : Form
{

    private Form2 f2;
    private obj returnedData;

    public Form1()
    {
        InitializeComponent();


    }



    private void button1_Click(object sender, EventArgs e)
    {
         f2 = new Form2();
        f2.FormClosing += F_FormClosing;
        f2.Show();
    }

    private void F_FormClosing(object sender, FormClosingEventArgs e)
    {
        this.returnedData = f2.GetData();  //clicked data is returned to your main form, do what you want with it
    }
}

public partial class Form2 : Form
{
    private List<obj> list;
    private obj selectedData;

    public Form2()
    {
        InitializeComponent();
        data.CellDoubleClick += DataGridView1_CellDoubleClick;

        list = new List<obj>();
        data.AutoGenerateColumns = true;
        list.Add(new obj() { a = "abc1", b = "abc2" });
        list.Add(new obj() { a = "cda1", b = "cda2" });
        data.DataSource = list;



    }



    internal obj GetData()
    {
        return selectedData;
    }

    private void DataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
    {

        selectedData = list.ElementAt(e.RowIndex);

        this.Close();
    }

}

internal class obj
{
    [DisplayName("aaa")]
    public string a { get; set; }

    [DisplayName("bbb")]
    public string b { get; set; }
}