changes in datarow update GUI

Hi experts

I'm a bit confused right now, hope you can help me.
I have a bindingsource which points to datatable (quite standard)
several fields of my datarow/databale points to different controls.

How can I acchieve that a change directly in my datarow is reflected in my gui. Do I have to do that myself?

example:
mydatarow["field1"] = "test";

should update the corresponding textfield
LVL 3
ArikaelAsked:
Who is Participating?
 
ToddBeaulieuConnect With a Mentor Commented:
They should, if you have the bindings set up correctly.

I'm not sure what you mean by the datarow points to controls. It should be the other way around.

Can you share the code that creates the control bindings?
0
 
ArikaelAuthor Commented:
hi

yeah, you are right the control points to the datarow respectivly the bindingsource of course.
so, the code
mydatarow["field1"] = "test";

should update the corresponding control?
0
 
ToddBeaulieuCommented:
Right, but only if you've set the binding up correctly. If, for instance, you populated the control with something like "Control.Text = mydatarow["field1"]", you haven't established a binding at all.

That's why I wanted to verify that the binding is correct.
0
 
ArikaelAuthor Commented:
hi

the binding is like:
this.JobFreeTextTextBox.DataBindings.Add("Text", this.MetaBindingSource, "JobFreeText", false, DataSourceUpdateMode.OnPropertyChanged);

I suspect that there is a problem elsewhere in the code
0
 
ToddBeaulieuConnect With a Mentor Commented:
I just worked up a super simple example that shows basic binding does in fact do what you're looking for. I don't think your binding is correct.

This doesn't use a database. Trying to keep it as simple as possible.

Upon startup the textbox correctly shows "Zebra Tech". When I hit the button, the textbox changes to "New Company".
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace C_WinForms_Quick_Tests
{
    public partial class Form1 : Form
    {
        private DataSet ds;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            SetBindings();
        }

        private void SetBindings()
        {
            ds = new DataSet("Customers");

            using (DataTable custTable = new DataTable("custTable"))
            {
                custTable.Columns.Add("CustomerName", typeof(string));

                ds.Tables.Add(custTable);

                custTable.Rows.Add( "Zebra Tech" );

                textBox1.DataBindings.Add("Text", ds, "custTable.CustomerName");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ds.Tables["custTable"].Rows[0]["CustomerName"] = "New Company";
        }

    }
}

Open in new window

0
All Courses

From novice to tech pro — start learning today.