none
Refreshing Grid RRS feed

  • Question

  • I've a refreshing problem. I added a new record to items table:

    items it = new items();
    it.itemName = itemNameTextBox.Text.ToString();
    myent.AddToitems(it);
    myent.SaveChanges();

    It's working. But I want to show a new record in my gridcontrol. How can I refresh the binding? I use this code for binding:

    BindingSource src = new BindingSource(myent.items, null);
    gridControl1.DataSource = src;
    src.DataSource = myent.items;

    If I call this method I lost the other bindings:

    itemNameTextBox.DataBindings.Add("Text", src, "itemName");

    I tried his but not doing anything:

    private void button2_Click(object sender, EventArgs e)
    {
      gridControl1.DataSource = new myent.items();
      gridControl1.RefreshDataSource();
    }

    Tuesday, September 11, 2012 7:49 AM

Answers

  • Hi Akoss,

    Please try this:

       public partial class Form1 : Form
        {
            public Entities context;
            public BindingSource src;
    
            public Form1()
            {
                InitializeComponent();
    
                this.context = new Entities();
                this.src = new BindingSource(context.CompanyProducts, null);
                dataGridView1.DataSource = src;
                src.DataSource = context.CompanyProducts;
    
                textBox1.DataBindings.Add("Text", src, "Name");
            }
            private void button2_Click(object sender, EventArgs e)
            {
                // Clear the collection before adding new Binding objects.
                textBox1.DataBindings.Clear();
    
                using (this.context = new Entities())
                {
                    this.src = new BindingSource(context.CompanyProducts, null);
                    dataGridView1.DataSource = src;
                    src.DataSource = context.CompanyProducts;
    
                    textBox1.DataBindings.Add("Text", src, "Name");
                }
            }
        }
    

    Good day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Ákoss Saturday, September 15, 2012 7:45 PM
    Friday, September 14, 2012 9:43 AM

