1

我有一个 Winforms datagridview,其中现有行不应该是可编辑的,但新行应该是。所以我在网格上将 ReadOnly 属性设置为 true,但是我仍然看到新行但无法编辑它。我怎样才能结合这两个属性?

编辑:刚刚尝试将 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;
4

2 回答 2

0

ReadOnly只为行/单元格设置,而不是为整个网格设置:

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

或者

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

什么时候DataGridViewReadOnly不能编辑、添加、删除网格中的任何行/单元格。

于 2013-06-10T08:41:29.870 回答
0

这是我的解决方案,只需添加一些自定义代码即可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;            
    }

一旦输入了该行中所有单元格的所有值,上面的代码将阻止您重新编辑新行。如果您想允许用户在提交所有值后编辑该新行,代码似乎要简单得多:

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

我已经测试过并且像魅力一样工作。我想您的新行将输入所有带有新值的单元格。

于 2013-06-10T09:12:55.530 回答