Asp tutorial. net listview item control instance
The following figure shows the design of the form. A LIstView control is placed on the form, an ImageList (ImageSize (50, 40) control is placed, and the largeImageList attribute of listView is selected as ImageList, then, place a TabControl control.
When I Load the form, I run the following code to add an item to the listview:
For (int I = 0; I <14; I ++)
{
ListViewItem lvi = new ListViewItem ();
Lvi. Text = DateTime. Now. AddDays (I). ToString ("MM dd ");
Lvi. ToolTipText = dt initial display time. AddDays (I). ToString ("yyyy-MM-dd ");
Lsv reservation status. Items. Add (lvi );
}
This is the re-drawing code.
Private void listviewincludrawitem (object sender, DrawListViewItemEventArgs e)
{
Rectangle rect = e. Bounds;
Rect. Width = rect. Width-3;
If (e. State & ListViewItemStates. Selected )! = 0)
{
// Change the selected background color
E. Graphics. FillRectangle (new SolidBrush (Color. FromArgb (215,232,252), rect );
// Draw a border
E. Graphics. DrawRectangle (new Pen (Color. FromArgb (125,162,206), rect );
}
Else
{
// Default background color
Using (SolidBrush bBrush = new SolidBrush (Color. White ))
{
E. Graphics. FillRectangle (bBrush, rect );
// Draw a border
E. Graphics. DrawRectangle (new Pen (Color. FromArgb (125,162,206), rect );
}
}
If (ListView) sender). View! = View. Details)
{
Font ft = new System. Drawing. Font (FontFamily. GenericSansSerif, 9 );
SolidBrush fontbrush = new SolidBrush (Color. Blue );
StringFormat Format = new StringFormat ();
Format. Alignment = StringAlignment. Center;
E. graphics. drawString (e. item. text, ft, fontbrush, new RectangleF (rect. left, rect. top + 10, rect. width, rect. height), Format );
Ft. Dispose ();
Fontbrush. Dispose ();
}
}
// Learning ListView usage
<Asp: ListView runat = "server"
ID = "_ simpleTableListView"
Performanceid = "_ moviesDataSource">
<LayoutTemplate>
<Ul>
<Asp: PlaceHolder runat = "server"
ID = "itemPlaceholder"/>
</Ul>
</LayoutTemplate>
<ItemTemplate>
<Li> <% # Eval ("title") %>,
<% # Eval ("release_date", "{0: d}") %> </li>
</ItemTemplate>
</Asp: ListView>
// Check the complete listview instance again.
Public frm_Manage ()
{
InitializeComponent ();
String ConnectionString = "Data Source = dzjc_2005; user = kk; password = kk;"; // write the connection string
OracleConnection conn = new OracleConnection (ConnectionString); // create a new connection
Conn. Open ();
OracleCommand cmd = conn. CreateCommand ();
Cmd. CommandText = "select u_name from netvideo. n_user"; // write an SQL statement here
OracleDataReader dr = cmd. ExecuteReader (); // create an OracleDateReader object
ListView1.GridLines = true; // display the separation line between the row and the row
ListView1.FullRowSelect = true; // the row to be selected
ListView1.View = View. Details; // defines how the list is displayed.
ListView1.Scrollable = true; // display the scroll bar when necessary
ListView1.MultiSelect = false; // you cannot select multiple rows.
ListView1.HeaderStyle = ColumnHeaderStyle. Clickable;
// Create an appropriate display header for the field name of the database tutorial
ListView1.Columns. Add ("Operator name", 282, HorizontalAlignment. Center); // The first parameter, header name, 2nd parameters, header size, 3rd parameters, Style
ListView1.Visible = true; // lstView visible
While (dr. Read ())
{
ListViewItem Item = new ListViewItem ();
Item. SubItems. Clear ();
Item. SubItems [0]. Text = dr ["u_name"]. ToString (); // read fields from the database
ListView1.Items. Add (Item); // display
}
}