如何在程序运行时编辑datagridview

时间:2015-10-18 23:28:48

标签: c#

拜托,我一直在研究这个问题,我尝试过的每一个解决方案都不起作用。我只需要一个小代码片段,我可以复制和粘贴,找出我出错的地方。或者甚至可以查看我的系统的datagridview是否运行不正常。感谢

1 个答案:

答案 0 :(得分:0)

感谢您的帮助。我已经解决了这个问题。错误是我用sqlDataReader加载数据库,它以只读模式加载数据库。所以我使用了sqlDataAdapter和dataset,它以编辑模式加载数据库。这是下面的正确代码

           private void button1_Click(object sender, EventArgs e)
    {
    SqlDataAdapter adap;
    DataSet ds;



        try
        {
            String myConnection = "data source = KEGA\\SQLEXPRESS; database = KEGA; integrated security=SSPI";
            SqlConnection con = new SqlConnection(myConnection);

            con.Open();
            adap = new SqlDataAdapter("select Eid as 'Employee Id', name as 'First Name', surname as 'Last Name', age as 'Age', gender as 'Gender', phone_number as 'Phone Number', bank as 'Bank', acct_name as 'Account Name', acct_number as 'Account Number' from Clients", con);
            ds = new System.Data.DataSet();
            adap.Fill(ds, "Clients");
            Clients_view.DataSource = ds.Tables[0];
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

`