Answered by:
Textbox format

Question
-
I want to format my text boxes as my data table fields. How can I do that, do I have to bind all text boxes to table fields?
- Moved by CoolDadTx Tuesday, March 15, 2011 1:40 PM (assuming) WinForm related (From:Visual C# General)
Tuesday, March 15, 2011 12:04 PM
Answers
-
Hi chamW,
I have wrote a sample for you. This sample shows that you can use Binding to bind the TextBox! After you change the Text of the textBox1, its datasource will be changed at the same time.
private DataTable dt_test; private void Form1_Load(object sender, EventArgs e) { dt_test = new DataTable(); dt_test.Columns.Add("id", typeof(Int32)); dt_test.Columns.Add("TextContent", typeof(string)); DataRow dr = dt_test.NewRow(); dr[0] = 1; dr[1] = "It's a test!"; dt_test.Rows.Add(dr); dt_test.AcceptChanges(); Binding b = new Binding("Text", dt_test, "TextContent"); this.textBox1.DataBindings.Add(b); } private void button1_Click(object sender, EventArgs e) { MessageBox.Show(dt_test.Rows[0][1].ToString()); }
Here is the Binding Class on MSDN Library:
http://msdn.microsoft.com/en-us/library/system.windows.forms.binding.aspxIf you have any questions, please feel free to let us know.
Best Regards
Neddy Ren [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- Marked as answer by DevNC Friday, March 18, 2011 7:51 AM
Thursday, March 17, 2011 3:07 AM
All replies
-
Yes. Bind each textbox to a specific column of the table.
- Proposed as answer by Mr.Mubi Tuesday, March 15, 2011 3:28 PM
Tuesday, March 15, 2011 1:07 PM -
Hi chamW,
I have wrote a sample for you. This sample shows that you can use Binding to bind the TextBox! After you change the Text of the textBox1, its datasource will be changed at the same time.
private DataTable dt_test; private void Form1_Load(object sender, EventArgs e) { dt_test = new DataTable(); dt_test.Columns.Add("id", typeof(Int32)); dt_test.Columns.Add("TextContent", typeof(string)); DataRow dr = dt_test.NewRow(); dr[0] = 1; dr[1] = "It's a test!"; dt_test.Rows.Add(dr); dt_test.AcceptChanges(); Binding b = new Binding("Text", dt_test, "TextContent"); this.textBox1.DataBindings.Add(b); } private void button1_Click(object sender, EventArgs e) { MessageBox.Show(dt_test.Rows[0][1].ToString()); }
Here is the Binding Class on MSDN Library:
http://msdn.microsoft.com/en-us/library/system.windows.forms.binding.aspxIf you have any questions, please feel free to let us know.
Best Regards
Neddy Ren [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- Marked as answer by DevNC Friday, March 18, 2011 7:51 AM
Thursday, March 17, 2011 3:07 AM