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());
}
}
}
}
windef12
最新推荐文章于 2021-02-26 21:19:39 发布