set combobox text from textbox - c#

I have user submitted content that is loaded into c# winform in our office for processing before officially added to database. The user can submit a 'Referrer' as two text fields-first and last name. In the office I want to have a combobox will all existing referrers loaded in, then the first couple letters of the name to advance the combobox down to the area it needs to be at. I want to do something like this, taking the first two letters of the name and use that to initialize the combobox.
if (txtrefFirstName.TextLength > 2)
{
string firstStart = "" + txtrefFirstName.Text[0] + txtrefFirstName.Text[1];
firstStart = firstStart.ToUpper();
ddlReferring.SelectedText.StartsWith(firstStart);
}
else
ddlReferring.Text = "";
Any ideas or suggestions to get this to work?
Thanks
David K.

You could write something like this...
foreach (string item in ddlReferring.Items)
{
if (item.StartsWith(firstStart))
{
ddlReferring.SelectedText = item;
break;
}
}

Assuming the ddl's datasource is a List of String objects, you should be able to do some comparison on the datasource itself. I tend to use Linq for things like this but it isn't strictly necessary, just shorter.
if (txtrefFirstName.TextLength > 2)
{
string firstStart = txtrefFirstName.Text.Substring(0,2).ToUpper();
string Selection = ddlReferring.DataSource.Where(a=>a.StartsWith(firstStart)).FirstOrDefault();
ddlReferring.SelectedText = Selection ?? "";
}
else
ddlReferring.Text = "";
The selection line can also come from the items collection directly
string Selection = ddlReferring.Items.OfType<string>().Where(a=>a.StartsWith(firstStart)).FirstOrDefault();
Or if you REALLY dont want to use Linq...
string Selection = "";
foreach (object item in ddlReferring.Items)
if (item.ToString().StartsWith(firstStart))
{
Selection = item.ToString();
break;
}
Similar methods can be used even if the ddl's data is not a list of strings, just make sure to cast the items appropriately and compare the correct values.

Related

C# Updating multiple text boxes with similar names

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!";
}

Selecting Multiple ListBox items from GridView column

I have a databound Listbox with Multiselect enabled. On page load, I feed the information from a GridView column and select all the options that match, using this code:
string[] separators = { "<br />" };
String Departments = Session["ProjDept"].ToString();
string[] splitDepartments = Departments.Split(separators, StringSplitOptions.RemoveEmptyEntries);
foreach (var dept in splitDepartments)
{
listDepartment.SelectedIndex = listDepartment.Items.IndexOf(listDepartment.Items.FindByText(dept));
}
However, I am running into a strange issue: when there is only one department in the GridView column, the option in the listbox gets properly selected, but when there's multiple departments only the LAST department gets selected.
I've ran System.Diagnostics.Debug.Print(dept) within my foreach to ensure that all the values are getting passed and they all appear in the STDOUT, but the listbox still won't cooperate.
Any ideas as to how I can fix this -- or alternatively, what other code could I use to achieve the same results?
Thank you!
The SelectedIndex property only allows one value at a time, so you're resetting it with each iteration. That's why only the last one is being selected. You need to access the "Selected" property from the ListItem itself.
Without trying it myself, it should look something like:
foreach (var dept in splitDepartments)
{
int index = listDepartment.Items.IndexOf(listDepartment.Items.FindByText(dept));
listDepartment.Items[index].Selected = true;
}
As long as you do have SelectionMode="Multiple" - that code should work.

Search Listview subitem with string

I have a Listview that contains all the guests from the database
public FormGuestManagement()
{
InitializeComponent();
listViewGuests.View = View.Details;
listViewGuests.GridLines = true;
listViewGuests.Scrollable = true;
listViewGuests.FullRowSelect = true;
listViewGuests.HideSelection = false;
var guests = Repository.GetAllGuests();
foreach (var guest in guests)
{
ListViewItem lvData = new ListViewItem(guest.AccountID.ToString());
lvData.SubItems.Add(guest.Username);
lvData.SubItems.Add(guest.Email);
lvData.SubItems.Add(guest.FirstName);
lvData.SubItems.Add(guest.LastName);
lvData.SubItems.Add(guest.TelephoneNumber);
lvData.SubItems.Add(guest.AddressLine1);
lvData.SubItems.Add(guest.AddressLine2);
lvData.SubItems.Add(guest.City);
lvData.SubItems.Add(guest.State);
lvData.SubItems.Add(guest.Postcode);
lvData.SubItems.Add(guest.Country);
listViewGuests.Items.Add(lvData);
}
}
How would it be possible to search one single column (in this case guest.Firstname) with a string and only show the values that match the string (hide the others) in the listview?
Thank you for your time!
The ListView control does not provide the functionality you want. In order to simulate what you describe, you would have to rebuild the desired list every time a filter condition changes.
Or you could do as one commenter suggested and use something more robust, such as a DataGridView. This control provides true Row/Column behaviors and takes a DataSource that can be bound.
It's possible with LINQ and should be very easy:
using System.Linq;
var result = guests.Where(guest => guest.FirstName.Equals("Test"));

