how can I pass value from selected field (LINQ) to textbox in winforms?
If single fields, I just do like this
var result = from row in dtValueBranch.AsEnumerable()
where row.Field<int>("branchID") == idBranch
select row.Field<string>("branchName");
StringBuilder sb = new StringBuilder();
foreach (string s in result)
{
sb.Append(s + Environment.NewLine);
}
tbBranch.Text = sb.ToString();
So this is the code LINQ to many fields
var result = from row in dtValueBranch.AsEnumerable()
where row.Field<int>("branchID") == idBranch
select new
{
BranchName = row["branchName"].ToString(),
branchTel = row["branchTel1"].ToString(),
// And many more fields
};
How can I to implement each fields to each textbox?
Keeping in mind that your result query may have multiple rows, your question appears to be focused on how to access the fields of one row. You can do that like this:
The
select newsyntax in your query creates an anonymous type in the result enumeration. You can’t refer to that type by name, but you can create local variables (like row) if you use thevardeclaration style and let the compiler do type inferencing.If you want to cram all the values of one field across all rows into into one textbox (as in your first code sample), you can do something like this:
However, if you’re wanting to display multiple rows of multiple fields of data, you might want to look at using a data grid control instead of a herd of textboxes. You’ll save yourself a lot of effort, and your users will probably thank you too.