| 发表于:2007-03-07 16:32:562楼 得分:35 |
下面的代码示例演示如何选择各列将表示的属性。(msdn) private void enumsandcombobox_load(object sender, system.eventargs e) { // populate the data source. bindingsource1.add(new knight(title.king, "uther ", true)); bindingsource1.add(new knight(title.king, "arthur ", true)); bindingsource1.add(new knight(title.sir, "mordred ", false)); bindingsource1.add(new knight(title.sir, "gawain ", true)); bindingsource1.add(new knight(title.sir, "galahad ", true)); // initialize the datagridview. datagridview1.autogeneratecolumns = false; datagridview1.autosize = true; datagridview1.datasource = bindingsource1; datagridview1.columns.add(createcomboboxwithenums()); // initialize and add a text box column. datagridviewcolumn column = new datagridviewtextboxcolumn(); column.datapropertyname = "name "; column.name = "knight "; datagridview1.columns.add(column); // initialize and add a check box column. column = new datagridviewcheckboxcolumn(); column.datapropertyname = "goodguy "; column.name = "good "; datagridview1.columns.add(column); // initialize the form. this.controls.add(datagridview1); this.autosize = true; this.text = "datagridview object binding demo "; } datagridviewcomboboxcolumn createcomboboxwithenums() { datagridviewcomboboxcolumn combo = new datagridviewcomboboxcolumn(); combo.datasource = enum.getvalues(typeof(title)); combo.datapropertyname = "title "; combo.name = "title "; return combo; } #region "business object " private class knight { private string hisname; private bool good; private title histitle; public knight(title title, string name, bool good) { histitle = title; hisname = name; this.good = good; } public knight() { histitle = title.sir; hisname = " <enter name> "; good = true; } public string name { get { return hisname; } set { hisname = value; } } public bool goodguy { get { return good; } set { good = value; } } public title title { get { return histitle; } set { histitle = value; } } } #endregion | | |
|