[英]datagridview not allowing user to delete row
I have a datagridview bound to a datatable. 我有一个绑定到数据表的datagridview。 The columns are not autogenerated, I create them in code.
列不是自动生成的,我在代码中创建它们。 I want my user's to be able to add and delete a row, so I have AllowUserToAddRows = true, AllowUserToDeleteRows = true.
我希望我的用户能够添加和删除一行,所以我有AllowUserToAddRows = true,AllowUserToDeleteRows = true。
The problem is: when they select a row and press DEL, the information is replaced by the row cell's default values. 问题是:当他们选择一行并按DEL时,信息将被行单元格的默认值替换。 The row does not go away.
这一行不会消失。
What is this behavior (why/what is happening), and how can I get around it? 这是什么行为(为什么/发生了什么),我该如何解决这个问题?
Here is my grid creation code: 这是我的网格创建代码:
private void resetDowntimeGrid()
{
downtimeEntries = new DataTable();
downtimeEntries.Columns.Add("Category", typeof(int));
downtimeEntries.Columns.Add("HoursDown");
downtimeEntries.Columns.Add("Reason");
//deptDowntimeCategories.Select("", "ListOrder ASC");
gridDowntime.Columns.Clear();
DataGridViewComboBoxColumn cboCategory = new DataGridViewComboBoxColumn();
{
cboCategory.HeaderText = "Category";
cboCategory.DataSource = deptDowntimeCategories;
cboCategory.DisplayMember = "Name";
cboCategory.ValueMember = "CategoryID";
cboCategory.DataPropertyName = "Category";
cboCategory.Width = Convert.ToInt16(gridDowntime.Width * 0.20);
cboCategory.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox;
cboCategory.AutoComplete = true;
gridDowntime.Columns.Add(cboCategory);
}
DataGridViewTextBoxColumn txtDowntime = new DataGridViewTextBoxColumn();
txtDowntime.HeaderText = "HoursDown";
txtDowntime.DataPropertyName = "HoursDown";
txtDowntime.Width = Convert.ToInt16(gridDowntime.Width * 0.15);
gridDowntime.Columns.Add(txtDowntime);
DataGridViewTextBoxColumn txtReason = new DataGridViewTextBoxColumn();
txtReason.HeaderText = "Reason";
txtReason.DataPropertyName = "Reason";
txtReason.Width = Convert.ToInt16(gridDowntime.Width * 0.60);
gridDowntime.Columns.Add(txtReason);
gridDowntime.DataSource = downtimeEntries;
lblStatus.Text = "Downtime grid reset.";
}
I created an empty winform and just put the grid code into it. 我创建了一个空的winform并将网格代码放入其中。 The difference was how I had the
DataGridView.EditMode
set. 不同之处在于我如何设置
DataGridView.EditMode
。 I had it = EditOnEnter
, but the delete works perfectly if I set it = EditOnKeystroke.
我有它
= EditOnEnter
,但如果我设置它= EditOnKeystroke.
,删除工作完美= EditOnKeystroke.
Here's a quick sample that I did, minus two of your columns, using part of your code. 这是我做的一个快速示例,使用部分代码减去两列。 I could not duplicate your problem.
我无法复制你的问题。 You may want to validate that you are actually selecting a value, clicking the value from the drop down, and then click 'Delete'.
您可能想验证您实际上是在选择一个值,单击下拉列表中的值,然后单击“删除”。 (In this case, I simply dragged a
DataGridView
from the toolbox to the form. I made no other modifications.) (在这种情况下,我只是将
DataGridView
从工具箱拖到表单中。我没有做任何其他修改。)
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
DataTable myTable = new DataTable("MyTable");
myTable.Columns.Add("CategoryID", typeof(int));
myTable.Columns.Add("CategoryName", typeof(string));
myTable.Rows.Add(new object[] { 1, "Small" });
myTable.Rows.Add(new object[] { 2, "Medium" });
myTable.Rows.Add(new object[] { 3, "Large" });
DataGridViewComboBoxColumn cboCategory = new DataGridViewComboBoxColumn();
cboCategory.HeaderText = "Category";
cboCategory.DataSource = myTable;
cboCategory.DisplayMember = "CategoryName";
cboCategory.ValueMember = "CategoryID";
cboCategory.DataPropertyName = "Category";
cboCategory.Width = Convert.ToInt16(dataGridView1.Width * 0.20);
cboCategory.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox;
cboCategory.AutoComplete = true;
dataGridView1.Columns.Add(cboCategory);
}
}
Another reason why hitting Delete doesn't remove the rows is if you have the RowHeadersVisible
property set to False, thus not showing the *
in the left most column. 如果您将
RowHeadersVisible
属性设置为False,那么点击Delete不会删除行的另一个原因是,因此不会在最左侧的列中显示*
。 With that fixed, @MAW74656's solution worked for me. 有了这个问题,@ MAW74656的解决方案对我有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.