Dropdownlist ID change name

The headline is not good. I don't know how anything better to write in the headline. But here it comes.
I am making a soccerfield with all the different positions. On each position I have a dropdownlist that shows the players in the team.
I made parts of the dropdownlists and came to realised that I was doing it wrong.
try
{
int pos = list[i].positionsID;
if (pos == 1)
{
ddlGoal.Visible = true;
lblGoal.Visible = true;
ddlGoal.DataSource = list1;
ddlGoal.DataValueField = "id";
ddlGoal.DataTextField = "name";
ddlGoal.DataBind();
}
} catch ...
It should only show the position of there are any players on that position. It all works fine... but i have 28 positions, and there for 28 of the above try/catch things.
I want to scale it down, by only making one.
If i let all the dropdownlists have the same name as the postions saved on my database I should be able to make just one. So i have tried that:
try
{
int pos = list[i].positionsID;
if (pos != 0)
{
string ddl = "ddl" + list3[i].positionsNavn.ToString();
ddl.Visible = true;
lblGoal.Visible = true;
ddlGoal.DataSource = list1;
ddlGoal.DataValueField = "id";
ddlGoal.DataTextField = "name";
ddlGoal.DataBind();
}
} catch...
It doesn't work. How do i get hold of the dropdownlist with ID ddlGoalKeeper when i am creating the ID as:
string ddl = "ddl" + list3[i].positionsNavn.ToString();
Any ideas or help??
I think this is what you need. Given the ID of any control you can use
FindControl
http://msdn.microsoft.com/en-us/library/system.web.ui.control.findcontrol.aspx
to get any control in the control tree. You wil need to use it on the naming container that contains the control i.e.
Panel.FindControl(controlID)
or use the recursive version wriiten by a certain Jeff Atwood
http://www.codinghorror.com/blog/2005/06/recursive-pagefindcontrol.html
Alternatively - construct a Dictionary collection will all your dropdowns mapped to IDs and get them for this. Could be more efficient with more setup

Populating RadiobuttonList dynamically

I have quite a few radiobuttonLists in my ASP.net webform. I am dynamically binding them using the method shown below:
public static void PopulateRadioButtonList(DataTable currentDt, RadioButtonList currentRadioButtonList, string strTxtField, string txtValueField,
string txtDisplay)
{
currentRadioButtonList.Items.Clear();
ListItem item = new ListItem();
currentRadioButtonList.Items.Add(item);
if (currentDt.Rows.Count > 0)
{
currentRadioButtonList.DataSource = currentDt;
currentRadioButtonList.DataTextField = strTxtField;
currentRadioButtonList.DataValueField = txtValueField;
currentRadioButtonList.DataBind();
}
else
{
currentRadioButtonList.Items.Clear();
}
}
Now, I want to Display only the first Letter of the DataTextField for the RadioButton Item Text.
For example if the Value is Good I just want to Display G. If it Fair I want to display F.
How do I do this in C#
Thanks
You can't do what you want when you do the binding, so you have 2 options:
Modify the data you get from the table, before you do the binding.
After binding, go through each item and modify its Text field.
So, it you want to display "only the first Letter of the DataTextField for the RadioButton Item Text", you can do:
currentRadioButtonList.DataSource = currentDt;
currentRadioButtonList.DataTextField = strTxtField;
currentRadioButtonList.DataValueField = txtValueField;
currentRadioButtonList.DataBind();
foreach (ListItem item in currentRadioButtonList.Items)
item.Text = item.Text.Substring(0, 1);
If I misunderstood you and you want to display the first letter of the Value field, you can replace the last two lines with:
foreach (ListItem item in currentRadioButtonList.Items)
item.Text = item.Value.Substring(0, 1);
You could add a property to the type that is being bound (the one that contains Good, Fair, etc.) and bind to this property. If you will always be using the first letter, you could make it like so (adding in null checks, of course):
public string MyVar { get; set; }
public string MyVarFirstChar
{
get { return MyVar.Substring(0, 2); }
}

Categories

Resources