当前位置:编程学习 > C#/ASP.NET >>

怎样将添加的数据在DataGridView中显示

本人现在有段程序,通过“添加”按钮,在各文本框中录入相关数据(学号,姓名,年级,性别),单击“保存”按钮,数据将保存到school数据库的student表中,现在希望将输入的相关数据在Form1窗体的DataGridView中显示出来,求知道的高手指点指点,不甚感激!以下是程序代码:

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

namespace SQL_luru_shujuku
{
    public partial class Form1 : Form
    {
        SqlConnection con = new SqlConnection(@"server=.\SQLEXPRESS;integrated security 

= true;database=school");
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ControlInfo(false);
            showinfo();
        }
        private void showinfo()
        {
            using (SqlDataAdapter da = new SqlDataAdapter("select * from student", con))
            {
                DataTable dt = new DataTable();
                da.Fill(dt);
                DataView dv = new DataView(dt);
                this.dataGridView1.DataSource = dv;

            }
        }

        private void tbADD_Click(object sender, EventArgs e)       
        {
            ControlInfo(true);
            this.tbSave.Enabled = true;
            this.tbADD.Enabled = false;
        }
        private void ControlInfo(Boolean B)
        {
            foreach (Control ct in this.groupBox1.Controls)
            {
                if (ct is TextBox)
                {
                    ct.Text = "";
                    if (B)
                    {
                        ct.Enabled = true;
                    }
                    else
                    {
                        ct.Enabled = false;
                    }
                }
            }
        }

        private void tbSave_Click(object sender, EventArgs e)
        {
            StringBuilder strSQL = new StringBuilder();
            strSQL.Append("insert into student(sno, sname,sclass,ssex)");
            strSQL.Append(" values('" + textBox1.Text.Trim().ToString() + "','" + 

textBox2.Text.Trim().ToString() + "',");
            strSQL.Append("'" + Convert.ToSingle(textBox3.Text.Trim().ToString()) + 

"','" + textBox4.Text.Trim().ToString() + "')");
            using (SqlCommand cmd = new SqlCommand(strSQL.ToString(), con))
            {
                con.Open();
               // SqlDataReader dr = cmd.ExecuteReader();
                cmd.ExecuteNonQuery();
                MessageBox.Show("OK");
                ControlInfo(false);
                con.Close();
            }
            showinfo();
            strSQL.Remove(0, strSQL.Length);
            this.tbSave.Enabled = false;
            this.tbADD.Enabled = true;
        }

      


    }
} --------------------编程问答-------------------- //datagridview 中绑定了数据源datatable以后,就不能直接对datagridview添加,删除行啦

所以只能对数据源下功夫,也就是先把数据添加到DataTable中然后在绑定一次

DataTable m_dt;
private void showinfo()
{
using (SqlDataAdapter da = new SqlDataAdapter("select * from student", con))
{
//这个方法里的DataTable 最好是用个全局变量保存起来,以备重复利用,不然就只有从数据库在查询一次啦
DataTable dt = new DataTable();
da.Fill(dt);
DataView dv = new DataView(dt);
m_dt = dt; //这里
this.dataGridView1.DataSource = dv;

}
}   

//添加
DataRow dr = m_dt.newrow();
 dr[0] = "111"; //textbox1.Text
 dr[1] = "222";  //textbox1.Text
 .....
m_dt.Rows.Add(dr);

this.dataGridView1.DataSource = m_dt; --------------------编程问答-------------------- 重新绑定一次数据表就可以了 --------------------编程问答--------------------

重新绑定数据 --------------------编程问答-------------------- 更新datagridview
DataSet ds = new DataSet();
SqlDataAdapter sda;

SqlCommandBuilder scb = new SqlCommandBuilder(sda);
sda.Update(ds);
this.dataGridView1.DataSource = ds.Tables[0];
   --------------------编程问答-------------------- 好长的代码啊,还是标注一下哪里有问题 --------------------编程问答-------------------- 插入后重新绑定,或用SQLCOMMANDBUILD及SQLDATAADAPTER.UPDATE来做 --------------------编程问答-------------------- 把你的数据保存到数据库中,在显示在到表格上 --------------------编程问答-------------------- 呵呵...C#啊!正在刻苦钻研中!
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,