Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C Winforms datagridview:readonly与AllowUserToAddress结合使用_C#_Winforms_Datagridview - Fatal编程技术网

C# C Winforms datagridview:readonly与AllowUserToAddress结合使用

C# C Winforms datagridview:readonly与AllowUserToAddress结合使用,c#,winforms,datagridview,C#,Winforms,Datagridview,我有一个Winforms datagridview,其中现有的行不应该是可编辑的,但新行应该是可编辑的。 因此,我在网格上将ReadOnly属性设置为true,但仍然可以看到新行,但无法编辑它。 如何组合这两个属性 编辑:刚刚尝试将ReadOnly设置为true,但仍然无法编辑或添加新行 conn = new SqlCeConnection(); conn.ConnectionString = connectionstring; conn.Open(); daFacturen

我有一个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;
仅为行/单元格设置只读,不为整个网格设置只读:

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


当DataGridView为只读时,您不能编辑、添加、删除网格中的任何行/单元格

这是我的解决方案,只需向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;                
        }
}

我已经测试过了,效果很好。我想您的新行将使用新值输入所有单元格。

提交新行后,您的期望是什么?您是否尝试过使用AllowNew=true?AllowUserToAddress为true,在哪里可以找到此AllowNew属性?如果您阅读DataGridView.AllowUserToAddress的备注部分,它会说-如果DataGridView绑定到数据,则如果此属性和数据源的IBindingList.AllowNew属性都设置为true,则允许用户添加行。我没有或使用bindingsource,希望避免这种情况并尽可能简单,但是我需要迭代所有行,在添加redo this之后,我希望有一个更简单的解决方案@gzaxx,通过将readonly设置为false使网格可编辑,但仍然无法编辑或添加您是否将行设置为只读,并且在对行进行迭代时跳过了新行?我确实将整个网格的readonly设置为false,但仍然无法编辑或添加,一定是关系问题谢谢King,但我刚刚发现,当网格设置为可编辑时,我甚至无法编辑,一定是关系问题,有什么建议吗?@peter啊,你的意思是如果Editable=true,你想允许用户编辑网格。如果是这样,那很简单。@peter你如何设置网格的可编辑性?没有这样的财产吗?你的意思是你想声明这样的标志吗?在King:在designer中,如果你选择了DataGridView,你就有了我设置为true的属性,但我仍然无法编辑,大概是因为我使用了一个关系作为source@peter如果这样做,DataGridView当然是只读的,您不能编辑。您想要的行为不能与ReadOnly=true匹配。所以我想你可以用某种旗帜来代替。我要用flag更新我的代码
private void dataGridView_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
        if(Editable) return;
        if(e.RowIndex < dataGridView.Rows.Count - 2)
        {
            e.Cancel = true;                
        }
}