How can I check if no items "At all" selected from listview?
Thanks.
if( myListView.SelectedItems == null || myListView.SelectedItems.Count == 0 )
{
}
See ListView..::.SelectedItems Property for more info.
EDIT: As per the MSDN documentation:
If no items are currently selected, an
empty
ListView..::.SelectedListViewItemCollection
is returned.
So the null check is not needed in this case and you can simply do:
if( myListView.SelectedItems.Count == 0 )
{
}
Related
I am trying to check if a combo box is empty using C# in a Windows Application Form. Below are two possible ways:
if (string.IsNullOrEmpty(comboBox1.Text))
if (comboBox1.SelectedIndex == -1)
In order to ensure that the user would ONLY select a value from the drop down and NOT write it s own answer, which is the best approach?
From my research the second method (if (comboBox1.SelectedIndex == -1)) will satisfy my needs. Am l right?
If your concern is only making sure that users choose an item from the list available, simply change your combobox's DropDownStyle to DropDownList
or if you want to allow them to type but then ensure it is on the list, you can do something like this:
var txt = comboBox1.Text;
if(string.IsNullOrEmpty())
return;
var test = comboBox1.Items?.OfType<string>().ToList().Any(x => x?.Trim() == txt?.Trim());
so if test is false, it means what they have selected/typed does not exist in list of available items
for combobox you can use this code below to check if it's empty or not
if(comboBox1.Items.Count == 0 )
{
// your code
}
This is what i try and it 's work. Feel free to comment:
if (comboBox1.SelectedIndex > -1 )
I have a RadListBox which has 11 items. And I need to have the first 7 items as static which means they shouldn't be reordered. I have written the below javascript and it works just fine.
The 8th item in listbox is still movable(up) and this shouldn't happen. I need to reorder the items only after 7th item till 11th item and only within themselves. Means out of 11 items, first 7 items order should be static and from 8 to 11 these fields can be reordered.
Can anyone suggest how this can be achieved?
HTML:
<telerik:RadListBox ID="RadListBox" runat="server"
AllowReorder="true OnClientSelectedIndexChanging="RadListBox_Reordering"/>
JS:
function RadListBox_Reordering(sender, eventArgs) {
var value = eventArgs.get_item().get_value();
if (value == "Item1" || value == "Item2" || value == "Item3" || value == "Item4" || value == "Item5" || value == "Item6" || value == "Item7") {
eventArgs.set_cancel(true);
}
}
You can use the OnClientDropping event, which is cancelable, to detect where the reordered item is landing.
function OnClientDroppingHandler(sender, eventArgs) {
var index = args.get_destinationItem().get_index();
if (index < 8) {
alert('you may not drop on the first 7 items');
args.set_cancel(true);
}
}
https://www.telerik.com/forums/how-to-disable-dragging-dropping-to-first-position
How to check for empty gridview row. I have tried the following
for (int i = 0; i < 5; i++)
{
if(i=0 &&
(Label)gvMaster.Rows[0].Cells[0].FindControl("dealer_name").Text!="" &&
gvMaster.Rows[0].Cells[0].FindControl("dealer_name") !=null)
{
// do something
}
}
I have getting an error stating that System.Web.UI.control does not contain definition for text.
How to check if the row exists and is empty or null?
Thanks
This:
if(i=0 && (Label)gvMaster.Rows[0].Cells[0].FindControl("dealer_name").
Text!="" && gvMaster.Rows[0].Cells[0].FindControl("dealer_name") !=null)
Should be This:
if((i==0) && (gvMaster.Rows[0].Cells[0].FindControl("dealer_name") !=null &&
(((Label)gvMaster.Rows[0].Cells[0].FindControl("dealer_name")).Text
.ToString().Trim()!=""))
Explanation :
1.for comparing values you should use == instead of single =.
2.You have to Cast the Control before reading the property values, Cast the Control as Label.
3.Trim the values before comparing strings for avoiding white space problems.
4.first do a null check before accessing the Control properties, because if control is not found it throws an Exception
What is ListBox SelectedIndex Equivalent in ListView ?
How i can write the below code in ListView ?
if(listbox.SelectedIndex == -1)
{
}
There are two ways to check if you have an item selected
if(listview.SelectedItems.Count > 0)
{
}
or
if(listview.SelectedIndices.Count > 0)
{
}
If multisect is true you could have more than one element in this collections else only the Item selected
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.