2
votes

I have an C# (.Net 3.5) application and I am trying to force/show the NewRow symbol (* - asterick/star) in my data bound data gridview. My data grid view is bound to a generic list.

class 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:

class myRecord {
    private List<myRecordRow> _recordRows;
    public List<myRecordRow> RecordRows { 
        get { return _recordRows; }
        set { _recordRows = value; }
    }
}

Now in my Winform form1_Load() event, I have the following:

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;
}

NOTE: I have drag & drop events that will properly add data to the underlying datasource (the generic list).

When my form is 1st loaded, my data grid view is populated with the 2 entries but there is no NewRow() (the one with the asterick/star symbol) displayed - how do I get my datagridview to show a NewRow?? Also, once the user starts adding data (by dragging & dropping) I want the NewRow to be displayed as well.

The main reason for the NewRow to be displayed is to allow another (alternative) method of added data - by typing in the necessary data in either cell/column. I've got my databinding working with drag/drop but can't seem to get the NewRow to be displayed so users can start editing the cells. And I would like to not use a button to "insert" a new row into my underlying data source.

Any assistance/help is appreciated. I've looked through this forum and found answers that will "hide" the NewRow - mine is the opposite, especially with data bounded datagridview. Thank you.

  • Lorentz
1
There is a property dataGrdiView1.RowHeaderVisible, have you set it to true? - Nick
I have set that property to true - no change. - Lorentz
You are using Auto-Implemented Properties but you also have backing fields. Get rid of the backing fields and use only the properties. Also, it's more helpful if you post actual code than pseudo-code (missing your types plus it can't possibly display the data you are adding because of your property/field issues). - Brad Rem

1 Answers

3
votes

Okay - the problem in my case was my BindingSource. The AllowNew property was set to false - I set it to true and got exactly what I needed. Sometimes the hardest questions result in simple answers.

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