1
votes

I have a Winforms datagridview where the existing rows shouldn't be editable but the new row should be. So i set the ReadOnly property to true on the grid, but then i still see the new row but can't edit it. How can i combine these two properties ?

EDIT: just tried to set ReadOnly to true, but still can't edit or add new rows.

  conn = new SqlCeConnection();
  conn.ConnectionString = connectionstring;
  conn.Open();

  daFacturen = new SqlCeDataAdapter("SELECT * FROM Factuur", conn);
  daFacturen.Fill(dsKlantenBeheer, "tblFactuur");

  daFactuurRegels = new SqlCeDataAdapter("SELECT * FROM Factuurregel", conn);
  daFactuurRegels.Fill(dsKlantenBeheer, "tblFactuurregel");

  // Relation between customers and orders
  DataRelation relKlantFactuur;
  DataColumn relKlantFactuurcolMaster;
  DataColumn relKlantFactuurcolDetail;
  relKlantFactuurcolMaster = dsKlantenBeheer.Tables["tblKlant"].Columns["ID"];
  relKlantFactuurcolDetail = dsKlantenBeheer.Tables["tblFactuur"].Columns["KlantID"];
  relKlantFactuur = new DataRelation("RelKlantFactuur", relKlantFactuurcolMaster, relKlantFactuurcolDetail);
  dsKlantenBeheer.Relations.Add(relKlantFactuur);

DataRelation relFactFactregel;
DataColumn relFactFactregelcolMaster;
DataColumn relFactFactregelcolDetail;
relFactFactregelcolMaster = dsKlantenBeheer.Tables["tblFactuur"].Columns["ID"];
relFactFactregelcolDetail = dsKlantenBeheer.Tables["tblFactuurregel"].Columns["FactuurID"];
relFactFactregel = new DataRelation("relFactFactregel", relFactFactregelcolMaster, relFactFactregelcolDetail);
dsKlantenBeheer.Relations.Add(relFactFactregel);

DataViewManager dsView = dsKlantenBeheer.DefaultViewManager;
dsView.DataViewSettings["tblKlant"].RowFilter = "Status = 0 or Status is null";
dsView.DataViewSettings["tblKlant"].Sort = "Naam, Voornaam";

// Grid Databinding 
dgvFacturen.DataSource = dsView;
dgvFacturen.DataMember = "tblKlant.relKlantFactuur";
dgvFacturen.ReadOnly = true;
dgvFacturen.allowUserToAddRows = true;
2
What is expected after you have committed the new row ? Have you tried using AllowNew=true; ?Angshuman Agarwal
allowUserToAddRows is true, where is this AllowNew property to be found ?peter
if you read the remarks section for DataGridView.AllowUserToAddRows, it says - If the DataGridView is bound to data, the user is allowed to add rows if both this property and the data source's IBindingList.AllowNew property are set to true.Angshuman Agarwal
i don't have or use a bindingsource, hoped to avoid that and keep it as simple as possiblepeter

2 Answers

0
votes

Set ReadOnly for row/cells only, not for whole grid:

var row = dataGridView1.Rows[0];
row.ReadOnly = true; //whole row can't be edited

or

var row = dataGridView1.Rows[0];
row.Cells[0].ReadOnly = true; //only first cell is not editable

When DataGridView is ReadOnly then you can't edit, add, delete any row/cell in grid.

0
votes

Here is my solution, simply add some custom code to CellBeginEdit, like this:

    public bool Editable {get;set;}
    int i;
    private void dataGridView_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
    {
        if(Editable) return;
        if(i == e.RowIndex)
        {
            foreach (DataGridViewCell cell in dataGridView1.Rows[e.RowIndex].Cells)
            {
                if (cell.Value == null)
                {                        
                    return;
                }
            }
            e.Cancel = true;                
        }
        else if (dataGridView1.Rows.Count - 1 != e.RowIndex)
        {
            e.Cancel = true;
        }
        else i = e.RowIndex;            
    }

The code above prevent you from re-editing the new row once you input all the values for all the cells in that row. If you want to allow user to edit that new row after committing all the values, the code seems to be much simpler:

private void dataGridView_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
        if(Editable) return;
        if(e.RowIndex < dataGridView.Rows.Count - 2)
        {
            e.Cancel = true;                
        }
}

I've tested and works like a charm. I suppose your new row will have all the cells input with new values.