DataGridView,TreeView(增删查改)

DataGridView,TreeViewusing System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Data.SqlClient;using System.Drawing;using System.Linq;using Syste...
摘要由CSDN通过智能技术生成
① DataGridView(增删查改)

在这里插入图片描述

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 Frm_Course_Load(object sender, EventArgs e)
        {
   
            DataBind();
        }

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

        private void btn_Save_Click(object sender, EventArgs e)
        {
   
            string courseName = txt_CourseName.Text.Trim();
            if (courseName.Length > 0)
            {
   
                Save(courseName);
            }
            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();
            //MessageBox.Show(cid + "......" + cname);
            //②
            //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();
            MessageBox.Show(dgv_Course.Rows.Count.ToString());
        }

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

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

        private void Clear()
        {
   
            txt_CourseName.Text = string.Empty;
            cid = string.Empty;
        }


        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, Message
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值