提供的列已属于DataGridView控件 [英] Provided column already belongs to the DataGridView control

查看:448
本文介绍了提供的列已属于DataGridView控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的人们

有人可以帮我吗,请使用DataGridView解决此问题.在单击事件按钮时,我使用数据库查询语句(请参见下面的代码)将提供行的两列生成到dataGridView中,同时在设计时我将另外两列与行一起添加.

当我运行我的应用程序并单击该按钮时,它会正确显示所有列和行,但是当我再次单击同一按钮时,它将生成此错误:


System.InvalidOpereationException:提供的列已属于DataGridView控件.

这也是代码:

Dear people

Can someone help me please sort out this problem with DataGridView. On click event button I''m generating into dataGridView two Columns with the Rows provided, using the database query statement (see the code below) and in the mean time I''m adding another 2 columns with the rows at design time.

When I run my application and I click the button it displays correctly the all columns and rows, but when I click again the same button it generates this error:


System.InvalidOpereationException: Provided column already belongs to the DataGridView control.

Here is also the code:

private DataGridViewTextBoxColumn ColQtyStock = new DataGridViewTextBoxColumn();
        private DataGridViewTextBoxColumn ColStatus = new DataGridViewTextBoxColumn();
        
        private void cmdStarters_Click(object sender, EventArgs e)
        {
            
            OleDbConnectionStringBuilder connBuilder = new OleDbConnectionStringBuilder();
            connBuilder.DataSource = @"C:\Users\AP_AE\Desktop\DTPOS_APP\DataBase\DtposMenu.accdb";
            connBuilder.Provider = "Microsoft.ACE.OLEDB.12.0";
            connBuilder.Add("Jet OLEDB:Engine Type", "5");
            // Food SQL Query
            string foodTypeSql = @"SELECT FoodName, FoodType FROM Food WHERE FoodType = @foodType";
            using (OleDbConnection conn = new OleDbConnection(connBuilder.ConnectionString))
            {
                try
                {
                    

                    OleDbCommand foodsCommand = new OleDbCommand(foodTypeSql, conn);
                    OleDbParameter foodType = foodsCommand.Parameters.Add("@foodType", OleDbType.VarChar, 15);
                    OleDbDataAdapter foodsDa = new OleDbDataAdapter(foodsCommand);
                    DataRow dr;
                    DataSet ds = new DataSet();
                    conn.Open();
                    foodType.Value = "Starter";
                    foodsDa.Fill(ds, "Food_table");
                    conn.Close();
                    dataGridView1.DataSource = ds;
                    dataGridView1.DataMember = "Food_table";
                    
                    dataGridView1.Columns.AddRange(ColQtyStock,ColStatus);//new DataGridViewColumn[] {
                    
                    //});
                    
                    DataGridViewCellStyle dataGridViewCellStyle1 = new DataGridViewCellStyle();
                    this.dataGridView1.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle1;
                    dataGridViewCellStyle1.Font = new Font("Verdana", 20.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));

                    DataGridViewCellStyle dataGridViewCellStyle2 = new DataGridViewCellStyle();
                    this.dataGridView1.RowHeadersDefaultCellStyle = dataGridViewCellStyle2;
                    dataGridViewCellStyle2.Font = new Font("Verdana", 18.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    
                    this.dataGridView1.RowTemplate.Height = 60;
                    
                    this.dataGridView1.Columns[0].Width = 500;
                    this.dataGridView1.Columns[1].Width = 200;
                    this.dataGridView1.Columns[2].Width = 300;
                    this.dataGridView1.Columns[3].Width = 200;

                    // ColStatus 
                    ColStatus.HeaderText = "Status";
                    ColStatus.Name = "ColStatus";

                    // ColQtyStock
                    ColQtyStock.HeaderText = "Quantity In Stock";
                    ColQtyStock.Name = "ColQtyStock";
                    
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: " + ex);
                }
            }

        }




有人可以告诉我我在做什么吗...


在此先感谢

roni




Could someone tell me what am I doing worng please...


thanks in advance

roni

推荐答案

您正在单击处理程序中添加列,如果单击两次,则将再次添加这些列,因此会出现错误.我认为错误消息对此非常清楚.

请同时查看OleDBCommand及其用法或参数. MSDN文档明确指出,当命令类型为文本(SQL)时,不能使用命名参数.设置参数的代码之所以有效,是因为您只有on参数,但是当有多个参数并且将它们填充到参数集合中的顺序与SQL中这些参数的顺序不同时,就会遇到问题. br/>
修改:
创建两列的代码:
You are adding the columns in the click handler and if you click twice you are going to add these columns again, hence the error. I think the error message is pretty clear about that.

Please also look into OleDBCommand and the use or Parameters with it. MSDN documentation clearly states that named parameters cannot be used with when command type is Text (SQL). Your code setting the parameters works because you only have on parameter, but you''ll run into problems when there is more than one parameters and the sequence you fill them into the parameters collection differs from the sequence of those parameters in your SQL.

Modification:
The code where you create the two columns:
private DataGridViewTextBoxColumn ColQtyStock = new DataGridViewTextBoxColumn();
private DataGridViewTextBoxColumn ColStatus = new DataGridViewTextBoxColumn();


在您的点击处理程序之外.在您的处理程序中,您可以添加一次,但是第二次必须失败,因为它们已经添加了!

最好的问候,
Manfred


is outside your click handler. Inside your handler you can add them once but the second time must fail, because they are already added!

Best Regards,
Manfred


似乎您正在使用DataGridView控件的AutoGenerateColumns=True.

解决方案1:
-设置AutoGenerateColumns = False.
-通过设计器或表单加载"事件创建列

解决方案2:
-致电
It seems like you are using DataGridView control''s AutoGenerateColumns=True.

Solution 1:
- Set AutoGenerateColumns = False.
- Create columns from designer or Form Loaded event

Solution 2:
- Call
dataGridView1.Columns.Clear()


在Button Click事件上.

如果有帮助,请将其标记为答案


on the Button Click event.

Mark it as answer, if it is helpful


这篇关于提供的列已属于DataGridView控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