欢迎来到传世资源网!
加载中...
正在加载,请耐心等待...
本站为收藏、学习站,如有侵权,请联系管理员删除!

DataGridView 直接修改数据

介绍 评论 失效链接反馈

     在C#的DataGrid中直接修改数据。 DataGridView 直接修改数据 C#数据库操作-第1张DataGridView 直接修改数据 C#数据库操作-第2张 private void dgvBind()
        {
            SqlConnection mycon = GetConnection();
            try
            {
                mycon.Open();
                SqlDataAdapter sda = new SqlDataAdapter("select * from mytable001", mycon);
                DataTable table = new DataTable();
                sda.Fill(table);
                this.dataGridView1.AutoGenerateColumns = true;
                this.dataGridView1.DataSource = table;
                this.dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter;

            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                mycon.Close();
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            dgvBind();
        }

        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            SqlConnection mycon = GetConnection();
            try
            {
                mycon.Open();

                string mystr1=dataGridView1.Columns[e.ColumnIndex].HeaderText "=" "'" dataGridView1.CurrentCell.Value.ToString() "'";
                string mystr2=dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
                string updateSql = "update mytable001 set " mystr1 " where id=" mystr2;
                SqlCommand mycom = new SqlCommand(updateSql, mycon);
                mycom.ExecuteNonQuery();
                dgvBind();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                mycon.Close();
            }

        }
    }

下载声明:

本站资源均有第三方用户自行上传分享推荐,非本站自制,仅供玩家做交流学习之用!切勿用于商业用途!游戏作品版权归原作者享有,如有版权问题,请附带版权证明至邮件,本平台将应您的要求删除。
相关推荐:

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复