C# 如何在写入Datagridview时禁用自动创建新行

C# 如何在写入Datagridview时禁用自动创建新行,c#,C#,如何最初显示datagridview的一行,以及如何在将文本写入单元格时禁用新行创建,以及在每行最后一列输入enter键时创建新行,这不是web或win应用程序,因此,win应用程序的示例如下: 1-最初先创建新行: 在表单加载时或当您进入特定节时,手动添加行。 像dataGridView1.Rows.Add(新的DataGridViewRow(“名称”、“年龄”、“城市”)) 或者将其添加到数据源(datatable或list)并绑定到DataGridview DataTable dt =

如何最初显示datagridview的一行,以及如何在将文本写入单元格时禁用新行创建,以及在每行最后一列输入enter键时创建新行,这不是web或win应用程序,因此,win应用程序的示例如下:

1-最初先创建新行:

在表单加载时或当您进入特定节时,手动添加行。 像dataGridView1.Rows.Add(新的DataGridViewRow(“名称”、“年龄”、“城市”))

或者将其添加到数据源(datatable或list)并绑定到DataGridview

 DataTable dt = new DataTable();
  dt.Columns.Add("Age", typeof(int));
  dt.Columns.Add("Name");
  dt.Rows.Add(new DataRow(0," ");
  dataGridView1.AllowUserToAddRows = false;
  dataGridView1.EditMode = DataGridViewEditMode.EditOnKeystroke;
  dataGridView1.DataSource = dt;
  dataGridView1.AllowUserToAddRows=false;
2-处理要监视的单元格单击事件,如果其enter键是最后一个单元格,则将另一个空行添加到数据源中

    dataGridView1.AllowUserToAddRows=true;
    dt.Rows.Add(new DataToRow(1,""));
    dataGridView1.AllowUserToAddRows=true;
    dataGridView1.AllowUserToAddRows=false;
代码是用记事本写的,所以没有经过测试,但给出了实现目标的方法


-谢谢

没有提到web或win应用程序,因此,下面是win应用程序的示例:

1-最初先创建新行:

在表单加载时或当您进入特定节时,手动添加行。 像dataGridView1.Rows.Add(新的DataGridViewRow(“名称”、“年龄”、“城市”))

或者将其添加到数据源(datatable或list)并绑定到DataGridview

 DataTable dt = new DataTable();
  dt.Columns.Add("Age", typeof(int));
  dt.Columns.Add("Name");
  dt.Rows.Add(new DataRow(0," ");
  dataGridView1.AllowUserToAddRows = false;
  dataGridView1.EditMode = DataGridViewEditMode.EditOnKeystroke;
  dataGridView1.DataSource = dt;
  dataGridView1.AllowUserToAddRows=false;
2-处理要监视的单元格单击事件,如果其enter键是最后一个单元格,则将另一个空行添加到数据源中

    dataGridView1.AllowUserToAddRows=true;
    dt.Rows.Add(new DataToRow(1,""));
    dataGridView1.AllowUserToAddRows=true;
    dataGridView1.AllowUserToAddRows=false;
代码是用记事本写的,所以没有经过测试,但给出了实现目标的方法

-谢谢