windef12

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Students
{
    public partial class Frm_Course : Form
    {
        string cid = string.Empty;
        public Frm_Course()
        {
            InitializeComponent();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void Frm_Course_Load(object sender, EventArgs e)
        {
            DataBind();

        }
        private void btn_Query_Click(object sender, EventArgs e)
        {
            DataBind();
        }

        string url = "server=localhost;database=Student;uid=sa;pwd=123";

        private void Save(string cName)
        {
            using (SqlConnection con = new SqlConnection(url))
            {
                con.Open();
                string sql = string.Empty;
                //修改
                if (cid.Length > 0)
                {
                    sql = "update t_course set cname=@cname where cid=@cid";

                }
                else
                {//增加
                    sql = "insert into t_course values(NEWID(),@cname)";
                }
                SqlCommand com = new SqlCommand(sql, con);
                com.Parameters.Add("@cname", cName);
                if (cid.Length > 0)
                {
                    com.Parameters.Add("@cid", cid);
                }
                int index = com.ExecuteNonQuery();
                if (index > 0)
                {
                    MessageBox.Show("操作成功", "成功提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    DataBind();
                    Clear();
                }
                else
                {
                    MessageBox.Show("操作失败", "失败提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void DataBind()
        {
            string query = Name = txt_QueryName.Text.Trim();

            using (SqlConnection con = new SqlConnection(url))
            {
                con.Open();
                StringBuilder sb = new StringBuilder();
                sb.Append("select * from t_course ");
                if (query.Length > 0)
                {
                    sb.Append("where cname like '%" + query + "%'");
                }
                SqlCommand com = new SqlCommand(sb.ToString(), con);
                SqlDataAdapter sda = new SqlDataAdapter(com);
                DataSet ds = new DataSet();
                sda.Fill(ds);
                dgv_Course.DataSource = ds.Tables[0];
            }
        }

        private void btn_Save_Click(object sender, EventArgs e)
        {
            string name = txt_CourseName.Text.Trim();
            if (name.Length > 0)
            {
                Save(name);
            }
            else
            {
                MessageBox.Show("请输入课程的名称", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void dgv_Course_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //int index = dgv_Course.CurrentRow.Index;
            //string cid = dgv_Course.Rows[index].Cells[0].Value.ToString();
            //string cname = dgv_Course.Rows[index].Cells[1].Value.ToString();

            //string cid = dgv_Course.SelectedRows[0].Cells[0].Value.ToString();
            //string cname = dgv_Course.SelectedRows[0].Cells[1].Value.ToString();

            //string cid = dgv_Course.CurrentRow.Cells[0].Value.ToString();
            //string cname = dgv_Course.CurrentRow.Cells[1].Value.ToString();
            //MessageBox.Show(cid + "----" + cname);

            cid = dgv_Course.CurrentRow.Cells[1].Value.ToString();
            txt_CourseName.Text = dgv_Course.CurrentRow.Cells[2].Value.ToString();


        }

        private void dgv_Course_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            dgv_Course.ClearSelection();
        }

        private void btn_Remove_Click(object sender, EventArgs e)
        {
            if (cid.Length > 0)
            {
                DialogResult dr = MessageBox.Show("你确定要删除当前选择的行吗", "操作提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (dr == DialogResult.OK)
                {
                    using (SqlConnection conn = new SqlConnection(url))
                    {
                        conn.Open();
                        SqlCommand comm = new SqlCommand("delete from t_course where cid=@cid", conn);
                        comm.Parameters.Add("cid", cid);
                        int index = comm.ExecuteNonQuery();
                        if (index > 0)
                        {
                            MessageBox.Show("操作成功", "成功提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            DataBind();
                            Clear();
                        }
                        else
                        {
                            MessageBox.Show("操作失败", "失败提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                }
            }
            else
            {
                MessageBox.Show("请选择你要删除的行的记录", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        private void Clear()
        {
            txt_CourseName.Text = string.Empty;
            cid = string.Empty;
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            Clear();
        }

        private void dgv_Sourse_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
           if(dgv_Course.Columns[e.ColumnIndex].Name.Equals("col_Select"))
            {
                MessageBox.Show( dgv_Course.CurrentRow.Cells[2].Value.ToString());
            }
        }
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值