关于C#中怎样才能实现向DataGridView中填充表的记录?用代码写。。。。。 关于C#中怎样才能实现向DataGridView中填充表的记录?用代码写。。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 try { conn = CreateConn(); if (conn.State == ConnectionState.Closed) { conn.Open(); } cmd = new SqlCommand(sql,conn); //创建DataSet ds = new DataSet(); sda = new SqlDataAdapter(); sda.SelectCommand = cmd; //表名与存储过程名相同 sda.Fill(ds, tablename); } catch (Exception ex) { throw new Exception(ex.Message); } finally { if (conn.State == ConnectionState.Open) { conn.Close(); } } return ds; this.DataGrid1.DataSource=ds; this.DataGrid1.DataBind(); SqlConnection conn; SqlDataAdapter da; DataSet ds=new DataSet(); SqlParameter sp=new SqlParameter(); DialogResult objresult; public Form1() { InitializeComponent(); conn = new SqlConnection("server=127.0.0.1;database=pubs;Integrated Security=SSPI"); conn.Open(); } private void Form1_Load(object sender, EventArgs e) { //ComboBox读取数据库信息 this.comboBox1.Items.Add("请选择科目"); this.comboBox1.SelectedIndex = 0; string sql = "select UserType from userinfo"; da = new SqlDataAdapter(sql, conn); ds = new DataSet(); da.Fill(ds,"userinfo"); for (int i = 0; i < ds.Tables["userinfo"].Rows.Count; i++) { this.comboBox1.Items.Add(ds.Tables[0].Rows[i]["UserType"]); } } //增加 private void button1_Click(object sender, EventArgs e) { //表格形式的添加方法 //string cbo = this.comboBox1.SelectedItem.ToString(); //string inscmd = "insert into UserInfo values('" + this.textBox1.Text + "','"+this.textBox2.Text+"',"+this.textBox3.Text+",'"+cbo+"')"; //cmd = new SqlCommand(inscmd,conn); //try //{ // cmd.ExecuteNonQuery(); // MessageBox.Show("添加成功"); //} //catch (Exception ex) //{ // MessageBox.Show(ex.Message); //} //在网格中添加的方法 string inscmd="insert into UserInfo values(@UserName,@UserPwd,@UserAge,@UserType)"; da.InsertCommand=new SqlCommand(inscmd,conn); //声明参数并指定数据库的列和版本 sp=da.InsertCommand.Parameters.Add("@UserName",SqlDbType.VarChar); sp.SourceColumn="UserName"; sp.SourceVersion=DataRowVersion.Current; sp=da.InsertCommand.Parameters.Add("@UserPwd",SqlDbType.VarChar); sp.SourceColumn="UserPwd"; sp.SourceVersion=DataRowVersion.Current; sp=da.InsertCommand.Parameters.Add("@UserAge",SqlDbType.Int); sp.SourceColumn="UserAge"; sp.SourceVersion=DataRowVersion.Current; sp=da.InsertCommand.Parameters.Add("@UserType",SqlDbType.VarChar); sp.SourceColumn="UserType"; sp.SourceVersion=DataRowVersion.Current; if (ds.HasChanges()) { try { da.Update(ds, "UserInfo"); MessageBox.Show("插入记录成功"); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } //查询 private void button3_Click(object sender, EventArgs e) { try { ds.Clear(); da = new SqlDataAdapter("Select * from UserInfo", conn); da.Fill(ds, "UserInfo"); this.dataGridView1.DataSource = ds.Tables["UserInfo"].DefaultView; } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { conn.Close(); } } //删除 private void button4_Click(object sender, EventArgs e) { string delcmd = "delete from userinfo where UserName=@UserName"; da.DeleteCommand = new SqlCommand(delcmd,conn); sp = da.DeleteCommand.Parameters.Add("@UserName",SqlDbType.VarChar); sp.SourceColumn = "UserName"; sp.SourceVersion = DataRowVersion.Original; //删除警告 objresult = MessageBox.Show("确定要删除当前记录么?","确认",MessageBoxButtons.YesNo,MessageBoxIcon.Warning); if (objresult == DialogResult.Yes) { try { ds.Tables["userinfo"].Rows[dataGridView1.CurrentRow.Index].Delete(); da.Update(ds, "userinfo"); MessageBox.Show("已删除记录"); } catch (Exception ex) { MessageBox.Show(ex.Message); } } else { return; } //如果行数为0,则禁用"删除"和"更新" if (ds.Tables["userinfo"].Rows.Count == 0) { this.button2.Enabled = false; this.button4.Enabled = false; } } //修改 private void button2_Click(object sender, EventArgs e) { string updcmd = "Update UserInfo set UserName=@UserName,UserPwd=@UserPwd,UserAge=@UserAge,UserType=@UserType where UserName=@UserName"; da.UpdateCommand = new SqlCommand(updcmd,conn); sp = da.UpdateCommand.Parameters.Add("@UserName",SqlDbType.VarChar); sp.SourceColumn = "UserName"; sp.SourceVersion = DataRowVersion.Current; sp = da.UpdateCommand.Parameters.Add("@UserPwd", SqlDbType.VarChar); sp.SourceColumn = "UserPwd"; sp.SourceVersion = DataRowVersion.Current; sp = da.UpdateCommand.Parameters.Add("@UserAge", SqlDbType.Int); sp.SourceColumn = "UserAge"; sp.SourceVersion = DataRowVersion.Current; sp = da.UpdateCommand.Parameters.Add("@UserType", SqlDbType.VarChar); sp.SourceColumn = "UserType"; sp.SourceVersion = DataRowVersion.Current; try { if (ds.HasChanges()) { objresult = MessageBox.Show("要保存更改的信息么?", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (objresult == DialogResult.Yes) { da.Update(ds, "userinfo"); MessageBox.Show("数据已更新"); } else { return; } } else { MessageBox.Show("未做任何更改"); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } BindingList,DevExpress/GridControl,MS/dataGridView,序列化/反序列化,XML文件 如何将汉字转换为西欧编码 求助: WCF动态调用 如何代码实现浏览【网上邻居】文件夹功能? TextBox如何识别回车键按钮事件? C#和sqlserver 空文档模板怎么做? 关闭窗口的问题 关于officexp 问题 C# 把当前页面生成WORD文档 C#读串口扫描枪的程序 修改密码又出问题了,为什么原始密码正确也提示:原始密码错 求一垃圾算法!高手速度进啦!
{
conn = CreateConn();
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
cmd = new SqlCommand(sql,conn);
//创建DataSet
ds = new DataSet();
sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
//表名与存储过程名相同
sda.Fill(ds, tablename);
}
catch (Exception ex)
{
throw new Exception(ex.Message); }
finally
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
}
return ds; this.DataGrid1.DataSource=ds;
this.DataGrid1.DataBind();
SqlDataAdapter da;
DataSet ds=new DataSet();
SqlParameter sp=new SqlParameter();
DialogResult objresult;
public Form1()
{
InitializeComponent();
conn = new SqlConnection("server=127.0.0.1;database=pubs;Integrated Security=SSPI");
conn.Open(); } private void Form1_Load(object sender, EventArgs e)
{
//ComboBox读取数据库信息
this.comboBox1.Items.Add("请选择科目");
this.comboBox1.SelectedIndex = 0;
string sql = "select UserType from userinfo";
da = new SqlDataAdapter(sql, conn);
ds = new DataSet();
da.Fill(ds,"userinfo");
for (int i = 0; i < ds.Tables["userinfo"].Rows.Count; i++)
{
this.comboBox1.Items.Add(ds.Tables[0].Rows[i]["UserType"]);
}
}
//增加
private void button1_Click(object sender, EventArgs e)
{
//表格形式的添加方法
//string cbo = this.comboBox1.SelectedItem.ToString();
//string inscmd = "insert into UserInfo values('" + this.textBox1.Text + "','"+this.textBox2.Text+"',"+this.textBox3.Text+",'"+cbo+"')";
//cmd = new SqlCommand(inscmd,conn);
//try
//{
// cmd.ExecuteNonQuery();
// MessageBox.Show("添加成功");
//}
//catch (Exception ex)
//{
// MessageBox.Show(ex.Message);
//} //在网格中添加的方法
string inscmd="insert into UserInfo values(@UserName,@UserPwd,@UserAge,@UserType)";
da.InsertCommand=new SqlCommand(inscmd,conn);
//声明参数并指定数据库的列和版本
sp=da.InsertCommand.Parameters.Add("@UserName",SqlDbType.VarChar);
sp.SourceColumn="UserName";
sp.SourceVersion=DataRowVersion.Current; sp=da.InsertCommand.Parameters.Add("@UserPwd",SqlDbType.VarChar);
sp.SourceColumn="UserPwd";
sp.SourceVersion=DataRowVersion.Current; sp=da.InsertCommand.Parameters.Add("@UserAge",SqlDbType.Int);
sp.SourceColumn="UserAge";
sp.SourceVersion=DataRowVersion.Current; sp=da.InsertCommand.Parameters.Add("@UserType",SqlDbType.VarChar);
sp.SourceColumn="UserType";
sp.SourceVersion=DataRowVersion.Current; if (ds.HasChanges())
{
try
{
da.Update(ds, "UserInfo");
MessageBox.Show("插入记录成功");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
//查询
private void button3_Click(object sender, EventArgs e)
{
try
{
ds.Clear();
da = new SqlDataAdapter("Select * from UserInfo", conn);
da.Fill(ds, "UserInfo");
this.dataGridView1.DataSource = ds.Tables["UserInfo"].DefaultView;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
}
//删除
private void button4_Click(object sender, EventArgs e)
{
string delcmd = "delete from userinfo where UserName=@UserName";
da.DeleteCommand = new SqlCommand(delcmd,conn);
sp = da.DeleteCommand.Parameters.Add("@UserName",SqlDbType.VarChar);
sp.SourceColumn = "UserName";
sp.SourceVersion = DataRowVersion.Original;
//删除警告
objresult = MessageBox.Show("确定要删除当前记录么?","确认",MessageBoxButtons.YesNo,MessageBoxIcon.Warning);
if (objresult == DialogResult.Yes)
{
try
{
ds.Tables["userinfo"].Rows[dataGridView1.CurrentRow.Index].Delete();
da.Update(ds, "userinfo");
MessageBox.Show("已删除记录");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
else
{
return;
}
//如果行数为0,则禁用"删除"和"更新"
if (ds.Tables["userinfo"].Rows.Count == 0)
{
this.button2.Enabled = false;
this.button4.Enabled = false;
}
}
//修改
private void button2_Click(object sender, EventArgs e)
{
string updcmd = "Update UserInfo set UserName=@UserName,UserPwd=@UserPwd,UserAge=@UserAge,UserType=@UserType where UserName=@UserName";
da.UpdateCommand = new SqlCommand(updcmd,conn); sp = da.UpdateCommand.Parameters.Add("@UserName",SqlDbType.VarChar);
sp.SourceColumn = "UserName";
sp.SourceVersion = DataRowVersion.Current; sp = da.UpdateCommand.Parameters.Add("@UserPwd", SqlDbType.VarChar);
sp.SourceColumn = "UserPwd";
sp.SourceVersion = DataRowVersion.Current; sp = da.UpdateCommand.Parameters.Add("@UserAge", SqlDbType.Int);
sp.SourceColumn = "UserAge";
sp.SourceVersion = DataRowVersion.Current; sp = da.UpdateCommand.Parameters.Add("@UserType", SqlDbType.VarChar);
sp.SourceColumn = "UserType";
sp.SourceVersion = DataRowVersion.Current; try
{
if (ds.HasChanges())
{
objresult = MessageBox.Show("要保存更改的信息么?", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (objresult == DialogResult.Yes)
{
da.Update(ds, "userinfo");
MessageBox.Show("数据已更新");
}
else
{
return;
}
}
else
{
MessageBox.Show("未做任何更改");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}