WinForm (ListView and Database connection)

Source: Internet
Author: User

One, ListView: List display data
1. View-
At the top right of the small arrow click to change the view to Largelcon; or the right-click property in the Appearance view to change it to details
2. Set column Header-
At the top right of the small arrow Click Select Edit Column and then add column name; Right-click Properties-Behavior-columns
3, add row data-
At the top right of the small arrow Click Select Edit Item, and then rename the property text, that is, the first column, add members as other columns; or right-click Properties-Behavior-items
-----------------------------------------------------------------------------
Second, connect the database:
1, establish entity class student, subject and data access Class Studentdata, Subjectdata
2. Call the Select method in the Studentdata class to remove all data

New Studentdata (). Select ();


3. Import data into the ListView

foreach inch  new= S.code;li. SubItems.Add (s.name); Li. SubItems.Add (s.sexstr); Li. SubItems.Add (s.birstr); Li. SubItems.Add (S.subjectname); ListView1.Items.Add (li);}

4. Automatically bind the data in the ListView after the form is loaded
Workaround: The data binding code is written in the form's constructor
5. Show the final data to the user (attribute extension)
6. Line number
Use C # variable loop + + to implement
7. Data duplication Binding
Empty the Items collection before binding
ListView1.Items.Clear ();
8. Select a row of data
Appearance-Fullrowselect: Select whether an entire row is selected for one of the items
Checkboxes: check box
GridLines: Grid lines
Behavior-hideselection: Whether to remove the highlight of the selected item when the control has no focus
HeaderStyle: Style of the list header
Hoverselection: Mouse hover on the item selection, not good, need to pause a while, usually with checkboxes
Focus:
1, how the data bound up

2. How to remove the selected data
One is the Fullrowselect property is true to select the entire row of data (Listview1.selecteditems)

if(ListView1.SelectedItems.Count >1)//multiple lines are selected{MessageBox.Show ("Modify allows only one row to be selected");}Else if(ListView1.SelectedItems.Count = =1){foreach(ListViewItem LiinchListview1.selecteditems) {if(li. Selected) {MessageBox.Show (li. subitems[0]. Text + li. subitems[1]. Text + li. subitems[2]. Text + li. subitems[3]. Text + li. subitems[4]. Text + li. subitems[5]. Text);}}}
else//not selected
{
MessageBox.Show ("Please check the data you want to modify first");
}


The other is when the Checkboxes property is true, you can select the entire row of data (Listview1.checkeditems) by check box

foreach inch Listview1.checkeditems) {if  (li. Checked) {MessageBox.Show (li. subitems[0]. Text + li. subitems[1]. Text + li. subitems[2]. Text + li. subitems[3]. Text + li. subitems[4]. Text + li. subitems[5]. Text);}}

Delete Code section:

Delete limit:
1. You cannot choose
2, a number of data if there is not deleted, hint, delete the success of a few, unsuccessful several, unsuccessful is the few

Private voidButton2_Click (Objectsender, EventArgs e) {            intDelcount =0;//total number of bars to delete            intCount =0;//number of bars that have been deleted            stringNotdelete =""; if(ListView1.CheckedItems.Count <=0) {MessageBox.Show ("Please select the data you want to delete first"); }            Else{Delcount=ListView1.CheckedItems.Count; foreach(ListViewItem Liinchlistview1.checkeditems) {if(li. Checked) {BOOLOK =NewStudentdata (). Delete (li. subitems[1].                      Text); if(OK) {count+=1; }                      Else{notdelete+ = li. subitems[1]. Text +",";                }}} Newmethod (); if(Count = =Delcount) {MessageBox.Show ("Delete succeeded"); }                Else{MessageBox.Show ("Delete failed! To delete"+ Delcount +"bar data, not deleted"+ (Delcount-count) +"data, the user name that was not deleted is:"+ Notdelete +""); }            }         }

To add a limit:

1. The number cannot be empty, and the number cannot be found in the database

2. The name cannot be empty;

   Public Partial classform2:form {Form1 F1=NULL;  PublicForm2 (Form1 F1) {InitializeComponent (); F1=F1; List<Subject> slist =NewSubjectdata ().Select(); Usub.datasource=slist; Usub.valuemember="Subjectcode"; Usub.displaymember="Subjectname"; }        BOOLc =false;//determine if a number exists        Private voidtextBox1_TextChanged (Objectsender, EventArgs e) {Student s=NewStudentdata ().Select(UCode.Text.Trim ());//calling the query method of the Studentdata class            if(s = =NULL) {Code1. Text=""; C=false; }            Else{code1. Text="this number already exists! "; C=true; }                    }        BOOLn =false;//determine if the name is empty        Private voidButton1_Click (Objectsender, EventArgs e) {            if(ucode.text=="") {Code1. Text="the number cannot be empty! "; C=true; }                                       if(uname.text=="") {name1. Text="the name cannot be empty"; N=false; }            Else{n=true; Name1. Text=""; } Student S1=NewStudent (); if(c==false&&N) {S1. Code=UCode.Text.Trim (); S1. Name=UName.Text.Trim (); S1. Sex=Ra_true.                Checked; S1. Birthday=BIR.                Value; S1. Subjectcode=Usub.SelectedValue.ToString (); BOOLdo{NewStudentdata ().              Insert (S1); if(bo) {MessageBox.Show ("Add success! "); F1.                  Newmethod ();  This.              Close (); }              Else{MessageBox.Show ("Add failed! "); }            }        }}
Add code Section

WinForm (ListView and Database connection)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.