c# - WinForms DataGridView Checkbox -
i trying figure out how set cell in datagridview readonly.
checkbox being added boolean property. therefore, looking tips how accomplished task of setting cell readonly based on column boolean property.
below snippet of code.
[displayname("lock")] public bool revenuelock { get; set; } revenue = new domesticcostrevenue() { revenuelock = convert.toboolean(values[10]), revenue = convert.todouble(values[11]) }; domestic.add(revenue); } costrevenuegridview.datasource = domestic;
this i've done no success far.
foreach (datagridviewrow row in costrevenuegridview.rows) { if ((bool)row.cells["revenuelock"].value == true) { row.cells["revenue"].readonly = true; //messagebox.show("lock"); } }
you can set whole column or whole row or specific cell read only:
- column:
this.datagridview1.columns[1].readonly = true;
- row:
this.datagridview1.rows[0].readonly = true;
- cell:
this.datagridview1.rows[0].cells[1].readonly = true;
test
put below code in button click or somewhere else show form you. in first row, second cell read only, because first cell value true:
var f = new form(); f.controls.add(new datagridview { name = "g", dock = dockstyle.fill }); f.load += (se, ev) => { var g = ((form)se).controls["g"] datagridview; g.autogeneratecolumns = true; g.allowusertoaddrows = false; g.datasource = new list<c1> { new c1{p1=true, p2="x"}, new c1{p1=false, p2="y"}, }; foreach (datagridviewrow row in g.rows) { if ((bool)row.cells["p1"].value == true) row.cells["p2"].readonly = true; } }; f.showdialog();
and here code class c1:
public class c1 { public bool p1 { get; set; } public string p2 { get; set; } }
also problem doesn't exist datatable
:
f.load += (se, ev) => { var g = ((form)se).controls["g"] datagridview; g.autogeneratecolumns = true; g.allowusertoaddrows = false; var dt = new datatable(); dt.columns.add("p1", typeof(bool)); dt.columns.add("p2", typeof(string)); dt.rows.add(true, "x"); dt.rows.add(false, "y"); g.datasource = dt; foreach (datagridviewrow row in g.rows) { if ((bool)row.cells["p1"].value == true) row.cells["p2"].readonly = true; } };
Comments
Post a Comment