基于MS_SQL数据库的各种数据控件的数据绑定(WinForm)
一些基本的功能都已实现 下载链接:dataGridView数据绑定练习
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace DataGridViewTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int Fcellvalue; //用于保存主键的值
SqlDataAdapter da;
SqlConnection conn;
SqlCommand cmd = new SqlCommand();
string connstr = "Data Source = .;database=Test;uid=sa;pwd=123;";
private void Form1_Load(object sender, EventArgs e)
{
Getdate();
dataGridView1.Columns[0].Visible = false;
dataGridView1.Columns[1].HeaderText = "姓名";
dataGridView1.Columns[2].HeaderText = "性别";
dataGridView1.Columns[3].HeaderText = "民族";
dataGridView1.Columns[4].HeaderText = "公司";
dataGridView1.Columns[5].HeaderText = "职位";
dataGridView1.Columns[6].HeaderText = "地址";
}
private void timer1_Tick(object sender, EventArgs e)
{
toolStripStatusLabel5.Text ="当前时间:"+ DateTime.Now.ToString();
}
//绑定数据
public void Getdate()
{
try
{
conn = new SqlConnection(connstr);
conn.Open();
string strcmdsel = "select * from T1";
da = new SqlDataAdapter(strcmdsel, connstr);
DataSet ds = new DataSet();
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0].DefaultView;
}
catch (SqlException sql)
{
MessageBox.Show(sql.ToString());
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
conn.Close();
}
}
//刷新数据
private void button5_Click(object sender, EventArgs e)
{
Getdate();
}
//单击行标题的时候 取到这一行第一列的值 即主键的值
private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
Fcellvalue = Convert.ToInt32(dataGridView1.CurrentRow.Cells[0].Value); //取到主键
}
//查询满足条件的数据
private void button1_Click(object sender, EventArgs e)
{
try
{
if (textBox1.Text.Trim() == "")
{
MessageBox.Show("请输入查询条件!!!");
}
else
{
conn = new SqlConnection(connstr);
conn.Open();
string selectLevel = "select * from T1 where LevelInfo='" + textBox1.Text.Trim() + "'";
da = new SqlDataAdapter(selectLevel, connstr);
DataSet ds = new DataSet();
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0].DefaultView;
}
}
catch (SqlException sql)
{
MessageBox.Show(sql.ToString());
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
conn.Close();
}
textBox1.Text = "";
}
//删除选中行
private void button2_Click(object sender, EventArgs e)
{
try
{
conn = new SqlConnection(connstr);
conn.Open();
string strcmddel = "delete from T1 where ID=" + Fcellvalue;
cmd.Connection = conn;
cmd.CommandText = strcmddel;
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
Getdate();
}
catch (SqlException sql)
{
MessageBox.Show(sql.ToString());
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
conn.Close();
}
}
//新增一行数据
private void button3_Click(object sender, EventArgs e)
{
try
{
conn = new SqlConnection(connstr);
conn.Open();
string F = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[1].Value.ToString();
string S = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[2].Value.ToString();
string T = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[3].Value.ToString();
string Four = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[4].Value.ToString();
string Five = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[5].Value.ToString();
string Six = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[6].Value.ToString();
string strcmdIn = " insert into T1 values('" + F + "','" + S + "','" + T + "','" + Four + "','" + Five + "','" + Six + "')";
cmd.Connection = conn;
cmd.CommandText = strcmdIn;
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
Getdate();
}
catch (SqlException sql)
{
MessageBox.Show(sql.ToString());
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
conn.Close();
}
}
//修改数据项
private void button4_Click(object sender, EventArgs e)
{
try
{
conn = new SqlConnection(connstr);
conn.Open();
string F = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[1].Value.ToString();
string S = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[2].Value.ToString();
string T = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[3].Value.ToString();
string Four = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[4].Value.ToString();
string Five = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[5].Value.ToString();
string Six = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[6].Value.ToString();
string strcmdUpdate = " update T1 set Name='" + F + "',Sex='" + S + "'," +
" Minzu='" + T + "',Com='" + Four + "',LevelInfo='" + Five + "',Address='" + Six + "' where ID = " + dataGridView1.CurrentRow.Cells[0].Value;
cmd.Connection = conn;
cmd.CommandText = strcmdUpdate;
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
Getdate();
}
catch (SqlException sql)
{
MessageBox.Show(sql.ToString());
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
conn.Close();
}
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex == -1)
return;
dataGridView1.Rows[e.RowIndex].Cells[1].Style.ForeColor = Color.Red;
dataGridView1.Rows[e.RowIndex].Cells[2].Style.ForeColor = Color.Red;
dataGridView1.Rows[e.RowIndex].Cells[3].Style.ForeColor = Color.Red;
dataGridView1.Rows[e.RowIndex].Cells[4].Style.ForeColor = Color.Red;
dataGridView1.Rows[e.RowIndex].Cells[5].Style.ForeColor = Color.Red;
dataGridView1.Rows[e.RowIndex].Cells[6].Style.ForeColor = Color.Red;
}
private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex == -1)
return;
dataGridView1.Rows[e.RowIndex].Cells[1].Style.ForeColor = Color.Black;
dataGridView1.Rows[e.RowIndex].Cells[2].Style.ForeColor = Color.Black;
dataGridView1.Rows[e.RowIndex].Cells[3].Style.ForeColor = Color.Black;
dataGridView1.Rows[e.RowIndex].Cells[4].Style.ForeColor = Color.Black;
dataGridView1.Rows[e.RowIndex].Cells[5].Style.ForeColor = Color.Black;
dataGridView1.Rows[e.RowIndex].Cells[6].Style.ForeColor = Color.Black;
}
}
}
种一棵树最好的时间是十年前,其次是现在.

浙公网安备 33010602011771号