ListviewControls can directly view data information, making operations easier. This articleArticleWill tell youC #InListviewData Binding controlsListviewA novice friend of the control is helpful.
In Listview Add a header
Add a header to the control edit Column
// Header style Listview1.gridlines = True ; Listview1.fullrowselect = True ; Listview1.view = View. Details; listview1.scrollable = False ; Listview1.multiselect = False ; Listview1.headerstyle = Columnheaderstyle. clickable; // Method 1 // Declare title and create object Columnheader CH = New Columnheader (); // Title Ch. Text = " Countries participating in the Olympic Games " ; // Column width. The default value is 60. Ch. width = 60 ; // Display text alignment Ch. textalign = horizontalalignment. Center;
// Add title to listview This . Listview1.columns. Add (CH ); // Follow-up Columnheader againch... // Method 2
Listview1.columns. Add ( " ID " , 50 , Horizontalalignment. center );
Listview1.columns. Add ( " Country " , 50 , Horizontalalignment. center );
Listview1.columns. Add ( " Gold Medal " , 50 , Horizontalalignment. center); listview1.columns. Add ( " Silver Medal " , 50 , Horizontalalignment. center); listview1.columns. Add ( " Bronze Medal " , 60 , Horizontalalignment. center );
Listview Control Data Binding
UseForeachTraversalDTOrDSAnd then bindIstview.
Private Void Binddatatolistview (){ // Clear old data This . Listview1.items. Clear (); dataset DS = Data (database) // Determine if DS has data If (Ds! = Null & Ds. Tables. Count> 0 & Ds. Tables [ 0 ]. Rows. Count> 0 ){ Foreach (Datarow Dr In DS. Tables [ 0 ]. Rows ){ Int Sor = convert. toint32 (Dr [ " ID " ]. Tostring ()); String Strnation = Dr [ " Nation " ]. Tostring (); String Strgoldmedal = Dr [ " Goldmedal " ]. Tostring (); String Strsilvermedal = Dr [ " Silvermedal " ]. Tostring (); String Strbronzemedal = Dr [ " Bronzemedal " ]. Tostring (); listviewitem ivitem = New Listviewitem (); // ID Ivitem. Text = Sor. tostring (); ivitem. Tag = Dr; // Country Ivitem. subitems. Add (strnation. tostring ()); // Gold Medal Ivitem. subitems. Add (strgoldmedal. tostring ()); // Silver Medal Ivitem. subitems. Add (strsilvermedal. tostring ()); // Bronze Medal Ivitem. subitems. Add (strbronzemedal. tostring ()); This . Listview1.items. Add (ivitem );}}}
Obtain the current click.ListviewThe row data is displayed as follows:
Private Void Listviewmediaclick (Object Sender, eventargs e ){ If (Listview1.items. Count <= 0 ) Return ; If (Listview1.selecteditems. Count <= 0 ) Return ; Int Index = This . Listview1.selecteditems [ 0 ]. Index; If (Index < 0 ) Return ; Datarow Dr = (Datarow )( This . Listview1.items [Index]. Tag); MessageBox. Show (Dr [ " Nation " ]. Tostring () + Dr [ "Goldmedal" ]. Tostring ());
MessageBox. Show ( This . Listview1.selecteditems [ 0 ]. Subitems [ 1 ]. Text. tostring ());
// Method 2
MessageBox. Show (listview1.selecteditems [ 0 ]. Subitems [ 0 ]. Text );
MessageBox. Show (listview1.selecteditems [ 0 ]. Subitems [ 1 ]. Text );
MessageBox. Show (listview1.selecteditems [ 0 ]. Subitems [ 2 ]. Text );
// Listviewitem IV = new listviewitem (Ds. Tables [0]. Rows [1] [1]. tostring ());
// Iv. Tag = Ds. Tables [0]. Rows [1] [1]. tostring (); /// /Read hidden Columns
// This. listview1.selecteditems [0]. Tag. tostring ();
} Hide Columns
There is a solution in the second method above to read hidden columns.
In the second method, Lv = new listviewitem (Ds. Tables [0]. Rows [I] [1]. tostring (); MeansListviewThe first row of data information in,
LV. Tag = Ds. Tables [0]. Rows [I] [0]. tostring (); // represents a hidden column.
When reading hidden columns, you can use the followingCodeTo obtain the value of a hidden column.
//This. listview1.selecteditems [0]. Tag. tostring ();