zoukankan      html  css  js  c++  java
  • datatable的问题.是不是微软的Bug.

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

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

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;

    namespace deldatagridcolumn
    {
        
    public class Form1 : System.Windows.Forms.Form
        
    {
            
    private System.Windows.Forms.Button button2;
            
    private System.ComponentModel.Container components = null;
            
    private System.Windows.Forms.DataGrid grid;

            DataTable dt 
    = new DataTable("T");
            
    public Form1()
            
    {
                InitializeComponent();
            }


            
    protected override void Dispose( bool disposing )
            
    {
                
    if( disposing )
                
    {
                    
    if (components != null)
                    
    {
                        components.Dispose();
                    }

                }

                
    base.Dispose( disposing );
            }


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

            }


            [STAThread]
            
    static void Main()
            
    {
                Application.Run(
    new Form1());
            }


            
    private void Form1_Load(object sender, System.EventArgs e)
            
    {

                DataColumn dc 
    = new DataColumn("dcode",typeof(string));
                DataColumn dc1 
    = new DataColumn("dname",typeof(string));
                dt.Columns.Add(dc);
                dt.Columns.Add(dc1);
                DataRow dr 
    = dt.NewRow();
                dr[
    0= "aa";
                dr[
    1= "aa";
                dt.Rows.Add(dr);

                grid.DataSource 
    = dt;

                DataGridTableStyle myGridStyle 
    = new DataGridTableStyle();
                myGridStyle.MappingName 
    = "T";


                DataGridTextBoxColumn style 
    = new DataGridTextBoxColumn();
                style.MappingName  
    = "dcode";
                style.HeaderText   
    = "DCODE";
                style.Width            
    = 100;
                myGridStyle.GridColumnStyles.Add(style);

                DataGridTextBoxColumn style1 
    = new DataGridTextBoxColumn();
                style1.MappingName  
    = "dname";
                style1.HeaderText   
    = "DName";
                style1.Width        
    = 100;

                myGridStyle.GridColumnStyles.Add(style1);
                grid.TableStyles.Add(myGridStyle);

            }



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

                button2.Enabled 
    = false;
            }

        }

    }


     

  • 相关阅读:
    NTP时钟服务器(卫星授时)技术让网络时间同步不再难
    视频监控系统为何需要GPS北斗授时服务器?
    如何选购GPS北斗卫星时钟同步装置?市场均价多少?
    Orange'S学习笔记(一)
    Oracle 11g中文版高清视频教程
    新书推荐:Orange'S:一个操作系统的实现
    引导扇区汇编代码解释
    灰度图转伪彩色图代码
    Visual Odometry For GPSDenied Flight And Mapping Using A Kinect
    [原创]日行一善,命运在不知不觉中改变
  • 原文地址:https://www.cnblogs.com/rock_chen/p/28347.html
Copyright © 2011-2022 走看看