当前位置:网站首页>C # use database to bind listview control data
C # use database to bind listview control data
2022-07-29 08:46:00 【InfoQ】
Preface
Once a day , Prevent decadence

1. Open the project of our last article , Delete static data . We use database processing. Students who can't need to read an article

2. Use database pair ListView Data display operation
2.1 Double click the form interface to automatically generate functions

2.2. Create the database we need and write the value

2.3. Use code to ListView Control establishes a connection with the database and binds data

2.4. Use database pair LIstVIew Control to display the effect of data binding query

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

4. Add a form to realize the operation of creating a new file
4.1 Use conTextMenuStrip Control, right-click to create a new

4.2 Realize the jump between forms

4.3 Yes ” New file “ Form code processing

5. Overall code presentation , design sketch

5.1 forms 1 Core code :
private void Form1_Load(object sender, EventArgs e)
{
string connString = "server=.;database=mychat; User ID = sa; Pwd=123456";// Write the database opening statement , Bloggers use SQL server database , If you are MySQL You can see the previous article of the blogger, which explains .
SqlConnection conn = new SqlConnection(connString);// Materialize
conn.Open();// Open database
string sql = "select name,type,size,date,Imageid from listview";// Query statement
SqlCommand cmd = new SqlCommand(sql, conn);// Establishing a connection
SqlDataReader reader = cmd.ExecuteReader();
listView1.Items.Clear();// Empty listView
while (reader.Read())// Query result traversal , And data binding
{
ListViewItem item = new ListViewItem(reader[0].ToString().Trim());//Trim() Clear the space
item.ImageIndex = Convert.ToInt32(reader[4]);// Binding Icon 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);// Implement fuzzy query
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader reader = cmd.ExecuteReader();
listView1.Items.Clear();
while (reader.Read())// The added data is consistent with the above code
{
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 New file ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Show();
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
5.2 Form 2 core code :
private void button2_Click(object sender, EventArgs e)// Reset button
{
textBox1.Clear();// Yes textbox Empty
textBox2.Clear();
textBox3.Clear();
textBox4.Clear();
textBox5.Clear();
}
private void button1_Click(object sender, EventArgs e)// Add a button
{
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));// Add data statement
SqlCommand cmd = new SqlCommand(sql, conn);
int x =cmd.ExecuteNonQuery();// Add operation , Successfully returned the number of data you added , Otherwise 0
if (x > 0)
{
MessageBox.Show(" Add success ");
this.Close();// Add success , Close this form
}
else
MessageBox.Show(" Add failure ");
}
}
summary

边栏推荐
- Osgsimplegl3 combined with renderdoc tool
- Day4: the establishment of MySQL database and its simplicity and practicality
- Osgsimplegl3 example analysis
- Simple operation of SQL server data table
- Information system project manager must recite the quality grade of the core examination site (53)
- Leetcode Hot 100 (brush question 9) (301/45/517/407/offer62/mst08.14/)
- Common query optimization technology of data Lake - "deepnova developer community"
- Use disco diffusion to generate AI artwork in moment pool cloud
- Squareline partners with visual GUI development of oneos graphical components
- Crawling JS encrypted data of playwright actual combat case
猜你喜欢

2022年山东省安全员C证上岗证题库及答案

C language function output I love you

MySQL statement mind map

Analysis of zorder sampling partition process in Hudi - "deepnova developer community"

Day6: using PHP to write landing pages

2022电工(初级)考题模拟考试平台操作

预训练模型与传统方法在排序上有啥不同?

(视频+图文)机器学习入门系列-第3章 逻辑回归

2022 P cylinder filling test simulation 100 questions simulation test platform operation

Ar virtual augmentation and reality
随机推荐
QT learning: use non TS files such as json/xml to realize multilingual internationalization
(视频+图文)机器学习入门系列-第2章 线性回归
Basic shell operations (Part 2)
Mathematical modeling - Differential Equations
Third week weekly report resnet+resnext
Fastjson's tojsonstring() source code analysis for special processing of time classes - "deepnova developer community"
Week 1 task deep learning and pytorch Foundation
Day4: SQL server is easy to use
Sword finger offer 26. substructure of tree
Collation of ml.net related resources
Day13: file upload vulnerability
信息系统项目管理师必背核心考点(五十三)质量等级
Markdown concise grammar manual
Analysis of zorder sampling partition process in Hudi - "deepnova developer community"
7.3-function-templates
Thrift installation manual
6.3 references
(Video + graphic) introduction series to machine learning - Chapter 2 linear regression
LeetCode力扣题目总结(题目编号:53、3、141、面试题022、剑指offer链表中环的入口节点、20、19、牛客NC1、103、1143、牛客127)
Use disco diffusion to generate AI artwork in moment pool cloud