All replies

  • Hi Akoss,

    Welcome to the MSDN forum.

    As far as I know, if you want to refresh grid, you are required to query your database again to catch all the records. In my opinion, I recommend you write a method which return a list. You can add your linq query and use parameters in this method. Then you can use the returned list as the datasource of grid.

    Good day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, September 12, 2012 7:47 AM
  • I use this 2 lines for filling my grid:

    BindingSource src = new BindingSource(myent.items, null);
    gridControl1.DataSource = src;

    The problem is if I call this 2 lines again after insert, I lost the other bindings.

    itemNameTextBox.DataBindings.Add("Text", src, "itemName");

    Wednesday, September 12, 2012 7:54 AM
  • Hi Akoss,

    Could you please let me know what is the other bindings you mean? For example: BackColor? ForeColor?

    Good day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, September 13, 2012 7:52 AM
  • I bind selected object to a textbox, as I wrote:

    itemNameTextBox.DataBindings.Add("Text", src, "itemName");

    Thursday, September 13, 2012 7:54 AM
  • Hi Akoss,

    Then Text of itemNameTextBox is the itemName. If you repeatly call

    itemNameTextBox.DataBindings.Add("Text", src, "itemName");

    You are required to clear DataBindings. Then the Text property of textbox will be reset.

    Good day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us


    • Edited by Alexander Sun Thursday, September 13, 2012 8:16 AM more information
    Thursday, September 13, 2012 8:11 AM
  • If I call again

    BindingSource src = new BindingSource(myent.items, null);
    gridControl1.DataSource = src;
    src.DataSource = myent.items;
    
    itemNameTextBox.DataBindings.Add("Text", src, "itemName");

    I got error message.

    two bindings in the collection to bind to the same property
    Parameter name: binding


    Thursday, September 13, 2012 8:29 AM
  • If you repeatedly call

    itemNameTextBox.DataBindings.Add("Text", src, "itemName");

    You are required to clear DataBindings when you update the property data binding like this:

    itemNameTextBox.DataBindings.Clear();

    Then the Text property of textbox will be reset.


    Thursday, September 13, 2012 8:44 AM
  • I tried but not working. If I call clear method, not binding the textbox. And not drop error message. The textbox will be empty.
    Thursday, September 13, 2012 8:47 AM
  • Yes, Clear() method just clear the collection of any bindings. After that, you can use your binding statement to bind a new data source.

    Best Regards,


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, September 13, 2012 8:58 AM
  • Yes, I think so too.

    itemNameTextBox.DataBindings.Clear();
    
    myent= new myentEntities();
    gridControl1.DataSource = myent.items;
    src.DataSource = myent.items;
    
    itemNameTextBox.DataBindings.Add("Text", src, "itemName");
    But not working.



    • Edited by Ákoss Friday, September 14, 2012 3:38 AM
    Thursday, September 13, 2012 9:26 AM
  • Hi Akoss,

    It’s strange. Could you please let me know what the text displayed?

    Good day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us

    Friday, September 14, 2012 8:39 AM
  • Nothing. The textbox will be empty.
    Friday, September 14, 2012 8:42 AM
  • Hi Akoss,

    Your code work normally on my side. Please check if the itemName of items is null or not. On my side, textbox will display the itemname of the first record in database.

    Good day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us

    Friday, September 14, 2012 9:00 AM
  • This is all of my code:

    public partial class frmItemsList : DevExpress.XtraEditors.XtraForm { myentEntities myent = new myentEntities(); BindingSource src = new BindingSource(); public frmItemsList() { InitializeComponent(); BindingSource src = new BindingSource(myent.items, null); gridControl1.DataSource = src; src.DataSource = myent.items; itemNameTextBox.DataBindings.Add("Text", src, "itemName"); } private void button1_Click(object sender, EventArgs e) { myent.SaveChanges(); } private void button2_Click(object sender, EventArgs e) { itemNameTextBox.DataBindings.Clear(); myent = new myentEntities(); gridControl1.DataSource = myent.items; src.DataSource = myent.items; itemNameTextBox.DataBindings.Add("Text", src, "itemName"); } }

    In first run the textbox binding working well. If I insert a new row and clicking button2 to refreshing the binding go bad. I don't understand.
    Friday, September 14, 2012 9:19 AM
  • Hi Akoss,

    Please try this:

       public partial class Form1 : Form
        {
            public Entities context;
            public BindingSource src;
    
            public Form1()
            {
                InitializeComponent();
    
                this.context = new Entities();
                this.src = new BindingSource(context.CompanyProducts, null);
                dataGridView1.DataSource = src;
                src.DataSource = context.CompanyProducts;
    
                textBox1.DataBindings.Add("Text", src, "Name");
            }
            private void button2_Click(object sender, EventArgs e)
            {
                // Clear the collection before adding new Binding objects.
                textBox1.DataBindings.Clear();
    
                using (this.context = new Entities())
                {
                    this.src = new BindingSource(context.CompanyProducts, null);
                    dataGridView1.DataSource = src;
                    src.DataSource = context.CompanyProducts;
    
                    textBox1.DataBindings.Add("Text", src, "Name");
                }
            }
        }
    

    Good day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Ákoss Saturday, September 15, 2012 7:45 PM
    Friday, September 14, 2012 9:43 AM
  • It's working fine. What's the main difference the  "using..." line?
    Friday, September 14, 2012 11:16 AM
  • Hi Akoss,

    The using statement calls the Dispose method on the object in the correct way, and (when you use it as shown earlier) it also causes the object itself to go out of scope as soon as Dispose is called. Within the using block, the object is read-only and cannot be modified or reassigned. Please check this page: http://msdn.microsoft.com/en-us/library/yh598w02.aspx

    Have a nice day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us

    Monday, September 17, 2012 8:40 AM
  • Thank you!


    • Edited by Ákoss Monday, September 17, 2012 12:50 PM
    Monday, September 17, 2012 8:48 AM