datagridview not allowing user to delete row

后端 未结 3 1505
梦如初夏
梦如初夏 2021-01-05 15:17

I have a datagridview bound to a datatable. 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 Allow

相关标签:
3条回答
  • 2021-01-05 15:38

    I created an empty winform and just put the grid code into it. The difference was how I had the DataGridView.EditMode set. I had it = EditOnEnter, but the delete works perfectly if I set it = EditOnKeystroke.

    0 讨论(0)
  • 2021-01-05 15:53

    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. With that fixed, @MAW74656's solution worked for me.

    0 讨论(0)
  • 2021-01-05 16:04

    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.)

    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);
    
           }
    }
    
    0 讨论(0)
提交回复
热议问题