Click here to Skip to main content
15,913,709 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi All,

My question seems simple but am having hard time finding the Answer.
How can I assign controls of the DataRepeater to database table columnID's?

I have a data repeater with 3 controls on it, Lable, and 2 Textboxs. How can i assign each control to to the column in the datatable


SqlCommand cmd = new SqlCommand("Select * from surveyQuestions", con);
           SqlDataAdapter adapt = new SqlDataAdapter(cmd);
           DataTable questions = new DataTable();
           adapt.Fill(questions);
           dataRepeater1.DataSource = questions;

???

Am Using Windows not Web.

Please help me.

Thanks in Advance.
Posted
Updated 5-Jun-11 23:25pm
v3

http://blogs.msdn.com/b/vsdata/archive/2009/08/12/datarepeater-control-for-windows-forms.aspx

Hope this link helps you.
 
Share this answer
 
Msdn docs only tell you how to databind using the designer. I too had a bit of a hard time learning this simple thing a few years earlier :)
You're manually loading data & binding - here's an example of manually binding to a textbox placed within a repeater
Assuming TextBox name is TextBox1, DataTable's name is questions, Repeater's name is DataRepeater1 & columns in your table like "Column1", "Column2" etc
TextBox1.DataBindings.Add("Text", questions, "Column1");
DataRepeater.DataSource=questions;

"Text" is the property of the control you're binding to - you can bind to other properties like "Tag", "Visible" etc. Hope you get the idea.
Here's another checkbox binding example
CheckBox1.DataBindings.Add("Checked",questions","Column2");


Add a repeater & a textbox within it & run this code to test if you want
DataTable dt = new DataTable()
dt.Columns.Add("c1");
dt.Rows.Add("1");
dt.Rows.Add("2");
dt.Rows.Add("3");
dt.Rows.Add("4");
dt.Rows.Add("5");
TextBox1.DataBindings.Add("Text", dt, "c1");
DataRepeater1.DataSource = dt;
 
Share this answer
 
Comments
engnoshi 6-Jun-11 7:14am    
Thanks a lot, I tried it and gave me a weird error though :
This causes two bindings in the collection to bind to the same property.
Parameter name: binding

Note that i have this code in the page load and that my database that am testing with has two rows only.
Member 10619512 7-Jul-14 2:47am    
mmbbvm
engnoshi 6-Jun-11 7:15am    
private void NewSurvey_Load(object sender, EventArgs e)
{

SqlCommand cmd = new SqlCommand("Select * from surveyQuestions", con);
SqlDataAdapter adapt = new SqlDataAdapter(cmd);
DataTable questions = new DataTable();
adapt.Fill(questions);


QIDLbl.DataBindings.Add("Text", questions, "QID");

dataRepeater1.DataSource = questions;

}
engnoshi 6-Jun-11 7:20am    
Figured it out, that was because i bound it before with the designer and now manually, removed the designer and it works great.
strogg 6-Jun-11 10:20am    
Ah, good. Glad you could work it out

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900