问题
I create a usercontrol(usercontrol2) and add textBox on the usercontrol(usercontrol2).
Form Code:
using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString))
{
myDatabaseConnection.Open();
using (SqlCommand SqlCommand = new SqlCommand("Select LastName from Employee", myDatabaseConnection))
using (SqlDataAdapter da = new SqlDataAdapter(SqlCommand))
{
SqlDataReader DR1 = SqlCommand.ExecuteReader();
int y = 0;
while (DR1.Read())
{
y++;
for (int i = 0; i < y; i++)
{
UserControl2 userconrol = new UserControl2();
userconrol.Location = new Point(50, 30 * i);
userconrol.Tag = i;
this.Controls.Add(usercontrol);
}
}
}
}
With the code above, my form displays 20 usercontrols because my table contains 20 records.
Now, How to display each LastName in each usercontrols' textbox?
回答1:
There are many ways to do it, but the simplest is probably to create a string
property on UserControl2
and update the text box in that property's setter. Something like this:
private string lastName;
public string LastName
{
get { return lastName; }
set
{
lastName = value;
lastNameTextBox.Text = value;
}
}
Then in the above code you'd do something like this:
UserControl2 userconrol = new UserControl2();
userconrol.Location = new Point(50, 30 * i);
userconrol.Tag = i;
userconrol.LastName = (string)DR1["LastName"];
this.Controls.Add(usercontrol);
(You'll probably want to do some error checking around that cast, make sure there's a value there and what not.)
As more fields are added, however, this doesn't scale well. You'd have to remember to update this same code anywhere that this user control is added. You can take it a step further by creating a view model for the user control. Given the fields we have so far, that would be something like this:
public class EmployeeViewModel
{
public string LastName { get; set; }
public Point Location { get; set; }
public int Tag { get; set; }
}
You can require this view model in your user control by adding it to the constructor:
public UserControl2(EmployeeViewModel model)
{
this.Location = model.Location;
this.Tag = model.Tag;
this.LastName = model.LastName;
}
So any time you create a new instance of UserControl2
, the compiler forces you to supply it with the data it needs:
this.Controls.Add(
new UserControl2(
new EmployeeViewModel
{
LastName = (string)DR1["LastName"],
Location = new Point(50, 30 * i),
Tag = i
}));
Again, this is all just one of many approaches. The idea is to logically group things into objects and pass around those objects. That is, setting one object is preferred over setting many primitive values. That way the object can internally control its required fields, how it responds to those fields, validation of data, etc. instead of having to manually remember to do it each time.
Perhaps I'm going off on a tangent here. For your needs, as in my first example, you just need to expose from the user control some way of setting the value, and then just set it. How you expose that is up to you and how your objects are logically encapsulated.
来源:https://stackoverflow.com/questions/16901588/show-each-record-in-each-usercontrols-textbox