How to bind using DataTable, BindingSource and TextBoxes

RDMAT
RDMAT used Ask the Experts™
on
//I have a DataTable with columns  
  DataTable JobsDataTable = new DataTable();
  JobsDataTable.Columns.Add("Job No.", Type.GetType("System.Int32"));   // [0]
 
//The DataTable is then populated with data  

//I created a  BindingSource object  and bind to the DataTable  
 BindingSource    JobsBindingSource = new BindingSource() ;          
JobsBindingSource.DataSource = JobsDataTable;
//I then want to binding to a TextBox control  using the DataTable columns' name but nothing happen
jobNoTextBox.DataBindings.Add(new Binding("Text", JobsBindingSource, "Job No."));
//What I am doing wrong?
//Any idea?
//Thanks for your help
Rob
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
anarki_jimbelSenior Developer

Commented:
Shouldn't be the last parameter be a column name? "Job No." isn't a column name definitely, rather column text... Try to change

Author

Commented:
Saga could you be more clear. Are you talking about the Datable or the textbox control?
Senior Developer
Commented:
I'm talking about the line:

new Binding("Text", JobsBindingSource, "Job No.")

If I'm not wrong
First parameter - name of a control property to be bind to
Second - binding source (table in our case)
Third parameter - property or column name in the binding source. In your case it's "Job No.". But column cannot have such a name - it should be one word, like "jobnumber" or "job_number" etc. Check what the column is called and change:

I'm talking about the line:

new Binding("Text", JobsBindingSource, "jobnumber")
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

anarki_jimbelSenior Developer

Commented:

Author

Commented:
Ok I'll try and let you know

Thanks

Author

Commented:
Yes, you are right, it should be "JobNo" instead of "Job No.", but "Job No." does work if you want to bind it to a DataGridView control.

Thank you very much for your help Anarki
anarki_jimbelSenior Developer

Commented:
Nice if it helps! :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial