Listview group works wierdly - c#

In my app, I display a listview with a few entries. I can group them by groups and it works well.
When I do the same in Excel/MatLab via the API I created (based on the same code) the groups doesn't work, see pictures:
In the application:
And in the API:
As you can see, it doesn't work. Here's the code I use:
// Sets the ListView to the groups created for the specified column.
private void SetGroups(Hashtable groups,int column)
{
// Remove the current groups.
listView1.Groups.Clear();
// Copy the groups for the column to an array.
ListViewGroup[] groupsArray = new ListViewGroup[groups.Count];
groups.Values.CopyTo(groupsArray, 0);
// Sort the groups and add them to the ListView.
Array.Sort(groupsArray, new ListViewGroupSorter(listView1.Sorting));
listView1.Groups.AddRange(groupsArray);
// Iterate through the items in the ListView, assigning each
// one to the appropriate group.
foreach (ListViewItem item in listView1.Items)
{
// Retrieve the subitem text corresponding to the column.
string subItemText = item.SubItems[column].Text;
// For the Title column, use only the first 3 letters.
if (column == 0)
{
subItemText = subItemText.Substring(0,3);
}
// Assign the item to the matching group.
item.Group = (ListViewGroup) groups[subItemText];
}
}
How can I fix this?

Put Application.EnableVisualStyles(); in constructor of form:
public Form1()
{
InitializeComponent();
Application.EnableVisualStyles();
}

Related

Filtering listbox using textbox using Linq but listbox stays full

I am unable to filter listbox data using linq and textbox.Listbox values don 't change when I enter text to be filtered inside the textbox. This function is important so that I can send the data to another form which contains the listbox.
public void GetList(List<SemesterDetails> modules)
{
string filter = txtFilter.Text;
foreach (var item in modules.Where(m => m.ModuleName.Contains(filter)))
{
LstModules.Items.Add(item);
}
}
I tried to call the function to the textbox event function:
private void txtFilter_TextChanged(object sender, TextChangedEventArgs e)
{
List<SemesterDetails> semesters = new List<SemesterDetails>();
GetList(semesters);
}
The result is:
Items remain unfiltered in the listbox.
List<SemesterDetails> semesters = new List<SemesterDetails>();
GetList(semesters);
This is an empty list.
public void GetList(List<SemesterDetails> modules)
{
string filter = txtFilter.Text;
foreach (var item in modules.Where(m => m.ModuleName.Contains(filter)))
{
LstModules.Items.Add(item);
}
}
Since you're passing in an empty list, the foreach will be executed 0 times, and therefore nothing can ever be added.
The filtering is irrelevant in this scenario.
Items remain unfiltered in the listbox
Nowhere in your code do you ever remove the items that are already in the listbox. Therefore, it's not possible for the listbox' contents to reduce.
When I add clear Listbox prior, I get no items
If you clear a list and then not add items to it (as discussed above), an empty list is indeed the logical outcome.

Select the specific column of ListView and print it in a new messagebox in C#.net

I've just started to use ListView in C#.net.
I got to know how to add items and subitems. Going through the listview I wanted to fetch all the data from a whole column with multiple rows.
I want to know how to do this.
I found this code to list a specific selected data from a row:
ListView.SelectedIndexCollection sel = listView1.SelectedIndices;
if (sel.Count == 1)
{
ListViewItem selItem = listView1.Items[sel[0]];
MessageBox.Show(selItem.SubItems[2].Text);
}
That was helpful but i want to list all the items in a row, may be i want to add all the column items in array?
private string[] GetListViewItemColumns(ListViewItem item) {
var columns = new string[item.SubItems.Count];
for (int column = 0; column < columns.Length; column++) {
columns[column] = item.SubItems[column].Text;
}
return columns;
}
I would recommend some caution against doing this. A ListView is really meant to display information, it is not a great collection class. Getting the data out of it is slow and crummy, it can only store strings. Keep the data in your program in its original form, maybe a List<Foo>. Now it is simple and fast.
foreach (ListViewItem item in listView1.Items) {
// Do something with item
}
you could do this by
foreach(ListViewItem item in listView1.Items)
{
foreach(var subtem in item.SubItems)
{
// Do what ever you want to do with the items.
}
}

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); }
}

