如何将选定的rowscells从datagridview以另一种形式获取到绑定的datagridview? [英] How to get selected rowscells from datagridview to a binded datagridview in another form?

查看:49
本文介绍了如何将选定的rowscells从datagridview以另一种形式获取到绑定的datagridview?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须表单...第一个是带有datagridview(Dgv_transactions)的form_Items_Transaction,第二个是带有datagridview(Dgv_Search)的Form_Search_Items ...我已经用datatable填充dgv_transactions.so我想从dgv_transaction填充第7列dgv_Search中的第1列包含事件Dgv_Transaction_KeyDown。但问题是,当我关闭Form_Search_Items时,Dgv_Search中的列[7]没有变化。



可以任何一个帮助PLZ?



我的尝试:



i have to forms ... the first one is form_Items_Transaction with datagridview (Dgv_transactions) and the second one is Form_Search_Items with datagridview(Dgv_Search)... i have fill dgv_transactions with datatable.so i want to fill column 7 from dgv_transaction with column 1 in dgv_Search with the event Dgv_Transaction_KeyDown.but the problem is that when i close Form_Search_Items there is no change in column[7] in Dgv_Search.

can any one help plz?

What I have tried:

private void Dgv_Transaction_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (Dgv_Transaction.CurrentRow.Cells[7].Selected == true)
                {
                    if (e.KeyCode == Keys.Escape)
                    {
                        string col1;
                        string col2;
                       
                        
                        DataTable dtcust = Class_Main_Units.StoredProcedure_Search_Items();

                        Form_Search cust = new Form_Search_Units();
                        cust.Dgv_Search.DataSource = dtcust;
                        
                        cust.ShowDialog();

                        col1 = cust.Dgv_Search.CurrentRow.Cells[0].Value.ToString();
                        col2 = cust.Dgv_Search.CurrentRow.Cells[1].Value.ToString();
                        

                        Dgv_Transaction.CurrentRow.Cells[7].Value = col2;
                        Dgv_Transaction.CurrentRow.Cells[6].Value = col1;

                    }
                }
            }
            catch
            {

            }
            
        }

推荐答案

你可以把 DataTable 静态类中的$ c>和 BindingSource ,然后您可以从所有表单访问它们。



Program.cs

You could put the DataTable and BindingSource in a Static class, then you can access them from all forms.

Program.cs
using System;
using System.Windows.Forms;
using System.ComponentModel;

namespace TestForm1
{
    static class Program
    {
        // for Form10 DataGridView.
        public static BindingList<MyClass> masterBindingList;

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            // Test data grid with BindingList.
            Application.Run(new Form10());
        }

        /// <summary>
        /// For Form10:
        /// Simple class with one string.
        /// </summary>
        public class MyClass
        {
            public string Title { get; set; }
        }
    }
}



Form10.cs


Form10.cs

using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace TestForm1
{
    /// <summary>
    /// Shows how to share a BindingList<> between two forms.
    /// </summary>
    public partial class Form10 : Form
    {
        public Form10()
        {
            InitializeComponent();
            this.Init();
        }

        private void Init()
        {
            Program.masterBindingList = new BindingList<Program.MyClass>();
            this.dataGridViewMaster.DataSource = Program.masterBindingList;
        }

        private void ButtonAddRowClick(object sender, EventArgs e)
        {
            var rowIndex = this.dataGridViewMaster.RowCount;
            this.AddRow(rowIndex);
            // this.UpdateRow(rowIndex);
            this.ScrollToRow(rowIndex);
        }

        /// <summary>
        /// Add new row via the BindingList.
        /// </summary>
        private void AddRow(int rowIndex)
        {
            var myRow = new Program.MyClass { Title = "Row " + rowIndex.ToString() };
            Program.masterBindingList.Add(myRow);
        }

        private void ScrollToRow(int rowIndex)
        {
            this.dataGridViewMaster.ClearSelection();
            this.dataGridViewMaster.FirstDisplayedScrollingRowIndex = rowIndex;
            this.dataGridViewMaster.Focus();
        }

        private void buttonShow_Click(object sender, EventArgs e)
        {
            var form10b = new Form10b();
            form10b.ShowDialog();
        }
    }
}



Form10b.cs


Form10b.cs

using System.Windows.Forms;

namespace TestForm1
{
    public partial class Form10b : Form
    {
        public Form10b()
        {
            InitializeComponent();
            dataGridView1.DataSource = Program.masterBindingList;
        }
    }
}


这篇关于如何将选定的rowscells从datagridview以另一种形式获取到绑定的datagridview?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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