想要从datagridview更新选定的行并在数据库中更新 [英] want to update selected row from datagridview and update in database

查看:85
本文介绍了想要从datagridview更新选定的行并在数据库中更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何从datagridview更新选定的行并在数据库中更新..
.有人可以帮我吗

how can we update selected row from datagridview and update in database..
.can any one help me

推荐答案

一旦您将datagridview属性设置为允许用户添加行,更新行添加按钮以保存在datagridview,例如:
once you set the datagridview properties to allowuser to add rows , update rows add a button to save your changes that have been done in the datagridview such as :
        private void button1_Click(object sender, EventArgs e)
        {

            string connstr = "Data Source=************";
            SqlConnection myconn = new SqlConnection(connstr);
            myconn.Open();
// here we are moving in all the rows 
            foreach (DataGridViewRow dgRow in dataGridView1.Rows)
            {
                string Id = dgRow.Cells[0].Value.ToString();
                int isEdit = Convert.ToInt32(dgRow.Cells[1].Value);
                //bool isDelete = Convert.ToBoolean(dgRow.Cells[1].Value);
                int isDelete = Convert.ToInt32(dgRow.Cells[2].Value);
                // bool  isView= Convert.ToBoolean(dgRow.Cells[4].Value);
                int isADD = Convert.ToInt32(dgRow.Cells[3].Value);
                string strQuery = "update Roles set CanEdit = ''" + isEdit.ToString() + "'', CanDelete=''" + isDelete.ToString() + "'', CanAdd=''" + isADD.ToString() + "'' where ID=" + Id;
                //update database with above query

                SqlCommand cmdSelect = new SqlCommand(strQuery, myconn);
                cmdSelect.ExecuteNonQuery();
            }
            myconn.Close();
        }



希望这对您有帮助...
问候...



hope this will help you ...
regards...


尝试以下
Try this link. This may be in VB.net but it does give an idea as to how to proceed.


这篇关于想要从datagridview更新选定的行并在数据库中更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