如何使DataGridView中的特定列只能编辑 - C#

时间:2016-06-06 07:11:23

标签: c# winforms datagridview

我使用DataGridViewdatabase(以编程方式)获取一些记录。此外,我将grid设为只读,因此用户无法修改/编辑某些内容。与此同时,我添加了另一列(checkbox)并且由于grid的属性,我无法选择我想要的任何行。是否有可能只编辑一列?感谢。

代码:

using (SqlConnection con = new SqlConnection(cs))
            {


                con.Open();
                SqlCommand cmd = new SqlCommand(select, con);
                SqlDataAdapter sda = new SqlDataAdapter();
                sda.SelectCommand = cmd;
                DataTable dt = new DataTable();
                sda.Fill(dt);
                BindingSource bs = new BindingSource();
                bs.DataSource = dt;
                dataGridView1.DataSource = dt;

            }

            DataGridViewCheckBoxColumn dch = new DataGridViewCheckBoxColumn();
            dch.HeaderText = "Selecteaza";
            dataGridView1.Columns.Add(dch);

编辑 - 我使用此选项,我只希望我添加的列("Selecteaza")可编辑:

    "SELECT p.cod AS Numar, p.data AS Data, c.nume AS Furnizor, d.nume AS DocFurnizor, p.doc_cod AS NrDocFurnizor, p.validat AS Validat, p.facturat AS Contat, g.nume AS Gestiune 
FROM primar p INNER JOIN cf c ON p.part1=c.cf_id 
INNER JOIN gestiuni g ON p.part2 = g.gest_id 
INNER JOIN documente d ON p.doc_id = d.doc_id WHERE (p.tip = '2')
ORDER BY p.Data";

1 个答案:

答案 0 :(得分:0)

ReadOnly设置falseDataGridView,然后根据需要为列分别设置ReadOnly属性。