Bind two datagridview's using bindingsource

sanchita goel 0 Reputation points
2023-11-20T03:40:32.5033333+00:00

I have a datagridview1 having two column name and quantity . it is showing some results . i also have another datagridview2 and a textbox1 , i want that the text that i entered in the textbox and press enter, that text should be added to datagridview2's column1 and datagridview1 column1 and the corresponding column2 (quanity) should show 0 . i tried to run some code but it didn't work

 public partial class Form1 : Form
    {
        private BindingList<StockItem> stockItems;
        private BindingSource bindingSource1;

        public Form1()
        {
            InitializeComponent();
            InitializeData();
        }

        private void InitializeData()
        {
            // Create the BindingList to hold stock items
            stockItems = new BindingList<StockItem>();

            // Set the DataSource of the BindingSource to the BindingList
            bindingSource1 = new BindingSource(stockItems);

            // Assign the BindingSource as the DataSource for both DataGridViews
            dataGridView1.DataSource = bindingSource1;
            dataGridView2.DataSource = bindingSource1;

            
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                string enteredText = textBox1.Text;

                // Create a new stock item with the entered text and quantity 0
                StockItem newItem = new StockItem(enteredText, 0);

                // Add the new stock item to the BindingList
                stockItems.Add(newItem);

               
            }
        }
    }

    public class StockItem
    {
        public string Name { get; set; }
        public int Quantity { get; set; }
}
Developer technologies Windows Forms
Developer technologies C#
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2023-11-20T06:50:09.76+00:00

    Hi @sanchita goel , Welcome to Microsoft Q&A,

    Your problem lies in the conversion use of BindingSource and BindingList.

    Refer to their official documentationBindingSource BindingList.

    using System.ComponentModel;
    using System.Windows.Forms;
    
    namespace xxx
    {
        public partial class Form1 : Form
        {
            private BindingList<StockItem> stockItems;
            private BindingSource bindingSource1;
            public Form1()
            {
                InitializeComponent();
                InitializeData();
            }
            private void InitializeData()
            {
                // Create the BindingList to hold stock items
                stockItems = new BindingList<StockItem>();
    
                // Set the DataSource of the BindingSource to the BindingList
                bindingSource1 = new BindingSource { DataSource = stockItems };
    
                // Assign the BindingSource as the DataSource for both DataGridViews
                dataGridView1.DataSource = bindingSource1;
                dataGridView2.DataSource = bindingSource1;
            }
    
            private void textBox1_KeyDown(object sender, KeyEventArgs e)
            {
                if (e.KeyCode == Keys.Enter)
                {
                    string enteredText = textBox1.Text;
    
                    // Create a new stock item with the entered text and quantity 0
                    StockItem newItem = new StockItem(enteredText, 0);
    
                    // Add the new stock item to the BindingList
                    stockItems.Add(newItem);
    
                    // Clear the TextBox after adding the item
                    textBox1.Clear();
                }
            }
        }
        public class StockItem
        {
            public string Name { get; set; }
            public int Quantity { get; set; }
    
            public StockItem(string name, int quantity)
            {
                Name = name;
                Quantity = quantity;
            }
        }
    }
    
    

    User's image

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.