in c# winform entity framework
im serching like this
var search = dataBase.tbl_Employee.First(i=>i.code == 1);
i have many column in database and many textBox in Form
how to fill textbox.text like this
foreach(textbox tb in this.control.oftype<textbox>())
{
tb.text = search.tb.name;
}
You can set name of textboxes equals to property name of model:
and like this set Text property:
foreach(var tb in this.Controls.OfType<Textbox>())
{
tb.Text=
search.GetType().GetProperty(tb.Name).GetValue(search,null).ToString();
}
Related
I have multiple textbox names such as R1TotalCost, R2TotalCost, R3TotalCost, all the way up to R25TotalCost. Is there anyway to edit the text values, and or text colours to them all using a code simular to this
for (i=1; i <=25, i++) {
string TextBoxName ="R" + i + "TotalCost";
TextBoxName.text = "£25";
TextBoxName.Foreground = Brushes.Green;
}
I think the best way to go about this since you seem to want to update them all at once, would be to create an Array or List that contains all of the TextBox elements. Then your can go through them all like below!
foreach (TextBox tb in myTextBoxes) {
tb.Content = "UPDATED CONTENT!";
}
I have 10 text boxes. Like textBox1.Text , textBox2.Text and so on. I need to use textBox2.Text if i=2, textBox2.Text if i =3 and so on.
I did:
string t = "textBox"+i+".Text";
But outcome coming as "textBox1.Text".
How to insert 'i' Value into textBox name instead of 1. And get the outcome as textBox1.Text
So I can use it as name and pass value from textbox to my program.
Based on what I think you are asking, you could store a reference to each TextBox in an array and then use your int to reference it.
TextBox[] boxes = new TextBox[10];
boxes[0] = textBox1;
// ... follow the pattern
boxes[9] = textBox10;
string value = boxes[i-1].Text; // Gets the value of the textBoxi.Text
You can do it like so:
private TextBox GetTB(int i) {
string name = "textBox" + i.ToString();
foreach (var ctrl in Controls) {
var tbox = ctrl as TextBox;
if (tbox?.Name == name) return tbox;
}
return null
}
The easiest way, if you need to access them in C# code this way, would be to maintain an array or List of these textboxes, indexed in order of their numeric value. Then you could simply reference textBoxes[i-1] and get the textbox numbered "i". How you get that array depends on exactly what you're developing. For WinForms, you can use a little Linq on the Form.Controls property:
public static IEnumerable<Control> Flatten(this IEnumerable<Control> controls)
{
var results = new List<Control>();
foreach (var control in controls)
{
results.Add(control);
control.Controls.OfType<Control>().Flatten(results);
}
return results;
}
private static void Flatten(this IEnumerable<Control> controls, List<Control> results)
{
foreach (var control in controls)
{
results.Add(control);
control.Controls.OfType<Control>().Flatten(results);
}
}
...
var textboxes = Form.Controls.Flatten()
.OfType<TextBox>()
.Where(t=>t.Name.StartsWith("textBox"))
.OrderBy(t=>t.Name)
.ToArray();
I am trying to get the Row's particular column from selected item in wpf from DataGrid.
Name of DataGrid is Datagrid_Newsale.
I am getting alert of whole row when it is selected, So i tried mapping its column.
Say if row is-
{ ID = 3, CustomerName = xyz, SaleDate = 05.08.2013 00:00:00, TotalAmount = 10 }
Then it's column CustomerName=xyz is to be shown in textbox.
Getting row-
var copyitem = Datagrid_NewSale.SelectedItem;
if (copyitem == null)
{
MessageBox.Show("Please select values from list");
}
if (copyitem != null)
{
MessageBox.Show(copyitem.ToString());
}
For getting customerName into text box i tried creating a new instance of model-
public class CustomerDetailes
{
public string CustomerName { get; set; }
}
And values from database from Customer Table-
public void viewcustomername()
{
List<CustomerDetailes> ilist = null;
ilist = (from order in db.Customer
select new CustomerDetailes
{
CustomerName= order.CustomerName
}).ToList();
txtCustumer.Text = ilist.ToString();
}
So giving it one more try-
CustomerDetailes copyitem = (CustomerDetailes)Datagrid_NewSale.SelectedItem;
if (copyitem == null)
{
MessageBox.Show("Please select values from list");
}
if (copyitem != null)
{
MessageBox.Show(copyitem.ToString());
}
txtCustomer.text=copyitem.CustomerName; //CustomerName into a textbox
But it is referencing null in copyitem.
How can I get particular column from the whole row.
You will have to bind the ItemsSource of DataGrid to CustomerDetails collection in order to get CustomDetails in SelectedItem.
Create property in your viewmodel (if using MVVM) or in code behind like
List<CustomerDetails> customerDetails = new List<CustomerDetails>();
List<CustomerDetails> MyCollection
{
get
{
return myList;
}
set
{
myList = value;
PropertyChanged(this, new PropertyChangedEventArgs("MyCollection"));
}
}
and in xaml just do.
<DataGrid ItemsSource="{Binding MyCollection}"/>
OR if you are directly filling the Items in the datagrid add instances of CustomerDetails like
dataGrid.Items.Add(new CustomerDetails(){Name = "abc"}, xyz propertis)
Thanks
If you can access the grid from your selection event then following should give your the column
((DataGrid)sender).CurrentCell.Column.Header
and use some mapping for the column name to the property of the object your want to show
I came up with this easy solution.
Mapped datatype of copyitem that was Anonymous in my case. In this case using Dynamic datatype solved my problem.
Due to my data was coming dynamically and then i was trying to map out particular column, so it was not really possible to do it statically because there is not data then.
Using Dynamic Datatype-
dynamic copyitem = dataGrid1.SelectedItem;
Accessing property-
int localId = copyitem.ID;
furthermore for customerName,TotalAmount i did the same.
Linq Query changes-
var query= (from order in db.Customer
where order.ID=localId
select order).ToList();
DataGrid_OpenSale.ItemsSource=query // returning data to another datagrid.
in VB ⠀⠀⠀
Private Sub SCUSTDataGrid_GotFocus(sender As Object, e As RoutedEventArgs) Handles SCUSTDataGrid.GotFocus
Dim og As DataGridCell = e.OriginalSource
Dim ccontent As TextBlock = og.Content
Dim dg As DataGrid
dg = e.Source
Dim selcol As String = dg.CurrentCell.Column.Header.ToString
MessageBox.Show(selcol.ToString + ccontent.Text + " got focus event")
End Sub
I'm trying to update a person's details in the database. Since I'm using textboxes, how would I keep the original value if the textbox is empty?
I'm trying to write it in a short way rather than having a tonne of IF statements.
The code I've written will work but not the way I want, any suggestions on what I could do?
Code (C#):
foreach (Control c in pnlUpdate.Controls)
{
if (c is TextBox)
{
TextBox questionTextBox = c as TextBox;
if (questionTextBox.Text == "")
{
questionTextBox.Text = pat[0].Forename;
questionTextBox.Text = pat[0].Surname;
questionTextBox.Text = pat[0].Street;
questionTextBox.Text = pat[0].Town;
questionTextBox.Text = pat[0].City;
questionTextBox.Text = pat[0].DOB.ToString();
questionTextBox.Text = pat[0].House_number;
questionTextBox.Text = pat[0].Mobile;
}
}
}
Pat = database object
I'm trying to update a person's details in the database. Since I'm using textboxes, how would I keep the original value if the textbox is empty?
Use Server Validation. To make sure that user has entered values for all controls you need to use RequiredFieldValidator. Go through below link:
http://msdn.microsoft.com/en-us/library/5hbw267h(v=vs.80).aspx
Something like below will provide you the help
pat[0].Name = TextBox.Text == "" ? pat[0].Name : TextBox.Text
Sorry for reposting this but I haven't figure out how to do it.
string str = string.Empty;
foreach (ListItem item in this.CheckBoxList1.Items)
{
if (item.Selected)
{
str += item.Value + "<br><br>";
TextBox txt1 = new TextBox();
form1.Controls.Add(txt1);
}
}
lbl1.Text = str;
This brings up a textbox each time I check a value but I want to bring the textbox near each selected value that I retain in the label. So value1 textbox1, value2 textbox2....It's asp.net web application.
Why do you want to use the same label?
U could do:
foreach (ListItem item in this.CheckBoxList1.Items)
{
if (item.Selected)
{
Label label = new Label();
label.Text = item.Value;
TextBox txt1 = new TextBox();
form1.Controls.Add(label);
form1.Controls.Add(txt1);
}
}
you also might have to assign id to the textboxes if you are planning to retrieve user input from them.
But if you only have limited number of fields u could use static fields, and change its visibility as Tim stated
I don't really know what you plan to do but why dont you define a CSS class for both the label object and the textbox object using the same y- but alternate x-coordinates.