好的,所以我正在尝试创建一个 wpf,其中屏幕上的文本框数与 DataTable 中匹配特定条件的行数相关。我希望根据表中的数据填充文本框。
我尝试过的 1)
int numContacts = 0;
List<TextBox> list = new List<TextBox>();
IEnumerable<DataRow> selectedContacts = from myRow in
ds.Tables["contacts"].AsEnumerable()
where myRow.Field<int>("company_ID") == 1
select myRow;
try
{
foreach (var item in selectedContacts)
{
list.Add(new TextBox());
// Here Is where I would like to setup the text box
// I'm not quite sure how I would do that though
numContacts++;
}
}
catch {}
基本上我想设置这样的属性:
TextBox[,] tb = new TextBox[numContacts, 3];
var width = txt_name.Width;
var height = txt_name.Height;
Thickness marginName = txt_name.Margin;
for (int i = 0; i < nametb.GetLength(0); i++)
{
tb[i, 0] = new TextBox();
tb[i, 0].HorizontalAlignment = HorizontalAlignment.Left;
tb[i, 0].VerticalAlignment = VerticalAlignment.Top;
tb[i, 0].Name = "name"+i;
tb[i, 0].Width = width;
tb[i, 0].Height = height;
tb[i, 0].Margin = new Thickness(marginName.Left, marginName.Top + (height+3)*(i+1), 0, 0);
grid.Children.Add(tb[i, 0]);
}
那么 Linq 是最好的选择吗?将来我想创建文本框,然后用每一行的信息填充它们,然后如果用户进行更改,最终将它们保存回 DataTable。
这是我的 DataTable 的设置方式:
DataSet ds = new DataSet();
DataTable contactTable = new DataTable("contacts");
contactTable.Columns.Add("company_ID", typeof(int));
contactTable.Columns.Add("name");
contactTable.Columns.Add("title");
contactTable.Columns.Add("email");
contactTable.Rows.Add(1, "Jim Bean", "CEO", "Jbean@acompany.ca");
contactTable.Rows.Add(1, "Tim Brown", "CFO", "tbrown@acompany.ca");
contactTable.Rows.Add(1, "Nancy Smith", "CTO", "nsmith@acompany.ca");
contactTable.Rows.Add(2, "Norman Jackson", "CEO", "njackson@bcompany.ca");
contactTable.Rows.Add(3, "Todd White", "CFO", "twhite@ccompany.ca");
ds.Tables.Add(contactTable);