I am trying to set a combobox.selectedValue to a string which works but when its nullorempty it errors out. I have tried the following code to no avail:
if (string.IsNullOrEmpty(docRelComboBox.SelectedValue.ToString()))
{
document = "other";
}
else
{
document = docRelComboBox.SelectedValue.ToString();
}
The combobox is databound but in theory it could be nullorempty in certain situations and I need to be able to pass the other value at those times. Any help would be great.
You probably need:
if ((docRelComboBox.SelectedValue==null) || string.IsNullOrEmpty(docRelComboBox.SelectedValue.ToString()))
Since SelectedValue itself might be null.
Calling ToString() when the SelectedValue is null is probably causing the error. I would try:
if (docRelComboBox.SelectedValue == null ||
string.IsNullOrEmpty(docRelComboBox.SelectedValue.ToString()))
{
document = "other";
}
else
{
document = docRelComboBox.SelectedValue.ToString();
}
instead.
Related
I am facing strange problem regarding WPF ListBox. I have tried all variations of setting ListBox bindings, but it still the same.
As soon as I change the selection and click on different item other than which is already selected, It jumps back to the previous selected item. On second click , it get selected afterwards which is completely wrong.
I debugged and see that everything is al right. After calling RaisePropertyChange(Property), it jumps to the getter section of property and everything fine there too. It is returning the valid value from there.
But after returning and before displaying again selected value, something bad happens and due to that, it jumps back to the previously selected item ( anchorItem ).
Following is the XAML for this.
<ListBox x:Name="yGradesListBox"
Style="{StaticResource enumListBox}"
SectedValue="{Binding Path=SelectedYieldGrade}"/>
While The binding Property is :
public YieldGrade SelectedYieldGrade
{
get
{
if (_sides[0].YieldGradeState == SelectionState.Selected)
return YieldGradeConverter.Convert(_sides[0].YieldGrade);
if (_sides[1].YieldGradeState == SelectionState.Selected)
return YieldGradeConverter.Convert(_sides[1].YieldGrade);
//this is the condition that meets after RaisePropertyChange and it has to be returned.
if (this.CarcassDto.USDAYieldGrade != YieldGrade.NoGrade)
return this.CarcassDto.USDAYieldGrade;
return YieldGrade.NoGrade;
}
set
{
if (this.CarcassDto.USDAYieldGrade == YieldGrade.NoGrade || this.CarcassDto.USDAYieldGrade != value)
{
this.CarcassDto.USDAYieldGrade = value;
foreach (SideViewModel sideData in _sides.Where(sideData => sideData.HasData))
{
sideData.YieldGradeState = SelectionState.Overwritten;
}
this.CarcassDto.YieldGradeOverrideState = this.GetYieldGradeOverride();
RaisePropertyChanged("SelectedYieldGrade");
_model.OnCarcassDataChanged(this, new CarcassDataChangedEventArgs() { RecalcProgram = true });
}
}
}
and YieldGrade type is Enum defined in the code
public enum YieldGrade
{
[Description("No Grade")]
NoGrade = 0,
[Description("Y1")]
YG1 = 1,
[Description("Y2")]
YG2 = 2,
[Description("Y3")]
YG3 = 3,
[Description("Y4")]
YG4 = 4,
[Description("Y5")]
YG5 = 5
}
Now If I click on any item represented by above enum, the selection would be jumped back to the anchor value( i.e the previously selected ).
I ahve applied all combinitions of properties in markup ( e.g TwoWay, SelectedItem )
Please help me out to solve this , I would be extremly obliged. I am kind a stuck here.
Regards
Usman
You are trying to do too much with get set
The get should return the set
If this is your set
this.CarcassDto.USDAYieldGrade = value;
Then this should be your get
return this.CarcassDto.USDAYieldGrade;
And you should not modify this.CarcassDto.USDAYieldGrade in the get
This is just squirrely
if (this.CarcassDto.USDAYieldGrade != YieldGrade.NoGrade)
return this.CarcassDto.USDAYieldGrade;
return YieldGrade.NoGrade;
Same as
if (this.CarcassDto.USDAYieldGrade == YieldGrade.NoGrade)
{
return YieldGrade.NoGrade;
}
else
{
return this.CarcassDto.USDAYieldGrade;
}
Same as
if (this.CarcassDto.USDAYieldGrade == YieldGrade.NoGrade)
{
return this.CarcassDto.USDAYieldGrade;
}
else
{
return this.CarcassDto.USDAYieldGrade;
}
Same as
return this.CarcassDto.USDAYieldGrade;
I have a variable value string i = xyz;
and i want to set dropdownlist selectedIndex value from a variable value.
And I am trying this
dd_Interest.SelectedIndex = dd_Interest.Items.IndexOf(dd_Interest.Items.FindByText(categoryInterest1));
But its not working
How can i do this?
Waiting for reply
You not need to find the index of the selected item you just try the following code where you want to get the selected value of dropdownlist.
dd_Interest.SelectedItem.Value = Convert.ToString(Request.QueryString["searchInterest"]);
Or there some other ways like given below:
dd_Interest.Items.FindByText(Convert.ToString(Request.QueryString["searchInterest"])).Selected= true;
That's all.
It is very simple, just set the SelectedItem property
dd_Interest.SelectedItem= myString
Have a look here at the MSDN documentation
ComboBox.SelectedItem Property
How about:
dd_Interest.Items.FindByText("some text").Selected = true;
or
dd_Interest.Items.FindByValue("some value").Selected = true
You can also do a combination between both using Linq:
ListItem item = dd_Interest.Items.Cast<ListItem>()
.FirstOrDefault(x => x.Text == "some text"|| x.Value == "some value");
if (item != null)
{
item.Selected = true;
}
Hope this helps :)
I read in your comment, and I'm not sure what's the error you met. Anyway I saw something strange in your comment...
This is a querystring (Request.QueryString["searchInterest"];) and I
get querystring value in a variable str_LastSearchInterest =
Request.QueryString["searchInterest"]; and I get
querystring(Interest|AP Chinese) and then i store AP Chinese in a
variable categoryInterest1 =
str_LastSearchInterest.Split('|')[0].ToString(); and Interest in
categoryInterest2 = str_LastSearchInterest.Split('|')[1].ToString();
But after splited, at the index 0 should store "Interest" and index 1 should store "AP Chinese".
So categoryInterest1 value should be "Interest", not "AP Chinese". Then if you want to find index of "AP Chinese" and select it as default, you have to use categoryInterest2 instead.
I am setting certain controls using this
divCorporateId.Style.Add("Visibility", "hidden");
How do i check what the value is in another method?
I thought something like:
if (divCorporateId.Style........ == "hidden")
{
do something
}
Something like:
if (divCorporateId.Style[HtmlTextWriterStyle.Visibility] == "hidden")
{
do something
}
You can get Css property value in C# like this
string value = divCorporateId.Style["Key"]
then you can apply your conditions based on this value.
I want to write a condition for when some cells of DataGridView are null or empty.
For example if cells[1] isn't null or empty , a method must be run and ...
I wrote it in some way , but some of them didn't work, and one of them work but its not good solution for my problem.As you now ,empty and null are different in DataGridView.
Additionally , My DataGridView has not been bound to database.
How can i do that in best way?
Regards.
DataGridViewCell object has "Value" method which returns callvalue as object, That value you can convert to string then check this string against null or empty.
string val = this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value as string;
if(string.isNullorEmpty(val) ==false)
{
// your method run.
}
Cell value is a Object. Empty cell Value is null.
DataTable / DataView uses DbNull.Value when Empty.
Strings with Length = 0 are String.Empty
You need verify three options.
if your columns and row layout is fixed then you may call out your function in the cell validating event of the datagrid
void dataGridView2_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
e.Cancel = false;
if (e.RowIndex == 1 && e.ColumnIndex == 0)
{
if(string.IsNullorEmpty(e.FormattedValue.ToString())
// method call
}
}
//just replace from Value to FormattedValue like that
string val = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].FormattedValue as string;
if(string.isNullorEmpty(val))
{
// cell is empty
}
else
{
// cell is not empty
}
I already tried this way but it is not working.
How do I fix it?
if(dgvProducts.Rows.Count < 1 )
{
MessageBox.Show("Something");
return;
}
you may try something like this,
if(dgvProducts.Rows.Count > 1 )
{
MessageBox.Show("Something");
return;
}
if it doesn't works then you may also try something like this too,
string nrCode = dataGridView1.Rows[0].Cells[6].Value.ToString();
nrCode = nrCode.Trim();
if (nrCode == string.Empty)
{
MessageBox.Show("there must be Entry in cell nrCode on first row.")
}
the above code will allow you to check if specific cell in first row in DataGridView is empty.
Hope it works.