数据绑定数据网格视图显示新的行符号
本文关键字:符号 显示 视图 数据 数据网 网格 数据绑定 | 更新日期: 2023-09-27 18:32:56
我有一个 C# (.Net 3.5( 应用程序,我正在尝试在我的数据绑定数据网格视图中强制/显示 NewRow 符号(* - 星号/星号(。 我的数据网格视图绑定到泛型列表。
类 myRecordRow:
class myRecordRow {
private string _recordName;
private string _recordLocation;
public string RecordName {
get { return _recordName; }
set { _recordName = value; }
}
public string RecordLocation {
get { return _recordLocation; }
set { _recordLocation = value; }
}
public myRecordRow() {
_recordName = string.Empty;
_recordLocation = string.Emtpy;
}
public myRecordRow(string name, string location) {
_recordName = name;
_recordLocation = location;
}
}
类我的记录:
class myRecord {
private List<myRecordRow> _recordRows;
public List<myRecordRow> RecordRows {
get { return _recordRows; }
set { _recordRows = value; }
}
}
现在在我的 Winform form1_Load(( 事件中,我有以下内容:
private form1_Load() {
BindingSource bindingSource1 = new BindingSource();
myRecord rec = new myRecord();
myRecordRow newRow = new myRecordRow("name1", "location1");
myRecordRow newRow2 = new myRecordRow("name2", "location2");
rec.RecordRows.Add(newRow); rec.RecordRows.Add(newRow2);
dataGridView1.AutoGenerateColumns = false;
dataGridView1.AllowDrop = true;
dataGridView1.AllowUserToAddRows = true;
int colIndex = dataGridView1.Columns.Add("RecordName", "myRecord Name");
dataGridView1.Columns[colIndex].DataPropertyName = "RecordName";
colIndex = dataGridView1.Columns.Add("RecordLocation", "myRecord Location");
dataGridView1.Columns[colIndex].DataPropertyName = "RecordLocation";
bindingSource1 = new BindingSource();
bindingSource1.DataSource = rec.RecordRows;
dataGridView1.DataSource = bindingSource1;
}
注意:我有拖放事件,可以将数据正确添加到基础数据源(通用列表(。
当我的表单第一次加载时,我的数据网格视图填充了 2 个条目,但没有显示 NewRow(((带有星号/星号符号的条目( - 如何让我的数据网格视图显示 NewRow? 此外,一旦用户开始添加数据(通过拖放(,我也希望显示NewRow。
显示NewRow的主要原因是允许另一种(替代(添加数据的方法 - 通过在单元格/列中键入必要的数据。 我已经使用拖放功能进行了数据绑定,但似乎无法显示NewRow,以便用户可以开始编辑单元格。 而且我不想使用按钮将新行"插入"到我的基础数据源中。
任何帮助/帮助将不胜感激。 我浏览了这个论坛,找到了可以"隐藏"NewRow 的答案 - 我的则相反,尤其是对于数据边界的数据网格视图。 谢谢。
- 洛伦兹
好的 - 我的情况是我的绑定源。 AllowNew 属性设置为 false - 我将其设置为 true 并得到了我需要的。 有时,最难的问题会产生简单的答案。
bindingSource1 = new BindingSource();
bindingSource1.AllowNew = true;