473,480 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Set Combobox SelectedValue from Code Behind In WPF

33 New Member
I have a combobox like
Expand|Select|Wrap|Line Numbers
  1. <ComboBox Height="23" HorizontalAlignment="Left" Margin="88,13,0,0" Name="cmbYears" VerticalAlignment="Top" Width="112" SelectionChanged="cmbYears_SelectionChanged" Canvas.Left="13" Canvas.Top="0">
  2.     <ComboBox.Items>
  3.     <ComboBoxItem Tag="2010" Content="2010" />
  4.     <ComboBoxItem Tag="2011" Content="2011" />
  5.     <ComboBoxItem Tag="2012" Content="2012" />
  6.     <ComboBoxItem Tag="2013" Content="2013" />
  7.     <ComboBoxItem Tag="2014" Content="2014" />
  8.     <ComboBoxItem Tag="2015" Content="2015" />
  9.     <ComboBoxItem Tag="2016" Content="2016" />
  10.     <ComboBoxItem Tag="2017" Content="2017" />
  11.     <ComboBoxItem Tag="2018" Content="2018" />
  12.     <ComboBoxItem Tag="2019" Content="2019" />
  13.     <ComboBoxItem Tag="2020" Content="2020" />
  14.     </ComboBox.Items>
  15. </ComboBox>
  16.  
Now say after a button click i want to change the value of the combobox to current year + 1, like
Expand|Select|Wrap|Line Numbers
  1. cmbYears.SelectedValue = (DateTime.Today.Year + 1).ToString() // But Its Not Working
  2.  
  3. cmbYears.SelectedItem = (DateTime.Today.Year + 1).ToString() // And Its also Not Working
  4.  
  5.  
The code runs proprtly without error, but it doesn't reflect on the screen... nothing got selected in the combobox and accordingly OnSelectionChanged event don't fire for the combobox :(
So how can i set the value?
Please Help :) Thanks...
Sep 6 '10 #1
1 24432
sanndeb
33 New Member
Ok I Got It Done...

Expand|Select|Wrap|Line Numbers
  1. foreach (var cmbi in
  2.                 cmbYears.Items.Cast<ComboBoxItem>().Where(cmbi => (string) cmbi.Tag == Datetime.Today.AddYears(1).Year.ToString()))
  3.                 cmbi.IsSelected = true;
  4.  
or by
Expand|Select|Wrap|Line Numbers
  1. cmbYears.Items.Cast<ComboBoxItem>().Where(cmbi => (string) cmbi.Tag == Datetime.Today.AddYears(1).Year.ToString()).Select(a => a).Single().IsSelected = true;
  2.  
Sep 6 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
380
by: amber | last post by:
Hello, I have a combobox populated by a dataset filled with data from a SQL view. In the view I have combined two fields to create one. I can't set my combobox.selectedvalue to this value. Any...
7
1902
by: Troy Whitlow | last post by:
I am developing a web application using aspx web forms and would like to send message boxes to the client browser when certain conditions and checks are performed. ...
5
36822
by: Richard Dixson | last post by:
I created a new C# web application. Basically all I am trying to do is create a table that consists of a few rows with two columns in each. And then to set those columns to text values from my...
171
7577
by: tshad | last post by:
I am just trying to decide whether to split my code and uses code behind. I did it with one of my pages and found it was quite a bit of trouble. I know that most people (and books and articles)...
15
26885
by: Swetha | last post by:
Hello I have a DropDownList that I am populating using the following SqlDataSource: <asp:DropDownList ID="parentIDDropDownList" runat="server" DataSourceID="SqlDataSource3"...
2
4803
by: rn5a | last post by:
Assume that a user control (MyUC.ascx) encapsulates 2 TextBoxes with the IDs 'txt1' & 'txt2' respectively. To use this user control in an ASPX page, the following Register directive will be...
0
1141
by: sanalsuryakalady | last post by:
Hello All, I have 2 combo box in ASP.NET 1.1. When ever I change the value in one combo the other combo will be loaded with the new values using XMLHTTP.But when I post back the screen I lose the...
3
2352
by: RPhlb | last post by:
This is my first post, so excuse me if I don't get this right the first time. I have an issue where when I use DropDownList I only get the first, or "SelectedValue" back when I update a GridView...
1
7221
by: =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= | last post by:
Hi, I generally work on web apps, but I am dealing with a Winform right now and may be missing something really basic. I have a combobox and I would like to know what value has been selected....
0
6903
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7027
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7071
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6726
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5318
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4468
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2987
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
557
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
170
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.