c# - 如何从父表单中的另一个 datagridview 更新子表单的 datagridview

标签 c# mysql sql database datagridview

我有DataGridView(dgHome),在parentForm(Home)中有一些列。 子 Form(bill) 有 DataGridView(dgbill) 。 我需要当我单击 dgHome 中的任何列时,它会将此列添加到 dgbill。 我希望它是清楚的。

    `private void dgMenu_CellContentClick(object sender, DataGridViewCellEventArgs e)

{
        DataTable dt;
        if (e.RowIndex > -1)
        {
            if (k < 4)
            {
                DataGridViewRow rw = this.dgMenu.Rows[e.RowIndex];
                string t = rw.Cells[1].Value.ToString();
                if (k == 1)
                {
                    dt = f.SelectFoodType(t);
                }
                else if (k == 2)
                {
                    dt = a.SelectAdditionType(t);
                }
                else if (k == 3)
                {
                    dt = d.SelectDrinkType(t);
                }
                else
                {
                    MessageBox.Show("select Food, Drink or Addition");
                    return;
                }
                dgMenu.DataSource = null;
                dgMenu.DataSource = dt;
                dgMenu.Columns[0].Visible = dgMenu.Columns[2].Visible = false;
                dgMenu.Columns[1].HeaderText = t;
                dgMenu.Columns[3].HeaderText = "Price";
                k = 100;
            }
            else
            {
                DataGridViewRow rw = this.dgMenu.Rows[e.RowIndex];
                string productname = rw.Cells[1].Value.ToString();
                string price = rw.Cells[3].Value.ToString();
                string input = Microsoft.VisualBasic.Interaction.InputBox("Enter Quantity: ", "Quantity", "1", -1, -1);
                int q;
                bool x = int.TryParse(input, out q);
                if (x)
                {
                    EnBill b = new EnBill(UserId);
                    if (IsFormOpen(typeof(EnBill)))
                    {
                        /* in this space I want to add this columns to another DataGridView in another Form*/
                    }
                }
                else
                {
                    MessageBox.Show("No Input or Valid Input");
                }
            }
        }
    }`

最佳答案

您可以使用Row.DataBoundItem来获取绑定(bind)到行的数据。然后可以将该数据设置为子窗体中的 DataGridView。有一个类似的问题已经得到回答。

How to filter / selectively copy values from one DataGridView to another DataGridView

添加了工作代码...

public class Customer
{
    string name;
    int age;

    public Customer(string thename, int theage)
    {
        name = thename;
        age = theage;
    }

    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    public int Age
    {
        get { return age; }
        set { age = value; }
    }

}
public class ChildForm : Form
{
    DataGridView dataGridView1 = new DataGridView();
    public ChildForm()
    {
        this.ClientSize = new System.Drawing.Size(284, 262);
        this.Controls.Add(dataGridView1);
        dataGridView1.Dock = DockStyle.Fill;
    }

    public void AddData(List<Customer> theData)
    {
        dataGridView1.AutoGenerateColumns = true;
        dataGridView1.DataSource = theData;
    }
}
public class ParentForm : Form
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new ParentForm());
    }


    DataGridView dataGridView1 = new DataGridView();
    Button button1 = new Button();
    ChildForm childForm = new ChildForm();

    public ParentForm()
    {
        this.ClientSize = new System.Drawing.Size(284, 262);
        this.Controls.Add(dataGridView1);
        this.Controls.Add(button1);
        this.Load += new EventHandler(ParentForm_Load);
        button1.Click += new EventHandler(button1_Click);
        button1.Dock = DockStyle.Top;
        button1.Text = "CopyToChild";
        dataGridView1.Dock = DockStyle.Fill;
    }

    void button1_Click(object sender, EventArgs e)
    {
        List<Customer> customers = new List<Customer>();
        foreach (DataGridViewRow row in this.dataGridView1.SelectedRows)
        {
            Customer customer = row.DataBoundItem as Customer;
            if (customer != null)
            {
                customers.Add(customer);
            }
        }
        childForm.AddData(customers);

    }

    private void ParentForm_Load(object sender, EventArgs e)
    {

        System.Collections.ArrayList customers = new System.Collections.ArrayList();

        customers.Add(new Customer("Thor", 120));
        customers.Add(new Customer("Loki", 110));
        dataGridView1.AutoGenerateColumns = true;
        dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        dataGridView1.DataSource = customers;
        childForm.Show();
    }
}

关于c# - 如何从父表单中的另一个 datagridview 更新子表单的 datagridview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21177026/

相关文章:

mysql - mysql中具有相同主键的不同表的最大日期

sql - postgres 和 h2 db 中名称中的 # 字符问题

c# - 用户登录后缓存用户信息

c# - 将字典转换为匿名对象

c# - 无法将参数值从 DropDownList 转换为字符串

PHP 时间戳自表连接

mysql - 内连接 值同表

c# - LogManager.configuration 为空

c# - ASP.NEt - 在发送给客户端之前编辑 Html

mysql - 在 MVC2 中使用 MySQL 的成员资格提供程序