码迷,mamicode.com
首页 > 其他好文 > 详细

listview+contentMenuStrip

时间:2015-03-19 14:31:07      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

 

namespace WindowsFormsApp
{
    public partial class listview : Form
    {
        private  void func()
        {
            listView1.Items.Clear();
            string sql = string.Format("select StudentId,LoginId,StudentName,UserStateId,Sex from Student where LoginId like ‘%{0}%‘", textBox1.Text);
            string connStr = DBHelp.connStr;
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                SqlCommand cmd = new SqlCommand(sql, conn);
                conn.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    ListViewItem item = new ListViewItem(dr["LoginId"].ToString());
                    string active = dr[3].ToString() == "1" ? "活动" : "非活动";
                    item.Tag = dr["StudentId"].ToString();
                    item.SubItems.AddRange(new string[] { dr["StudentName"].ToString(), active, dr["Sex"].ToString() });
                    listView1.Items.Add(item);
                }
                dr.Close();
            }
        }

        private void SetActive(int state)
        {

            int id = Convert.ToInt32(listView1.SelectedItems[0].Tag);
            string connStr = DBHelp.connStr;
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                string sql = string.Format("update Student set UserStateId=@state where StudentId=@id");
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@id", id);
                cmd.Parameters.AddWithValue("@state", state);
                conn.Open();
                cmd.ExecuteNonQuery();
            }
        }

        private void SetDelete()
        {
            int id = Convert.ToInt32(listView1.SelectedItems[0].Tag);
            string connStr = DBHelp.connStr;
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                string sql = string.Format("delete from Student where StudentId=@id");
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@id", id);
                conn.Open();
                cmd.ExecuteNonQuery();
            }
        }
        public listview()
        {
            InitializeComponent();
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void listview_Load(object sender, EventArgs e)
        {
            func();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            func();
        }

        private void 活动ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int state = 1;
            SetActive(state);
            func();
        }

        private void 非活动ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int state = 0;
            SetActive(state);
            func();
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SetDelete();
            func();
        }
    }
}

 

listview+contentMenuStrip

标签:

原文地址:http://www.cnblogs.com/Penfield/p/4350244.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!