List view new line for new values

The following code produces a ListView with the names of the customers:
private void displayDeliveries()
{
lstDeliveryDetails.Items.Clear();
foreach (Delivery d in mainForm.myDeliveries)
{
lstDeliveryDetails.Items.Add(d.DeliveryName);
}
}
If I add, (d.DeliveryAddress), how can I get it to line up alongside the correct name?
I assume you mean that you want the delivery address to appear in the next column.
Set ListView's View property to Details.
Add two columns to the ListView's Collumns collection
Use the following code to populate the ListView
private void displayDeliveries()
{
lstDeliveryDetails.Items.Clear();
foreach (Delivery d in mainForm.myDeliveries)
{
ListViewItem item = lstDeliveryDetails.Items.Add(d.DeliveryName);
item.SubItems.Add(d.DeliveryAddress);
}
}
Assuming want to have the Address followed by the name in each list item:
foreach (Delivery d in mainForm.myDeliveries)
{
lstDeliveryDetails.Items.Add(d.DeliveryName + " " + d.DeliveryAddress);
}
This simply concatenates the DeliveryName with a space (" ") and then the DeliveryAddress strings.
Change the code in the for loop to:
ListViewItem item = lstDeliveryDetails.Items.Add(d.DeliveryName);
item.SubItems.Add(d.DeliveryAddress);

Listview filling issue

I need to know what are the things going on here.Actually my aim is to add a column additionally and display the contents.I Added column but i want to know
the codeflow required to finish to display items in that column
ManagedDeviceCollection list = new ManagedDeviceCollection();
try
{
if(SpoServer == null)
return;
_listSelected.BeginUpdate();
_listAvailable.BeginUpdate();
#region populate the selected list
// Collect selected items.
object[] selected = new object[_listSelected.SelectedItems.Count];
_listSelected.SelectedItems.CopyTo(selected, 0);
// Clear listview.
_listSelected.Items.Clear();
// Add systems.
ResourcePolicySystemsLVI item;
foreach(ManagedDevice md in PolicySystemsList)
{
item = new ResourcePolicySystemsLVI(md);
item.Update();
foreach(object obj in selected)
{
item.Selected = (((ResourcePolicySystemsLVI)obj).Data == item.Data);
break;
}
_listSelected.Items.Add(item);
}
// Sort list.
_listSelected.Sort();
// Ensure selected items are visible.
if(_listSelected.SelectedItems.Count > 0)
{
_listSelected.SelectedItems[_listSelected.SelectedItems.Count
- 1].EnsureVisible();
_listSelected.SelectedItems[0].EnsureVisible();
}
#endregion
#region populate the available list
// Collect selected items.
selected = new object[_listAvailable.SelectedItems.Count];
_listAvailable.SelectedItems.CopyTo(selected, 0);
// Clear listview.
_listAvailable.Items.Clear();
// Add systems.
ResourcePolicyAvailSystemsLVI item2;
foreach(ManagedDevice md in AvailableSystemsList)
{
item2 = new ResourcePolicyAvailSystemsLVI(md);
item2.Update();
foreach(object obj in selected)
{
item2.Selected = (((ResourcePolicyAvailSystemsLVI)obj).Data
== item2.Data);
break;
}
_listAvailable.Items.Add(item2);
}
// Sort list.
_listAvailable.Sort();
// Ensure selected items are visible.
if(_listAvailable.SelectedItems.Count > 0)
{
_listAvailable.SelectedItems[_listAvailable.SelectedItems.Count
- 1].EnsureVisible();
_listAvailable.SelectedItems[0].EnsureVisible();
}
#endregion
_listSelected.EndUpdate();
_listAvailable.EndUpdate();
To display text in columns after the first, use the ListViewItem.SubItems collection. For example, you might modify your main loop as follows:
item2 = new ResourcePolicyAvailSystemsLVI(md);
// skipped some of your code
item2.SubItems.Add("My second piece of info"); // this is what puts data into the second column
I wasn't sure from your question whether you had already added the requisite column to the ListView: if not, you must do so using ListView.Columns.Add in order to display the subitem.

Categories

Resources