ListView 修改SubItem 项
ListView 修改选择项目,必须判断是否选中,否则会出现异常。
if (listView1.SelectedItems.Count > 0) { txtID.Text = listView1.SelectedItems[0].SubItems[0].Text; txtName.Text = listView1.SelectedItems[0].SubItems[1].Text; txtPrice.Text = listView1.SelectedItems[0].SubItems[2].Text; }
全文代码:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace AttachDataBaseDatomuToListView { public partial class Form1 : Form { public Form1() { InitializeComponent(); btnSearch.Click += BtnSearch_Click; btnCancel.Click += BtnCancel_Click; btnModfy.Click += BtnModfy_Click; listView1.SelectedIndexChanged += ListView1_SelectedIndexChanged; this.Load += Form1_Load; } private void BtnModfy_Click(object sender, EventArgs e) { SqlConnection connection; string strUpdate = "Update tb_02 set Approp='" + txtPrice.Text.Trim() + "' where ID='" + txtID.Text.Trim() + "'"; using (connection = new SqlConnection("server=.;pwd=test;user=sa;database=db_02")) { connection.Open(); SqlCommand command = new SqlCommand(strUpdate, connection); if (command.ExecuteNonQuery() > 0) { MessageBox.Show(string.Format("编码为:{0}的产品价格已经更新为{1}", txtID.Text.Trim(), txtPrice.Text.Trim())); } } ListViewDisplayDate(listView1); } private void ListView1_SelectedIndexChanged(object sender, EventArgs e) { if (listView1.SelectedItems.Count > 0) { txtID.Text = listView1.SelectedItems[0].SubItems[0].Text; txtName.Text = listView1.SelectedItems[0].SubItems[1].Text; txtPrice.Text = listView1.SelectedItems[0].SubItems[2].Text; } } private void Form1_Load(object sender, EventArgs e) { listView1.SmallImageList = imageList1; listView1.View = View.Details; //listView1.FullRowSelect = true; //listView1.LabelEdit = true; ColumnHeader columnHeader1 = new ColumnHeader(); columnHeader1.TextAlign = HorizontalAlignment.Center; columnHeader1.Text = "商品编码"; columnHeader1.Width = 100; ColumnHeader columnHeader2 = new ColumnHeader(); columnHeader2.TextAlign = HorizontalAlignment.Center; columnHeader2.Text = "商品名称"; columnHeader2.Width = 120; ColumnHeader columnHeader3 = new ColumnHeader(); columnHeader3.TextAlign = HorizontalAlignment.Center; columnHeader3.Text = "商品价格"; columnHeader3.Width = 100; listView1.Columns.Add(columnHeader1); listView1.Columns.Add(columnHeader2); listView1.Columns.Add(columnHeader3); } private void BtnCancel_Click(object sender, EventArgs e) { this.Close(); Application.Exit(); } private void BtnSearch_Click(object sender, EventArgs e) { ListViewDisplayDate(listView1); } private void ListViewDisplayDate(ListView listView) { int imageIndex = 0; listView.Items.Clear(); SqlConnection connection = new SqlConnection("server=.;pwd=test;user=sa;database=db_02"); connection.Open(); SqlCommand command = new SqlCommand("select * from tb_02", connection); SqlDataReader reader = command.ExecuteReader(); //listView1.View = View.Details; //listView1.FullRowSelect = true; //listView1.BeginUpdate(); while (reader.Read()) { ListViewItem viewItem = new ListViewItem(reader[0].ToString()); viewItem.ImageIndex = imageIndex; viewItem.SubItems.Add(reader[1].ToString()); viewItem.SubItems.Add(reader[2].ToString()); listView.Items.Add(viewItem); imageIndex++; } reader.Close(); connection.Close(); } } }
效果: