시간에 따라 블루스크린 정도가 변경이 되므로, 최고값 최소값 설정을 잘해두세요.
전 최소 최대를 똑같이 맞추고 씁니다.
(출처: https://justgetflux.com/ )
시간에 따라 블루스크린 정도가 변경이 되므로, 최고값 최소값 설정을 잘해두세요.
전 최소 최대를 똑같이 맞추고 씁니다.
(출처: https://justgetflux.com/ )
Data Loading using a DataSet
This article will show how to fill a ListView Control with the data loaded into a DataSet. You may use a DataSet bind it to a Grid Control to show the output of a query, but data binding of controls is not always the ideal method of accessing the data (You may encounter problems with the DataBinding). A DataSet maintains a copy of the entire resultset in the client systems memory in case you need to make changes to a row. Instead of using a bound grid and a DataSet, we can use the listview control with the view set to details mode and fill it with the data from a DataSet.
Sorting the ListView
When you are working with the ListView control, you may want to sort its contents based on a specific column. An example of this kind of functionality occurs in a Windows Explorer program when you view the contents of a folder on your hard disk. In Details view, Windows Explorer displays information about the files in that folder. For example, you see the file name, the file size, the file type, and the date that the file was modified. When you click one of the column headers, the list is sorted in ascending order based on that column. When you click the same column header again, the column is sorted in descending order.
The example in this article defines a class that inherits from the IComparer interface. Additionally, this example uses the Compare method of the CaseInsenstiveComparer class to perform the actual comparison of the items. Note that this method of comparison is not case sensitive ("Apple" is considered to be the same as "apple"). Also, note that all of the columns in this example are sorted in a "text" manner.
Introduction
The ListView control is a great way to display file system information and data from an XML file or database. The ListView control is typically used to display a graphical icon that represents the item, as well as the item text. In addition, the ListView control can be used to display additional information about an item in a subitem. For example, if the ListView control is displaying a list of files, you can configure the ListView control to display details such as file size and attributes as subitems. To display subitem information in the ListView control, you must set the View property to View.Details. In addition, you must create ColumnHeader objects and assign them to the Columns property of the ListView control. Once these properties are set, items are displayed in a row and column format that is similar to a DataGrid control. The ability to display items in this way makes the ListView control a quick and easy solution for displaying data from any type of data source.
Sorting for the ListView control is provided by using the Sorting property of the ListView. This enables you define the type of sorting to apply to the items. This is a great feature if you want to sort only by items. If you want to sort by subitems, you must use the custom sorting features of the ListView control. This article will demonstrate how to perform custom sorting in the ListView control and how to handle special data-type conditions when sorting.
Custom Sorting Features of the ListView Control
The ListView control provides features that enable you to use sorting other than that provided by the Sorting property. When the ListView control sorts items using the Sorting property, it uses a class that implements theSystem.Collections.IComparer interface. This class provides the sorting features used to sort each item. In order to sort by subitems, you must create your own class that implements the IComparer interface that in turn implements the sorting your ListView control needs. The class is defined with a constructor that specifies the column by which the ListView control is sorted. Once you have created this class, typically as a nested class of your form, you create an instance of this class and assign it to the ListViewItemSorter property of the ListView. This identifies the custom sorting class that the ListView control will use when the Sort method is called. The Sort method performs the actual sorting of the ListView items.
Initializing the Control
To begin, create an instance of a ListView control and add it to a form. After the control is on the form, add items to the ListView control using the Items property. You can add as many items as you want; just be sure that each item's text is unique. While you are creating the items, add subitems for each. The following table is an example of how this information might look in the ListView control.
Item Subitem 1 Subitem 2 ... ... ...5 ... ... ... ... ... ... // Initialize ListView
private void InitializeListView()
{
// Set the view to show details.
listView1.View = View.Details;
// Allow the user to edit item text.
listView1.LabelEdit = true;
// Allow the user to rearrange columns.
listView1.AllowColumnReorder = true;
// Select the item and subitems when selection is made.
listView1.FullRowSelect = true;
// Display grid lines.
listView1.GridLines = true;
// Sort the items in the list in ascending order.
listView1.Sorting = SortOrder.Ascending;
// Attach Subitems to the ListView
listView1.Columns.Add("Title", 300, HorizontalAlignment.Left);
listView1.Columns.Add("ID", 70, HorizontalAlignment.Left);
listView1.Columns.Add("Price", 70, HorizontalAlignment.Left);
listView1.Columns.Add("Publi-Date", 100, HorizontalAlignment.Left);
// The ListViewItemSorter property allows you to specify the
// object that performs the sorting of items in the ListView.
// You can use the ListViewItemSorter property in combination
// with the Sort method to perform custom sorting.
_lvwItemComparer = new ListViewItemComparer();
this.listView1.ListViewItemSorter = _lvwItemComparer;
}
Loading the ListView with Data from a DataSet
In this example we use a DataSet to load the "Titles" DataTable, which was filled with the Database Table "Titles" in the Pub Database on SQL-Server 2000.
// Load Data from the DataSet into the ListView
private void LoadList()
{
// Get the table from the data set
DataTable dtable = _DataSet.Tables["Titles"];
// Clear the ListView control
listView1.Items.Clear();
// Display items in the ListView control
for (int i = 0; i < dtable.Rows.Count; i++)
{
DataRow drow = dtable.Rows[i];
// Only row that have not been deleted
if (drow.RowState != DataRowState.Deleted)
{
// Define the list items
ListViewItem lvi = new ListViewItem(drow["title"].ToString());
lvi.SubItems.Add (drow["title_id"].ToString());
lvi.SubItems.Add (drow["price"].ToString());
lvi.SubItems.Add (drow["pubdate"].ToString());
// Add the list items to the ListView
listView1.Items.Add(lvi);
}
}
}
Handling the ColumnClick Event
In order to determine which set of subitems to sort by, you need to know when the user clicks a column heading for a subitem. To do this, you need to create an event-handling method for the ColumnClick event of the ListView. Place the event-handling method as a member of your form and ensure that it contains a signature similar to the one shown in the following code example.
// Perform Sorting on Column Headers
private void listView1_ColumnClick(
object sender,
System.Windows.Forms.ColumnClickEventArgs e)
{
// Determine if clicked column is already the column that is being sorted.
if (e.Column == _lvwItemComparer.SortColumn)
{
// Reverse the current sort direction for this column.
if (_lvwItemComparer.Order == SortOrder.Ascending)
{
_lvwItemComparer.Order = SortOrder.Descending;
}
else
{
_lvwItemComparer.Order = SortOrder.Ascending;
}
}
else
{
// Set the column number that is to be sorted; default to ascending.
_lvwItemComparer.SortColumn = e.Column;
_lvwItemComparer.Order = SortOrder.Ascending;
}
// Perform the sort with these new sort options.
this.listView1.Sort();
}Connect the event-handling method to the ListView control by adding code to the constructor of your form, as shown in the following example.
this.listView1.ColumnClick +=
new System.Windows.Forms.ColumnClickEventHandler(
this.listView1_ColumnClick);
Perform custom Sorting
Case Insenstive Sorting
The sorting is performed in a required method of the IComparer interface called Compare. This method takes two objects as parameters, which will contain the two items being compared. When the Sort method is called in the ColumnClick event-handling method of the ListView control, the Sort method uses the ListViewItemComparerobject that was defined and assigned to the ListViewItemSorter property and calls its Compare method.
In this example the ListViewItemComparer class uses the Compare method of the CaseInsenstiveComparer class to perform the actual comparison of the items. Note that this method of comparison is not case sensitive ("Apple" is considered to be the same as "apple"). Also, note that all of the columns in this example are sorted in a "text" manner.
The Compare method of the CaseInsenstiveComparer performs a case-insensitive comparison of two objects of the same type and returns a value indicating whether one is less than, equal to or greater than the other.
Add the following class definition to your Form class and ensure that it is nested properly inside your form.
// This class is an implementation of the 'IComparer' interface.
public class ListViewItemComparer : IComparer
{
// Specifies the column to be sorted
private int ColumnToSort;
// Specifies the order in which to sort (i.e. 'Ascending').
private SortOrder OrderOfSort;
// Case insensitive comparer object
private CaseInsensitiveComparer ObjectCompare;
// Class constructor, initializes various elements
public ListViewItemComparer()
{
// Initialize the column to '0'
ColumnToSort = 0;
// Initialize the sort order to 'none'
OrderOfSort = SortOrder.None;
// Initialize the CaseInsensitiveComparer object
ObjectCompare = new CaseInsensitiveComparer();
}
// This method is inherited from the IComparer interface.
// It compares the two objects passed using a case
// insensitive comparison.
//
// x: First object to be compared
// y: Second object to be compared
//
// The result of the comparison. "0" if equal,
// negative if 'x' is less than 'y' and
// positive if 'x' is greater than 'y'
public int Compare(object x, object y)
{
int compareResult;
ListViewItem listviewX, listviewY;
// Cast the objects to be compared to ListViewItem objects
listviewX = (ListViewItem)x;
listviewY = (ListViewItem)y;
// Case insensitive Compare
compareResult = ObjectCompare.Compare (
listviewX.SubItems[ColumnToSort].Text,
listviewY.SubItems[ColumnToSort].Text
);
// Calculate correct return value based on object comparison
if (OrderOfSort == SortOrder.Ascending)
{
// Ascending sort is selected, return normal result of compare operation
return compareResult;
}
else if (OrderOfSort == SortOrder.Descending)
{
// Descending sort is selected, return negative result of compare operation
return (-compareResult);
}
else
{
// Return '0' to indicate they are equal
return 0;
}
}
// Gets or sets the number of the column to which to
// apply the sorting operation (Defaults to '0').
public int SortColumn
{
set
{
ColumnToSort = value;
}
get
{
return ColumnToSort;
}
}
// Gets or sets the order of sorting to apply
// (for example, 'Ascending' or 'Descending').
public SortOrder Order
{
set
{
OrderOfSort = value;
}
get
{
return OrderOfSort;
}
}
}
Simple String Sorting
Another approach is to use the String.Compare method.
When the ListViewItemComparer object is created, it is assigned the index of the column that was clicked. This column index is used to access subitems from the column that needs to be sorted. The subitems are then passed to the String.Compare method, which compares the items and returns one of three results. If the item in the x parameter is less than the item in the y parameter, a value less than zero is returned. If the items are identical, a zero is returned. Finally, if the item in the x parameter is greater than the item in the y parameter, a value greater than zero is returned.
public int Compare(object x, object y)
{
int compareResult;
ListViewItem listviewX, listviewY;
// Cast the objects to be compared to ListViewItem objects
listviewX = (ListViewItem)x;
listviewY = (ListViewItem)y;
// Simple String Compare
compareResult = String.Compare (
listviewX.SubItems[ColumnToSort].Text,
listviewY.SubItems[ColumnToSort].Text
);
// Calculate correct return value based on object comparison
if (OrderOfSort == SortOrder.Ascending)
{
// Ascending sort is selected, return normal result of compare operation
return compareResult;
}
else if (OrderOfSort == SortOrder.Descending)
{
// Descending sort is selected, return negative result of compare operation
return (-compareResult);
}
else
{
// Return '0' to indicate they are equal
return 0;
}
}
Sorting Dates
Data that is placed into the ListView control as an item is displayed as text and stored as text. This makes it easy to sort using the String.Compare method in an IComparer class. String.Compare sorts both alphabetical characters and numbers. However, certain data types do not sort correctly using String.Compare, such as date and time information. For this reason, the System.DateTime structure has a Compare method just as the String class does. This method can be used to perform the same type of sorting based on chronological order. In this section, you modify only the Compare method to allow for dates to be sorted properly.
public int Compare(object x, object y)
{
int compareResult;
ListViewItem listviewX, listviewY;
// Cast the objects to be compared to ListViewItem objects
listviewX = (ListViewItem)x;
listviewY = (ListViewItem)y;
// Determine whether the type being compared is a date type.
try
{
// Parse the two objects passed as a parameter as a DateTime.
System.DateTime firstDate =
DateTime.Parse(listviewX.SubItems[ColumnToSort].Text);
System.DateTime secondDate =
DateTime.Parse(listviewY.SubItems[ColumnToSort].Text);
// Compare the two dates.
compareResult = DateTime.Compare(firstDate, secondDate);
}
// If neither compared object has a valid date format,
// perform a Case Insensitive Sort
catch
{
// Case Insensitive Compare
compareResult = ObjectCompare.Compare (
listviewX.SubItems[ColumnToSort].Text,
listviewY.SubItems[ColumnToSort].Text
);
}
// Calculate correct return value based on object comparison
if (OrderOfSort == SortOrder.Ascending)
{
// Ascending sort is selected, return normal result of compare operation
return compareResult;
}
else if (OrderOfSort == SortOrder.Descending)
{
// Descending sort is selected, return negative result of compare operation
return (-compareResult);
}
else
{
// Return '0' to indicate they are equal
return 0;
}
}The Compare method starts by casting the
x
andy
parameters to DateTime objects. This extraction is performed in a try/catch block to catch exceptions that might occur by forcing the casting of the two items being compared into DateTime objects. If an exception does occur, it signals to the code that the type being converted is not a valid date or time and can be sorted by the String.Compare method. If the two types are dates, they are sorted using the DateTime.Compare method.
The ListView control can provide the ability to display data in a number of ways. It can be used to display single items as well as items that contain subitem information. Using the sorting features provided by the ListView control, you can also enable users to sort items in the ListView control based on those subitems, regardless of the type of data being presented. This ability to sort items and their subitems enables your application to behave in ways that are familiar to users of Microsoft® Windows® Explorer and other applications that provide a ListView display of data and the ability to sort its contents.
Note that Dictionary is not a 100% drop in replacement for HashTable.
There is a slight difference in the way they handle NULLs. The dictionary will throw an exception if you try to reference a key that doesn't exist. The HashTable will just return null. The reason is that the value might be a value type, which cannot be null. In a Hashtable the value was always Object, so returning null was at least possible.
[스크랩] 비틀즈로 알아보는 모노와 스테레오의 차이 (0) | 2018.06.04 |
---|---|
SnapLock (0) | 2018.06.04 |
[스크랩] [C#] 람다식 작성 (0) | 2018.05.30 |
[.NET] ListView와 ListBox의 차이점 (0) | 2018.05.30 |
[UWP] StorageFolder.GetFoldersAsync Method (0) | 2018.05.26 |
SnapLock (0) | 2018.06.04 |
---|---|
[.NET] Hashtable Generic (0) | 2018.05.30 |
[.NET] ListView와 ListBox의 차이점 (0) | 2018.05.30 |
[UWP] StorageFolder.GetFoldersAsync Method (0) | 2018.05.26 |
[UWP] Uploading a file using Windows.Web.Http from UWP app to a WebAPI web service (0) | 2018.05.26 |
-
Have a look at the class inheritance hierarchy -
ListVIew
System.Object
System.MarshalByRefObject
System.ComponentModel.Component
System.Windows.Forms.Control
System.Windows.Forms.ListView
ListBox
System.Object
System.MarshalByRefObject
System.ComponentModel.Component
System.Windows.Forms.Control
System.Windows.Forms.ListControl
System.Windows.Forms.ListBox
Microsoft.VisualBasic.Compatibility.VB6.DirListBox
Microsoft.VisualBasic.Compatibility.VB6.FileListBox
System.Windows.Forms.CheckedListBox
A ListView
can be displayed using one of four different views.
-
The ListView control is used to display the values from a data source. It resembles the GridView control, except that it displays data by using user-defined templates instead of row fields. Creating your own templates gives you more flexibility in controlling how the data is displayed.
The ListView is exactly like the Windows Explorer lists, allowing views for tiles, detail, icons. ListView also allows other properties like unique colors for each item, and checkboxes.
ListBox is much simpler, vertical only list.
-
[.NET] Hashtable Generic (0) | 2018.05.30 |
---|---|
[스크랩] [C#] 람다식 작성 (0) | 2018.05.30 |
[UWP] StorageFolder.GetFoldersAsync Method (0) | 2018.05.26 |
[UWP] Uploading a file using Windows.Web.Http from UWP app to a WebAPI web service (0) | 2018.05.26 |
[UWP] 선택기를 사용하여 파일 및 폴더 열기 (0) | 2018.05.26 |
Get | Gets the subfolders in the current folder. |
Get | Gets the subfolders in the current folder. When the value of the query argument is something other than CommonFolderQuery.DefaultQuery, gets a list of virtual folders that represent containers for groups of files in the subfolders of the current folder. Files are grouped into folders based on the specified value from the CommonFolderQuery enumeration. |
Get | Gets an index-based range of folders from the list of all subfolders in the current folder. When the value of the query argument is something other than CommonFolderQuery.DefaultQuery, gets a list of virtual folders that represent containers for groups of files in the subfolders of the current folder. Files are grouped into folders based on the specified value from the CommonFolderQuery enumeration. |
Gets the subfolders in the current folder.
public IAsyncOperation<IReadOnlyList<StorageFolder>> GetFoldersAsync()
When this method completes successfully, it returns a list of the subfolders in the current folder. The list is of type IReadOnlyList<StorageFolder >. Each folder in the list is represented by a StorageFolder object.
You don't have permission to access the contents of the current folder.
The following example shows how to get the contents of the subfolders in the user's Pictures folder, grouped by month, by calling the GetFoldersAsync(CommonFolderQuery, UInt32, UInt32) method. (Files from the root of the current folder are not included.) This example returns a maximum of 4 folders, starting with the folder at index 0. Since the CommonFolderQuery.GroupByMonth option sorts dates in descending order (that is, from newest to oldest), this example returns folders for the 4 most recent months for which the user has photos. Each folder contains all the user's photos from that month.
Before you run the following example, enable the Pictures Library capability in the app manifest file.
using Windows.Storage;
using Windows.Storage.Search;
using System.Threading.Tasks;
using System.Diagnostics; // For writing results to Output window.
// Get the user's Pictures folder.
// Enable the corresponding capability in the app manifest file.
StorageFolder picturesFolder = KnownFolders.PicturesLibrary;
// Get the files in the subfolders of the user's Pictures folder,
// grouped by month. Get only the first 4 folders (months).
IReadOnlyList <StorageFolder> groupedItems = await picturesFolder.GetFoldersAsync(CommonFolderQuery.GroupByMonth, 0, 4);
// Iterate over the results and print the list of folders
// and files to the Visual Studio Output window.
foreach (StorageFolder folder in groupedItems)
{
Debug.WriteLine(folder.Name);
// To iterate over the files in each folder, uncomment the following lines.
// foreach(StorageFile file in await folder.GetFilesAsync())
// Debug.WriteLine(" " + file.Name);
}
// Get the user's Pictures folder.
// Enable the corresponding capability in the app manifest file.
StorageFolder^ picturesFolder = KnownFolders::PicturesLibrary;
// Get the files in the user's Pictures folder, grouped by month.
// Get only the first 4 folders (months).
create_task(picturesFolder->GetFoldersAsync(CommonFolderQuery::GroupByMonth)).then([=](IVectorView<StorageFolder^>^ itemsInFolder) {
//Iterate over the results and print the list of file groups
// to the visual studio output window
for (auto it = itemsInFolder->First(); it->HasCurrent; it->MoveNext())
{
StorageFolder^ file = it->Current;
String^ output = file->Name + "
";
OutputDebugString(output->Begin());
}
});
This query is a shallow query that returns only subfolders in the current folder.
The following table lists methods of the StorageFolder class that get a list of subfolders. The table identifies shallow queries that only return subfolders from the current folder, and deep queries that return the contents of nested subfolders, grouped into virtual folders.
Some methods take a value from the CommonFolderQuery enumeration.
July 2014
, August 2014
, and September 2014
. > > [!TIP] > You can use the DefaultQuery option with any folder; you can use the other options from the CommonFolderQuery enumeration only with library folders, such as the Pictures library, or the Homegroup folder.To get deep query results from a folder that's not a library folder, call the CreateFolderQueryWithOptions(QueryOptions) method and specify Deep as the value of the FolderDepth property of the QueryOptions object.
Method | Create a shallow query that only returns subfolders from the current folder | Create a deep query that returns all nested subfolders |
---|---|---|
GetFoldersAsync() | Default behavior of this method. | N/A |
GetFoldersAsync(CommonFileQuery) | Specify the DefaultQuery option. | For a library folder, specify an option other than DefaultQuery. |
GetFoldersAsync(CommonFileQuery, UInt32, UInt32) | Specify the DefaultQuery option. | For a library folder, specify an option other than DefaultQuery. |
CreateFolderQuery() | Default behavior of this method. | N/A |
CreateFolderQuery(CommonFileQuery) | Specify the DefaultQuery option. | For a library folder, specify an option other than DefaultQuery. |
CreateFolderQueryWithOptions(QueryOptions) |
|
|
Gets the subfolders in the current folder. When the value of the query argument is something other than CommonFolderQuery.DefaultQuery, gets a list of virtual folders that represent containers for groups of files in the subfolders of the current folder. Files are grouped into folders based on the specified value from the CommonFolderQuery enumeration.
public IAsyncOperation<IReadOnlyList<StorageFolder>> GetFoldersAsync(CommonFolderQuery query)
One of the enumeration values that specifies how to group the files into folders and determines whether the query is shallow or deep.
When this method completes successfully, it returns a list of subfolders. When the value of the query argument is something other than CommonFolderQuery.DefaultQuery, this method returns a list of virtual folders that represent containers for groups of files in the subfolders of the current folder. (Files from the current folder are not included.) The files are grouped as specified by query. The list is of type IReadOnlyList<StorageFolder >. Each folder in the list is represented by a StorageFolder object.
You don't have permission to access the contents of the current folder.
Gets an index-based range of folders from the list of all subfolders in the current folder. When the value of the query argument is something other than CommonFolderQuery.DefaultQuery, gets a list of virtual folders that represent containers for groups of files in the subfolders of the current folder. Files are grouped into folders based on the specified value from the CommonFolderQuery enumeration.
public IAsyncOperation<IReadOnlyList<StorageFolder>> GetFoldersAsync(CommonFolderQuery query, UInt32 startIndex, UInt32 maxItemsToRetrieve)
One of the enumeration values that specifies how to group the files into folders and determines whether the query is shallow or deep.
The zero-based index of the first folder in the range to retrieve.
The maximum number of folders to retrieve.
When this method completes successfully, it returns a list of subfolders. When the value of the query argument is something other than CommonFolderQuery.DefaultQuery, this method returns a list of virtual folders that represent containers for groups of files in the subfolders of the current folder. (Files from the current folder are not included.) The files are grouped as specified by query. The list is of type IReadOnlyList<StorageFolder >. Each folder in the list is represented by a StorageFolder object.
You don't have permission to access the contents of the current folder.
[스크랩] [C#] 람다식 작성 (0) | 2018.05.30 |
---|---|
[.NET] ListView와 ListBox의 차이점 (0) | 2018.05.30 |
[UWP] Uploading a file using Windows.Web.Http from UWP app to a WebAPI web service (0) | 2018.05.26 |
[UWP] 선택기를 사용하여 파일 및 폴더 열기 (0) | 2018.05.26 |
[UWP] 파일 만들기, 쓰기 및 읽기 (0) | 2018.05.26 |