简体   繁体   English

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

[英]Double click in DataGridView row and display columns in another DataGridView in C#

So, I am trying to build a code where I could be able to after click F1 it will display in a new form a datagridview depending on the textbox . 所以,我正在尝试构建一个代码,我可以在单击F1之后,它将以新的形式显示datagridview具体取决于textbox Then I want to double click in that row and display in the main datagridview what I selected into the new form datagridview . 然后我想在该行中双击并在主datagridview显示我在新表单datagridview This is the code I got: 这是我得到的代码:

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);
    }
}

How could I solve it? 我该怎么解决?

If I understood you well, you wanna get selected data from popup Form back to your main form. 如果我理解你,你想从弹出窗口中选择数据回到你的主表单。 I prefer doing it in following way: 我更喜欢这样做:

On main form when you are creating new Form, attach to it event handler 在创建新表单时在主窗体上,附加到它的事件处理程序

FormClosing

On that event call method that you need to add in your new form for example 例如,您需要在新表单中添加的事件调用方法

var selectedData= MyForm.GetData();

This is available because form is still closing and not yet disposed. 这是可用的,因为表单仍在关闭但尚未处理。

Your method "GetData" has to collect selected data form datagridView of course. 您的方法“GetData”当然必须从datagridView中收集选定的数据。 This was covered many times on stackoverflow so you will find this easily. 在stackoverflow上多次覆盖了这一点,因此您可以轻松找到它。

Corrected way: 纠正方式:

  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; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM