I'm curious as to the best route (more looking towards simplicity, not speed or efficiency) to sort a DropDownList in C#/ASP.NET - I've looked at a few recommendations but they aren't clicking well with me. this drop down is giving me list in alphabetical order. But I have to sort out randomly.
Note:I do not have control over how the data comes into the DropDownList - I cannot modify the SQL.
public void populateLocationList()
{
DataTable dt_locations = (DataTable)daa_addresses.GetDataByEventLocations(int_eventid);
if (dt_locations != null && dt_locations.Rows.Count > 0)
{
// Populate locations dropdown menu
// Bind locationsList instead of dt_locations
ddl_locations.DataTextField = "Key";
ddl_locations.DataValueField = "Value";
ddl_locations.DataSource = RemoveDuplicateLocations(dt_locations);
ddl_locations.DataBind();
string location = "";
// Set the selection based upon the query string
if (Request.QueryString["loc"] != null)
{
location = Request.QueryString["loc"];
locationLbl.Text = Request.QueryString["loc"];
locationID.Value = "-1";
}
if (dt_locations.Rows.Count == 1)
{
location = ddl_locations.Items[0].Text;
locationLbl.Text = location;
}
// Set location in drop down list
int int_foundlocation = 0;
bool foundLocation = false;
foreach (ListItem lsi_item in ddl_locations.Items)
{
if (lsi_item.Text.ToLower().Trim() == location.ToLower().Trim())
{
int_foundlocation = ddl_locations.Items.IndexOf(lsi_item);
foundLocation = true;
break;
}
}
ddl_locations.SelectedIndex = int_foundlocation;
if (ddl_locations.Items.Count == 1)
{
// Make location label visible.
locationLbl.Visible = true;
ddl_locations.Visible = false;
}
else
{
locationLbl.Visible = false;
ddl_locations.Visible = true;
}
//* defualt location S for short courses *//
if (!IsPostBack && !foundLocation)
{
ListItem s = ddl_locations.Items.FindByText("S");
int index = 0;
if (s != null)
{
index = ddl_locations.Items.IndexOf(s);
}
ddl_locations.SelectedIndex = index;
ddl_locations.DataBind();
}
}
}
I have to sort out randomly.
You'd have to shuffle rows, probably with something close to this code (borrowed from #configurator's answer):
internal static class Extensions
{
internal static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source, Random rng)
{
T[] elements = source.ToArray();
// Note i > 0 to avoid final pointless iteration
for (int i = elements.Length - 1; i > 0; i--)
{
// Swap element "i" with a random earlier element it (or itself)
int swapIndex = rng.Next(i + 1);
yield return elements[swapIndex];
elements[swapIndex] = elements[i];
// we don't actually perform the swap, we can forget about the
// swapped element because we already returned it.
}
// there is one item remaining that was not returned - we return it now
yield return elements[0];
}
}
Assuming that RemoveDuplicateLocations returns a DataTable the binding part of your code should be changed to:
ddl_locations.DataSource = RemoveDuplicateLocations(dt_locations)
.AsEnumerable()
.Shuffle(new Random())
.CopyToDataTable();
Related
I have group of conditions like
foreach
{
Condition a
condition b
}
So I am validating the values based on conditions.I am facing the problem: for example I have list of items like {1,2,3,4}. So I have a condition like if item 1 is fail then item 2,3,4 should be fail.
if item 2 is fail then item 3,4 should be fail and so on.
I am trying in below code.
foreach (SagaData item in lstPrm)
{
PriceDetail prevPrice = null;
PriceDetail currentPrice = null;
PriceDetail nextPrice = null;
bool bHasError = false;
int iPriceMasterId = 0;
int iPriceTypeId = 0;
string sMprCurrencyType = null;
string sPublisherCurrencyType = null;
int? iExpirationCalendarId = 0;
string sPRMMessage = string.Empty;
//a) If Change Indicator = C or A and Price = 0.00: Cannot change price value to zero.
if ((item.ChangeIndicator == 'C' || item.ChangeIndicator == 'A') && item.PostingPrice == 0)
{
bHasError = true;
sPRMMessage = "FAILURECannot change price value to zero";
}
//b) If Change Indicator = D and Price > 0.00: Invalid deactivation.
if ((item.ChangeIndicator == 'D') && (item.PostingPrice > 0) && (!bHasError))
{
bHasError = true;
sPRMMessage = "FAILUREInvalid deactivation";
}
so i have if condition a fail for item 1 then how should i keep maintain the error for next iteration.
Thanks for the help. if you want more info plz let me know.
You can go through your Collection with a simple for loop and use an ErrorArray:
bool[] bHasError = new bool[lstPrm.Count];
for (int i = 0; i < lstPrm.Count; i++)
{
...
bHasError[i] = true;
...
}
or you can define bHasError BEFORE the foreach if one error is enough for you to consider.
Scope: Sort listview by ColumnClick Ascending and/or Descending.
Column click is working and sorts columns Descending, however clicking on the same column, or any column, does not sort Ascending. Can not figure out out what I am missing.
ColumnClick Event
// The column we are currently using for sorting.
private ColumnHeader SortingColumn = null;
private void list_ProjectsColumnClick(object sender, ColumnClickEventArgs e)
{
// Get the new sorting column.
ColumnHeader new_sorting_column = listProjects.Columns[e.Column];
// Figure out the new sorting order.
System.Windows.Forms.SortOrder sort_order;
if (SortingColumn == null)
{
// New column. Sort ascending.
sort_order = System.Windows.Forms.SortOrder.Ascending;
}
else
{
// See if this is the same column.
if (new_sorting_column == SortingColumn)
{
// Same column. Switch the sort order.
if (SortingColumn.Text.StartsWith("> "))
{
sort_order = System.Windows.Forms.SortOrder.Descending;
}
else
{
sort_order = System.Windows.Forms.SortOrder.Ascending;
}
}
else
{
// New column. Sort ascending.
sort_order = System.Windows.Forms.SortOrder.Ascending;
}
// Remove the old sort indicator.
SortingColumn.Text = SortingColumn.Text.Substring(2);
}
// Display the new sort order.
SortingColumn = new_sorting_column;
if (sort_order == System.Windows.Forms.SortOrder.Ascending)
{
SortingColumn.Text = "> " + SortingColumn.Text;
}
else
{
SortingColumn.Text = "< " + SortingColumn.Text;
}
// Create a comparer.
listProjects.ListViewItemSorter =
new ListViewComparer(e.Column, sort_order);
// Sort.
listProjects.Sort();
}
ListViewComparer Class
class ListViewComparer : IComparer
{
private int ColumnNumber;
private System.Windows.Forms.SortOrder SortOrder;
public ListViewComparer(int column_number,
System.Windows.Forms.SortOrder sort_order)
{
ColumnNumber = column_number;
SortOrder = sort_order;
}
// Compare two ListViewItems.
public int Compare(object object_x, object object_y)
{
// Get the objects as ListViewItems.
ListViewItem item_x = object_x as ListViewItem;
ListViewItem item_y = object_y as ListViewItem;
// Get the corresponding sub-item values.
string string_x;
if (item_x.SubItems.Count <= ColumnNumber)
{
string_x = "";
}
else
{
string_x = item_x.SubItems[ColumnNumber].Text;
}
string string_y;
if (item_y.SubItems.Count <= ColumnNumber)
{
string_y = "";
}
else
{
string_y = item_y.SubItems[ColumnNumber].Text;
}
// Compare them.
int result;
double double_x, double_y;
if (double.TryParse(string_x, out double_x) &&
double.TryParse(string_y, out double_y))
{
// Treat as a number.
result = double_x.CompareTo(double_y);
}
else
{
DateTime date_x, date_y;
if (DateTime.TryParse(string_x, out date_x) &&
DateTime.TryParse(string_y, out date_y))
{
// Treat as a date.
result = date_x.CompareTo(date_y);
}
else
{
// Treat as a string.
result = string_x.CompareTo(string_y);
}
}
// Return the correct result depending on whether
// we're sorting ascending or descending.
if (SortOrder == System.Windows.Forms.SortOrder.Ascending)
{
return result;
}
else
{
return -result;
}
}
The following code works for to show people who have holidays with an icon in my calendar but the problem i have is the first line of code in my for statement where i extract the date times
protected void apertureAppointments_TimeSlotCreated(object sender, TimeSlotCreatedEventArgs e)
int i = 0;
bool isFound = false;
List<tblApertureNetShiftPattern> _list = new List<tblApertureNetShiftPattern>();
_list = _dal.getHolidays();
List<Resource> resources = new List<Resource>(apertureAppointments.Resources.GetResourcesByType("Managers"));
Resource res = resources[5];
foreach (tblApertureNetShiftPattern sp in _list)
{
if (_list.Count >= 1)
i++;
else
i = 0;
DateTime? dt1 = _list[i - 1].startdate;
DateTime? dt2 = _list[i - 1].endDate;
if (e.TimeSlot.Start == dt1 && e.TimeSlot.Resource.Text == sp.manager_name)
{
isFound = true;
if (DoDateRangesOverlap(e.TimeSlot.Start, e.TimeSlot.End, dt1, dt2) && isFound == true)
{
Label temperatureLabel = new Label();
if (sp.appointmentType == Constants.shiftDayoff)
{
e.TimeSlot.CssClass = "DayOfCssStyle";
temperatureLabel.CssClass = "DayOfCssStyle";
}
else if (sp.appointmentType == Constants.shiftHoliday)
{
e.TimeSlot.CssClass = "HolidayCssStyle";
temperatureLabel.CssClass = "HolidayCssStyle";
}
else if (sp.appointmentType == Constants.shiftStat)
{
e.TimeSlot.CssClass = "statCssStyle";
temperatureLabel.CssClass = "statCssStyle";
}
else if (sp.appointmentType == Constants.shiftsickDay)
{
e.TimeSlot.CssClass = "SickDayStyle";
temperatureLabel.CssClass = "SickDayStyle";
}
temperatureLabel.Text = sp.Description;
Image imageControl = new Image();
imageControl.ImageUrl = #"~\images\aperturenet\Calendar\resources\holidays.png";
temperatureLabel.BackColor = System.Drawing.Color.Orange;
dt1 = null;
dt2 = null;
isFound = false;
e.TimeSlot.Control.Controls.AddAt(1, temperatureLabel);
e.TimeSlot.Control.Controls.AddAt(2, imageControl);
}
}
}
My Problem lies in this code here
if (_list.Count >= 1)
i++;
else
i = 0;
DateTime? dt1 = _list[i - 1].startdate;
DateTime? dt2 = _list[i - 1].endDate;
When I do not have the -1 in the [ array int] it bombs out because obv the list shows 0,1 and the loop probally shows 2 items. What is the best way to avoid the object out of range index error.?
Getholidays is just a simple list of when the staf memeber our off so nothing to fancy here.
public List<tblApertureNetShiftPattern> getHolidays()
{
List<tblApertureNetShiftPattern> list = new List<tblApertureNetShiftPattern>();
var q = from _holidays in apertureNetEntities.tblApertureNetShiftPatterns.Where(w => w.isDeleted == false)
select _holidays;
list = q.ToList();
return list;
}
Also is their a neater way of doing I somehow feel that my code is very bloated.
Are you sure, these are the lines causing your problem? Because you can only enter the body of the foreach loop if the _list contains at least one element. Thus, _list.Count >= 1 will always be true, and i will be incremented and never become -1.
I suspect your out of bound exception originating from this line of code
List<Resource> resources = new List<Resource>(apertureAppointments.Resources.GetResourcesByType("Managers"));
Resource res = resources[5];
Are you sure, there are always at least 6 items in this list?
Generally, why do you access your listitems by index, if you iterate them with a foreach loop? YOu could easily do it as follows
foreach (tblApertureNetShiftPattern sp in _list) {
DateTime? dt1 = sp.startDate;
DateTime? dt2 = sp.endDate;
}
just like you do it with the manager_name property.
If you insist on accessing by index, I suggest incrementing the index variable i at the end of the loop. This way you won't always have to decrement the index before accessing the array.
To find an item (and select it) in a dropdownlist using a value we simply do
dropdownlist1.Items.FindByValue("myValue").Selected = true;
How can I find an item using partial value? Say I have 3 elements and they have values "myValue one", "myvalue two", "myValue three" respectively. I want to do something like
dropdownlist1.Items.FindByValue("three").Selected = true;
and have it select the last item.
You can iterate from the end of the list and check if value contains the item (this will select the last item which contains value "myValueSearched").
for (int i = DropDownList1.Items.Count - 1; i >= 0 ; i--)
{
if (DropDownList1.Items[i].Value.Contains("myValueSearched"))
{
DropDownList1.Items[i].Selected = true;
break;
}
}
Or you can use linq as always:
DropDownList1.Items.Cast<ListItem>()
.Where(x => x.Value.Contains("three"))
.LastOrDefault().Selected = true;
You can iterate the items in your list, and when you find the first one whose items's string contains the pattern, you can set its Selected property to true.
bool found = false;
int i = 0;
while (!found && i<dropdownlist1.Items.Count)
{
if (dropdownlist1.Items.ToString().Contains("three"))
found = true;
else
i++;
}
if(found)
dropdownlist1.Items[i].Selected = true;
Or you could write a method (or extension method) that does this for you
public bool SelectByPartOfTheValue(typeOfTheItem[] items, string part)
{
bool found = false;
bool retVal = false;
int i = 0;
while (!found && i<dropdownlist1.Items.Count)
{
if (items.ToString().Contains("three"))
found = true;
else
i++;
}
if(found)
{
items[i].Selected = true;
retVal = true;
}
return retVal;
}
and call it like this
if(SelectByPartOfTheValue(dropdownlist1.Items, "three")
MessageBox.Show("Succesfully selected");
else
MessageBox.Show("There is no item that contains three");
Above mentioned answers are perfect, just there are not case sensitivity proof :
DDL.SelectedValue = DDL.Items.Cast<ListItem>().FirstOrDefault(x => x.Text.ToLower().Contains(matchingItem)).Text
I have a datagridview that I would like to have rows sorted based on the portion of a string entered from a user. The entered string is compared with all of the strings in a particular column. For instance, if I gave "comp" as the search word, the program would try to compare the search word with the strings on first column and sort the rows in a descending order which starts with "comp", such as "compare", "composition", "computer" etc. Rest of the words that do not match is either left alone or sorted in an alphabetical order (whichever is easier).
In LINQ, I am aware that you can apply the following code to achieve what you wanted with a string array:
var sortedWords = words.Where(x => x.Contains("comp"))
.OrderByDescending(x => x);
How can I achieve the same thing in Datagridview as I need to have the rows sorted, not just the items inside a particular column?
Edit:
The following code is giving a System.InvalidOperationException. (SetCurrentCellAddressCore is being called twice)
private void DGVPointCtrl_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
MatchComparer mc = new MatchComparer();
DGVPointCtrl.Sort(mc); //Error
}
I'm probably doing something wrong but I'm not sure why. Here is the code that programatically adds the rows for testing purposes:
private void BtnRefresh_Click(object sender, EventArgs e)
{
try
{
DGVPointCtrl.Rows.Clear();
int mainIndex = CmbMainDevice.SelectedIndex;
int subIndex = CmbSubDevice.SelectedIndex;
DDCDAO ddcdao = new DDCDAO(DDCGlobal.ddcEngineIP, ddc.Ip);
string pointListType;
object rs;
//Currently only supports IO DDC Request
//TO DO: Change DDCDAO to send proper subdevice requests
if (mainIndex == 0) //IO
{
#region Main Device: IO
}
//First row is for searching items
DGVPointCtrl.Rows.Add(new DataGridViewRow());
for (int i = 1; i < 5; i++)
{
DGVPointCtrl.Rows.Add(new DataGridViewRow());
DGVPointCtrl.Rows[i].ReadOnly = true;
}
DGVPointCtrl.Columns[0].SortMode = DataGridViewColumnSortMode.Programmatic;
DGVPointCtrl.Rows[0].DefaultCellStyle.Font =
new Font(DGVPointCtrl.DefaultCellStyle.Font, FontStyle.Italic | FontStyle.Bold);
if (subIndex == 1) //BI
{
PointDGVColumnGenerate("IO_BI");
}
else if (subIndex == 2) //BO
{
PointDGVColumnGenerate("IO_BO");
}
else if (subIndex == 3) //AI
{
PointDGVColumnGenerate("IO_AI");
}
else if (subIndex == 4) //AO
{
PointDGVColumnGenerate("IO_AO");
}
DGVPointCtrl.Rows[1].Cells[0].Value = "IO12314";
DGVPointCtrl.Rows[2].Cells[0].Value = "IO21948";
DGVPointCtrl.Rows[3].Cells[0].Value = "IO28194";
DGVPointCtrl.Rows[4].Cells[0].Value = "VP12984";
DGVPointCtrl.Rows[2].Cells[1].Value = "asdf";
#endregion
}
catch
{
}
}
private void PointDGVColumnGenerate(string key)
{
int colCount = 0;
DGVColumnTable.Clear();
for (int i = 0; i < COL_MAX; i++)
{
DGVPointCtrl.Columns[i].HeaderText = " ";
DGVPointCtrl.Columns[i].Visible = true;
}
foreach (string s in UIConstant.DDCPCtrlListColumnText[key])
{
DGVPointCtrl.Columns[colCount].HeaderText = s;
DGVColumnTable.Add(DGVPointCtrl.Columns[colCount]);
colCount++;
}
}
Edit2:
public class MatchComparer : IComparer
{
private static IComparer defaultComparer = new CaseInsensitiveComparer();
int IComparer.Compare(object x, object y)
{
DataGridViewRow xr = (DataGridViewRow)x;
DataGridViewRow yr = (DataGridViewRow)y;
string xs = "";
string ys = "";
try
{
xs = xr.Cells[0].Value.ToString();
}
catch
{
}
try
{
ys = yr.Cells[0].Value.ToString();
}
catch
{
}
if (HasMatch(xs) && !HasMatch(ys)) return -1;
else if (!HasMatch(xs) && HasMatch(ys)) return 1;
else return defaultComparer.Compare(xs, ys);
}
This is possible only if you are populating the grid yourself as opposed to binding it to the database.
Set DataGridViewColumn.SortMode to Programmatic.
Use DataGridView.Sort to impose a comparer like this:
public class MatchComparer : IComparer {
int IComparer.Compare(object x, object y) {
if (HasMatch(x) && !HasMatch(y)) return -1;
else if (!HasMatch(x) && HasMatch(y)) return 1;
else return defaultComparer.Compare(x, y);
}
private bool HasMatch(object x) {
return x is string && ((string)x).StartsWith("comp");
}
private static IComparer defaultComparer = new CaseInsensitiveComparer();
}