简体   繁体   English

C#Winforms datagridview:与allowUserToAddRows组合使用的只读

[英]C# Winforms datagridview: readonly in combination with allowUserToAddRows

I have a Winforms datagridview where the existing rows shouldn't be editable but the new row should be. 我有一个Winforms datagridview,其中现有行不应是可编辑的,但新行应是可编辑的。 So i set the ReadOnly property to true on the grid, but then i still see the new row but can't edit it. 因此,我在网格上将ReadOnly属性设置为true,但随后我仍然看到新行,但无法对其进行编辑。 How can i combine these two properties ? 我如何结合这两个属性?

EDIT: just tried to set ReadOnly to true, but still can't edit or add new rows. 编辑:只是试图将ReadOnly设置为true,但仍然无法编辑或添加新行。

  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;

Set ReadOnly for row/cells only, not for whole grid: 仅将ReadOnly设置为行/单元格,而不是整个网格:

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. DataGridViewReadOnly您将无法编辑,添加,删除网格中的任何行/单元格。

Here is my solution, simply add some custom code to CellBeginEdit , like this: 这是我的解决方案,只需向CellBeginEdit添加一些自定义代码,如下所示:

    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. 我想您的新行将为所有单元格输入新值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM