This is the 17th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

preface

In the previous article, the blogger wrote an article about the simple processing of ListView control, data processing limitations, for this control we are mostly connected to the database for use, most of our data is dynamic, so we simply add data is limited, So this article uses a database to bind the ListView control, which is an extension of our last article!! Hey hey, if you can’t, you can comment or ask the blogger in a private message

Once a day to prevent decadence

(We have to hold on for ourselves, no matter how difficult it is)

1. Open the project of our last article and delete the static data. We use database processing for those of you who can’t read this article

2. Use the database to perform operations on ListView data

2.1 Double-click the window to automatically generate functions

2.2. Create the database we need and write the values

The blogger is using SQL Server and for those of you who don’t have a database like this, you can use MySQL

2.3. Use code to establish a connection between ListView control and database and bind data

2.4. Use database to perform data binding query effect display on LIstVIew control

3. Use the dialog box to create a search query against the database

4. Add a form to create a file

4.1 Creating a conTextMenuStrip control by right-clicking

4.2 Realize the jump between forms

Create a new form file, drag the style, and instantiate a form 2 in the function you want to jump to, using Show.

4.3 “New File” window code processing

5. Overall code display and effect drawing

5.1 Form 1 Core code:

private void Form1_Load(object sender, EventArgs e) { string connString = "server=.; database=mychat; User ID = sa; Pwd=123456"; MySQL > open SQL server database; MySQL > open SQL server database SqlConnection conn = new SqlConnection(connString); Conn.open (); / / open the database string SQL = "select name, type, size, date, Imageid from listview"; SqlCommand CMD = new SqlCommand(SQL, conn); SqlDataReader reader = cmd.executereader (); listView1.Items.Clear(); {ListViewItem item = new ListViewItem(reader[0].toString ().trim ()); ListViewItem item = new ListViewItem(reader[0].toString ().trim ()); ImageIndex = convert.toint32 (reader[4]); //Trim() clears space item.imageIndex = convert.toint32 (reader[4]); Id item.subitems.add (Reader [1].toString ().trim ()); // Data binding item.subitems.add (reader[2].toString ().trim ()); item.SubItems.Add(reader[3].ToString().Trim()); this.listView1.Items.Add(item); } reader.Close(); conn.Close(); } private void button4_Click(object sender, EventArgs e) { string connString = "server=.; database=mychat; User ID = sa; Pwd=123456"; SqlConnection conn = new SqlConnection(connString); conn.Open(); string sql =string.Format("select name,type,size,date,Imageid from listview where name like '%{0}%';" , textBox1.Text); SqlCommand CMD = new SqlCommand(SQL, conn); SqlDataReader reader = cmd.ExecuteReader(); listView1.Items.Clear(); ListViewItem item = new ListViewItem(reader[0].toString ().trim ()); ListViewItem item = new ListViewItem(reader[0].toString ().trim ()); item.ImageIndex =Convert.ToInt32(reader[4]); item.SubItems.Add(reader[1].ToString().Trim()); item.SubItems.Add(reader[2].ToString().Trim()); item.SubItems.Add(reader[3].ToString().Trim()); this.listView1.Items.Add(item); } reader.Close(); conn.Close(); } private void createFile ToolStripMenuItem_Click(object sender, EventArgs e) {Form2 Form2 = new Form2(); form2.Show(); } private void listView1_SelectedIndexChanged(object sender, EventArgs e) { }Copy the code

5.2 Form 2 Core Code:

Private void button2_Click(Object Sender, EventArgs e)// Reset button {textBox1.clear (); // Clear textBox 2.clear (); textBox3.Clear(); textBox4.Clear(); textBox5.Clear(); } private void button1_Click(Object sender, EventArgs e) {string connString = "server=. database=mychat; User ID = sa; Pwd=123456"; SqlConnection conn = new SqlConnection(connString); conn.Open(); string sql = string.Format("INSERT INTO listview(name, type, size, date, Imageid) VALUES('{0}','{1}','{2}','{3}',{4});" ,textBox1.Text, textBox2.Text, textBox3.Text+"MB", textBox4.Text,Convert.ToInt32(textBox5.Text)); SqlCommand CMD = new SqlCommand(SQL, conn); int x =cmd.ExecuteNonQuery(); If (x > 0) {MessageBox.Show(" add successfully "); this.Close(); } else MessageBox.Show(" Failed to add "); }}Copy the code

conclusion

Blogger is roughly explain the operation of the controls to delete and add the same, we can add a delete button at the back, by the name to delete, it is ok to add a function, bloggers think it’s very simple, there is no more processing, for a beginner, or able to read this article may connect to the database for beginners is not friendly, But as long as you study carefully, follow the blogger’s logic, or understandable, blogger this article is based on the last article on the basis of the extension. Well, creation is not easy, click on the likes to pay attention to the comment collection !!!!