现在数据库(sqlserver)与DataGridView已经连接成功(表已经能在DataGridView中显示)
如何在弹出的对话框中(由一个 button 和两个textbox组成) 将数据输入到textbox中 ,并点击button将数据更新到数据库中

解决方案 »

  1.   

    if (frm.DialogResult == DialogResult.OK)
      {}绑定数据
    http://topic.csdn.net/u/20090625/10/dd522997-7397-45bc-9af5-e725fdcfed6f.html
      

  2.   

    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.Windows.Forms;namespace WindowsFormsApplication1
    {
        public partial class 药品录入 : Form
        {
            public 药品录入()
            {
                InitializeComponent();
            }        internal void showdialog()
            {
                throw new NotImplementedException();
            }
            private DataSet data = new DataSet();
            private SqlDataAdapter dataapter;        private void 药品录入_Load(object sender, EventArgs e)
            {            // this.skinEnglish1.skinfile=@"macos.ssk" ;    // 皮肤           string sql = "select  ypbh as 药品编号, yptm as  药品条码, tym as 通用名, dw as  单位, gg as 规格,   jx  as 剂型, cd  as 产地, bzsl as 包装数量,yxq as 有效期,zlbz as 质量标准,jyfs  as 经营方式,ph as 批号,lsjg as 零售价格,jhjg as 进货价格 ,yplb as 药品类别 , ylbh as 医疗编号  from tb_medicine_jbqkb";
                //string sql = "select  ypbh , yptm , tym, dw , gg ,   jx , cd , bzsl,yxq ,zlbz ,jyfs ,ph ,lsjg ,jhjg  ,yplb,  ylbh   from tb_medicine_jbqkb";            SqlConnection conn = new SqlConnection("server=localhost;uid=sa;pwd=1234;database=DB_JXC");
                conn.Open();
                dataapter = new SqlDataAdapter(sql, conn);
                dataapter.Fill(data, "tb_medicine_jbqkb");
                dataGridView1.DataSource = data.Tables["tb_medicine_jbqkb"];
                
            }              private void baocun_Click(object sender, EventArgs e)   //保存
            {
                string  BianHao =bianhao.Text;    //药品编码            string   TiaoMa = tiaoma.Text;     //药品条码            string GuiGe = guige.Text;     //药品规格            string  ChanDi = chandi.Text;     // 药品产地            string  CheckedListBox1 = checkedListBox1.Text;  //药品有效期            string  LingShouJia = lingshoujia.Text;  // 药品零售价            string  JinHuoJia = jinhuojia.Text;  //药品进货价            string  BaoXian =baoxian.Text;    // 医疗保险编号            string   YaoPinName = yaopinname.Text;  //药品名称            string TYname = tyname.Text;      //通用名称            string  CheckedListBox2 = checkedListBox2.Text;//药品单位            string  CheckedListBox3 = checkedListBox3.Text;//药品剂型            string CheckedListBox4 = checkedListBox4.Text;//药品质量标准           string  BaoShuLiang =baoshuliang.Text; //包装数量           string  PiHao = pihao.Text;   //药品批号           string  CheckedListBox5 = checkedListBox5.Text; //  药品类别            string  JYtime=dateTimePicker1.Text;            
                SqlConnection conn = new SqlConnection("server=localhost;uid=sa;pwd=1234;database=DB_JXC");
                
                conn.Open();            string sqlll = string.Format("insert  into tb_medicine_jbqkb( ypbh , yptm , tym, dw , gg ,   jx , cd , bzsl,yxq ,zlbz ,jyfs ,ph ,lsjg ,jhjg  ,yplb,  ylbh) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}','{15}')", BianHao, TiaoMa, GuiGe, ChanDi, CheckedListBox1, LingShouJia, JinHuoJia, BaoXian, YaoPinName, TYname, CheckedListBox2, CheckedListBox3, CheckedListBox4, BaoShuLiang, PiHao, CheckedListBox5, JYtime);
                 
                SqlCommand cmd = new SqlCommand(sqlll, conn);
                //SqlDataAdapter sda = new SqlDataAdapter(sqlll, conn);          
                 var result = cmd.ExecuteNonQuery();
                if (result != 1)
                {
                     SqlCommandBuilder bulider = new SqlCommandBuilder(dataapter);  //自动生成单表命令,用于生成dataset所做的更改与SQL数据库的更改相协调
                    dataapter.Update(data, "tb_medicine_jbqkb");
                    MessageBox.Show("添加失败", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("添加成功", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                dataapter.Update(data, "tb_medicine_jbqkb");  //更新数据库            conn.Close();
            }        private void shuaxin_Click(object sender, EventArgs e)    //刷新
            {
                data.Tables["tb_medicine_jbqkb"].Clear();
                //data.Tables["tb_medicine_jbqkb"].Clear();
                药品录入_Load(sender, e);
            }        private void exit_Click(object sender, EventArgs e)      // 退出
            {
                this.Close();
            }        private void shanchu_Click(object sender, EventArgs e)    //删除
            {
                DialogResult result = MessageBox.Show("确定要删除吗?", "操作提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);            if (result == DialogResult.OK)
                {
                    SqlCommandBuilder builder = new SqlCommandBuilder(dataapter);
                    data.Tables["tb_medicine_jbqkb"].Rows[this.dataGridView1.CurrentRow.Index].Delete();
                    //dataapter.Update(data, "tb_medicine_jbqkb");
                    MessageBox.Show("删除成功", "操作提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                   
                }
                else
                {
                    MessageBox.Show("删除失败", "操作提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                }
                dataapter.Update(data, "tb_medicine_jbqkb");//更新数据库
            }        private void dataGridView1_MouseClick(object sender, MouseEventArgs e)          //鼠标单击显示事件,在文本的输入框中显示dataGridView数据表中的信息
            {
                bianhao.Text = Convert.ToString(dataGridView1.SelectedRows[0].Cells[0].Value);    //药品编码            tiaoma.Text = Convert.ToString(dataGridView1.SelectedRows[0].Cells[1].Value);     //药品条码            guige.Text = Convert.ToString(dataGridView1.SelectedRows[0].Cells[2].Value);      //药品规格            chandi.Text = Convert.ToString(dataGridView1.SelectedRows[0].Cells[3].Value);     // 药品产地            checkedListBox1.Text = Convert.ToString(dataGridView1.SelectedRows[0].Cells[4].Value);  //药品有效期            lingshoujia.Text = Convert.ToString(dataGridView1.SelectedRows[0].Cells[5].Value);  // 药品零售价            jinhuojia.Text = Convert.ToString(dataGridView1.SelectedRows[0].Cells[6].Value);  //药品进货价            baoxian.Text = Convert.ToString(dataGridView1.SelectedRows[0].Cells[7].Value);    // 医疗保险编号            yaopinname.Text = Convert.ToString(dataGridView1.SelectedRows[0].Cells[8].Value);  //药品名称            tyname.Text = Convert.ToString(dataGridView1.SelectedRows[0].Cells[9].Value);      //通用名称            checkedListBox2.Text = Convert.ToString(dataGridView1.SelectedRows[0].Cells[10].Value);//药品单位            checkedListBox3.Text = Convert.ToString(dataGridView1.SelectedRows[0].Cells[11].Value);//药品剂型            checkedListBox4.Text = Convert.ToString(dataGridView1.SelectedRows[0].Cells[12].Value);//药品质量标准            baoshuliang.Text = Convert.ToString(dataGridView1.SelectedRows[0].Cells[13].Value); //包装数量            pihao.Text = Convert.ToString(dataGridView1.SelectedRows[0].Cells[14].Value);   //药品批号            checkedListBox5.Text = Convert.ToString(dataGridView1.SelectedRows[0].Cells[15].Value); //  药品类别        }        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)       //鼠标双击事件
            {        }
      
          
        }
    }