【问题标题】:Data bound DataGridView show new row symbol数据绑定 DataGridView 显示新行符号
【发布时间】:2012-04-30 20:51:23
【问题描述】:

我有一个 C# (.Net 3.5) 应用程序,我试图在我的数据绑定数据网格视图中强制/显示 NewRow 符号 (* - asterick/star)。我的数据网格视图绑定到一个通用列表。

类 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()(带有星号/星号的那个) - 如何让我的 datagridview 显示一个 NewRow? ?此外,一旦用户开始添加数据(通过拖放),我也希望显示 NewRow。

显示 NewRow 的主要原因是允许添加数据的另一种(替代)方法 - 通过在任一单元格/列中输入必要的数据。我的数据绑定使用拖放操作,但似乎无法显示 NewRow,因此用户可以开始编辑单元格。而且我不想使用按钮将新行“插入”到我的基础数据源中。

感谢任何帮助/帮助。我浏览了这个论坛,找到了可以“隐藏”NewRow 的答案——我的正好相反,尤其是数据有界的datagridview。谢谢。

  • 洛伦兹

【问题讨论】:

  • 有一个属性dataGrdiView1.RowHeaderVisible,你设置为true了吗?
  • 我已将该属性设置为 true - 没有变化。
  • 您正在使用Auto-Implemented Properties,但您也有支持字段。摆脱支持字段并仅使用属性。此外,如果您发布实际代码而不是伪代码(缺少您的类型,而且由于您的属性/字段问题,它可能无法显示您正在添加的数据)会更有帮助。

标签: c# winforms data-binding datagridview


【解决方案1】:

好的 - 我的问题是我的 BindingSource。 AllowNew 属性设置为 false - 我将其设置为 true 并得到了我所需要的。有时最难的问题会得到简单的答案。

bindingSource1 = new BindingSource();
bindingSource1.AllowNew = true;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-15
    • 2013-10-31
    相关资源
    最近更新 更多