datatable的问题.是不是微软的Bug.

没办法了,好多地方找不到答案,csdn也没人回答,只有贴到这了。
斑竹手下留情,2天后移贴

如果你不排序,直接点击 “del“按钮,没有问题,可以正常删除。
但是如果你单击列名为 “Dname”的列,排了一下序,然后点击 “del“按钮,就发生一个异常,下面是我的代码。

None.gif using  System;
None.gif
using  System.Drawing;
None.gif
using  System.Collections;
None.gif
using  System.ComponentModel;
None.gif
using  System.Windows.Forms;
None.gif
using  System.Data;
None.gif
None.gif
namespace  deldatagridcolumn
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public class Form1 : System.Windows.Forms.Form
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private System.Windows.Forms.Button button2;
InBlock.gif        
private System.ComponentModel.Container components = null;
InBlock.gif        
private System.Windows.Forms.DataGrid grid;
InBlock.gif
InBlock.gif        DataTable dt 
= new DataTable("T");
InBlock.gif        
public Form1()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            InitializeComponent();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override void Dispose( bool disposing )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if( disposing )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (components != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    components.Dispose();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
base.Dispose( disposing );
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.grid = new System.Windows.Forms.DataGrid();
InBlock.gif            
this.button2 = new System.Windows.Forms.Button();
InBlock.gif            ((System.ComponentModel.ISupportInitialize)(
this.grid)).BeginInit();
InBlock.gif            
this.SuspendLayout();
InBlock.gif            
// 
InBlock.gif            
// grid
InBlock.gif            
// 
InBlock.gif
            this.grid.DataMember = "";
InBlock.gif            
this.grid.HeaderForeColor = System.Drawing.SystemColors.ControlText;
InBlock.gif            
this.grid.Location = new System.Drawing.Point(2416);
InBlock.gif            
this.grid.Name = "grid";
InBlock.gif            
this.grid.Size = new System.Drawing.Size(448288);
InBlock.gif            
this.grid.TabIndex = 0;
InBlock.gif            
// 
InBlock.gif            
// button2
InBlock.gif            
// 
InBlock.gif
            this.button2.Location = new System.Drawing.Point(400312);
InBlock.gif            
this.button2.Name = "button2";
InBlock.gif            
this.button2.TabIndex = 2;
InBlock.gif            
this.button2.Text = "Del";
InBlock.gif            
this.button2.Click += new System.EventHandler(this.button2_Click);
InBlock.gif            
// 
InBlock.gif            
// Form1
InBlock.gif            
// 
InBlock.gif
            this.AutoScaleBaseSize = new System.Drawing.Size(614);
InBlock.gif            
this.ClientSize = new System.Drawing.Size(504341);
InBlock.gif            
this.Controls.Add(this.button2);
InBlock.gif            
this.Controls.Add(this.grid);
InBlock.gif            
this.Name = "Form1";
InBlock.gif            
this.Text = "Form1";
InBlock.gif            
this.Load += new System.EventHandler(this.Form1_Load);
InBlock.gif            ((System.ComponentModel.ISupportInitialize)(
this.grid)).EndInit();
InBlock.gif            
this.ResumeLayout(false);
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [STAThread]
InBlock.gif        
static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Application.Run(
new Form1());
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void Form1_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
InBlock.gif            DataColumn dc 
= new DataColumn("dcode",typeof(string));
InBlock.gif            DataColumn dc1 
= new DataColumn("dname",typeof(string));
InBlock.gif            dt.Columns.Add(dc);
InBlock.gif            dt.Columns.Add(dc1);
InBlock.gif            DataRow dr 
= dt.NewRow();
InBlock.gif            dr[
0= "aa";
InBlock.gif            dr[
1= "aa";
InBlock.gif            dt.Rows.Add(dr);
InBlock.gif
InBlock.gif            grid.DataSource 
= dt;
InBlock.gif
InBlock.gif            DataGridTableStyle myGridStyle 
= new DataGridTableStyle();
InBlock.gif            myGridStyle.MappingName 
= "T";
InBlock.gif
InBlock.gif
InBlock.gif            DataGridTextBoxColumn style 
= new DataGridTextBoxColumn();
InBlock.gif            style.MappingName  
= "dcode";
InBlock.gif            style.HeaderText   
= "DCODE";
InBlock.gif            style.Width            
= 100;
InBlock.gif            myGridStyle.GridColumnStyles.Add(style);
InBlock.gif
InBlock.gif            DataGridTextBoxColumn style1 
= new DataGridTextBoxColumn();
InBlock.gif            style1.MappingName  
= "dname";
InBlock.gif            style1.HeaderText   
= "DName";
InBlock.gif            style1.Width        
= 100;
InBlock.gif
InBlock.gif            myGridStyle.GridColumnStyles.Add(style1);
InBlock.gif            grid.TableStyles.Add(myGridStyle);
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
private void button2_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(dt.Columns.Count > 1)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                dt.Columns.RemoveAt(
1);
InBlock.gif                
//grid.TableStyles[0].GridColumnStyles.RemoveAt(1);
ExpandedSubBlockEnd.gif
            }

InBlock.gif            button2.Enabled 
= false;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


 

转载于:https://www.cnblogs.com/rock_chen/archive/2004/07/29/28347.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值