在datagrid里添加CheckBox列、赋值、修改!!

类别:.NET开发 点击:0 评论:0 推荐:

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

namespace WindowsApplication1
{
 /// <summary>
 /// Form1 的摘要说明。
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  private System.Windows.Forms.DataGrid dataGrid1;
  private System.Windows.Forms.Button button1;
  private DataSet ds = new DataSet();
  private DataTable dtable;
  private System.Windows.Forms.DataGridTableStyle table;
  private System.Windows.Forms.DataGridTextBoxColumn stringcom;
  private System.Windows.Forms.DataGridBoolColumn boolcom;
  private System.Windows.Forms.Button button2;
  private System.Windows.Forms.Button button3;
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.Container components = null;

  public Form1()
  {
   //
   // Windows 窗体设计器支持所必需的
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
   //
  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   this.dataGrid1 = new System.Windows.Forms.DataGrid();
   this.button1 = new System.Windows.Forms.Button();
   this.table = new System.Windows.Forms.DataGridTableStyle();
   this.stringcom = new System.Windows.Forms.DataGridTextBoxColumn();
   this.boolcom = new System.Windows.Forms.DataGridBoolColumn();
   this.button2 = new System.Windows.Forms.Button();
   this.button3 = new System.Windows.Forms.Button();
   ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
   this.SuspendLayout();
   //
   // dataGrid1
   //
   this.dataGrid1.DataMember = "";
   this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
   this.dataGrid1.Location = new System.Drawing.Point(0, 8);
   this.dataGrid1.Name = "dataGrid1";
   this.dataGrid1.Size = new System.Drawing.Size(288, 168);
   this.dataGrid1.TabIndex = 0;
   this.dataGrid1.TableStyles.AddRange(new System.Windows.Forms.DataGridTableStyle[] {
                          this.table});
   //
   // button1
   //
   this.button1.Location = new System.Drawing.Point(0, 192);
   this.button1.Name = "button1";
   this.button1.Size = new System.Drawing.Size(72, 40);
   this.button1.TabIndex = 1;
   this.button1.Text = "显示";
   this.button1.Click += new System.EventHandler(this.button1_Click);
   //
   // table
   //
   this.table.DataGrid = this.dataGrid1;
   this.table.GridColumnStyles.AddRange(new System.Windows.Forms.DataGridColumnStyle[] {
                         this.stringcom,
                         this.boolcom});
   this.table.HeaderForeColor = System.Drawing.SystemColors.ControlText;
   this.table.MappingName = "Rock";
   //
   // stringcom
   //
   this.stringcom.Format = "";
   this.stringcom.FormatInfo = null;
   this.stringcom.HeaderText = "列1";
   this.stringcom.MappingName = "Band";
   this.stringcom.NullText = "";
   this.stringcom.Width = 75;
   //
   // boolcom
   //
   this.boolcom.FalseValue = "0";
   this.boolcom.HeaderText = "列2";
   this.boolcom.MappingName = "Song";
   this.boolcom.NullText = "";
   this.boolcom.NullValue = "";
   this.boolcom.TrueValue = "1";
   this.boolcom.Width = 75;
   //
   // button2
   //
   this.button2.Location = new System.Drawing.Point(80, 192);
   this.button2.Name = "button2";
   this.button2.Size = new System.Drawing.Size(72, 40);
   this.button2.TabIndex = 2;
   this.button2.Text = "修改方法1";
   this.button2.Click += new System.EventHandler(this.button2_Click);
   //
   // button3
   //
   this.button3.Location = new System.Drawing.Point(168, 192);
   this.button3.Name = "button3";
   this.button3.Size = new System.Drawing.Size(72, 40);
   this.button3.TabIndex = 3;
   this.button3.Text = "修改方法2";
   this.button3.Click += new System.EventHandler(this.button3_Click);
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(292, 273);
   this.Controls.Add(this.button3);
   this.Controls.Add(this.button2);
   this.Controls.Add(this.button1);
   this.Controls.Add(this.dataGrid1);
   this.Name = "Form1";
   this.Text = "Form1";
   this.Load += new System.EventHandler(this.Form1_Load);
   ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }

  
  private void CreateDataTable()
  {
   dtable = new DataTable("Rock");

   
   //set columns names
   dtable.Columns.Add("Band",typeof(System.String));
   dtable.Columns.Add("Song",typeof(System.String));
   dtable.Columns.Add("Album",typeof(System.String));

   //Add Rows
   DataRow drow = dtable.NewRow();
   drow["Band"] = "Iron Maiden";
   drow["Song"] = "0";
   drow["Album"] = "Ed Hunter";
   dtable.Rows.Add(drow);

   drow = dtable.NewRow();
   drow["Band"] = "Metallica";
   drow["Song"] = "1";
   drow["Album"] = "Metallica";
   dtable.Rows.Add(drow);
   
   drow = dtable.NewRow();
   drow["Band"] = "Jethro Tull";
   drow["Song"] = "0";
   drow["Album"] = "Aqualung";
   dtable.Rows.Add(drow);
   
   drow = dtable.NewRow();
   drow["Band"] = "Mr. Big";
   drow["Song"] = "1";
   drow["Album"] = "Japandemonium";
   dtable.Rows.Add(drow);
  }


  private void CreateDataTable2()
  {
   dtable = new DataTable("Rock");

   
   //set columns names
   dtable.Columns.Add("Band",typeof(System.String));
   dtable.Columns.Add("Song",typeof(System.String));
   dtable.Columns.Add("Album",typeof(System.String));

   //Add Rows
   DataRow drow = dtable.NewRow();
   drow["Band"] = "Iron Maiden";
   drow["Song"] = "1";
   drow["Album"] = "Ed Hunter";
   dtable.Rows.Add(drow);

   drow = dtable.NewRow();
   drow["Band"] = "Metallica";
   drow["Song"] = "1";
   drow["Album"] = "Metallica";
   dtable.Rows.Add(drow);
   
   drow = dtable.NewRow();
   drow["Band"] = "Jethro Tull";
   drow["Song"] = "1";
   drow["Album"] = "Aqualung";
   dtable.Rows.Add(drow);
   
   drow = dtable.NewRow();
   drow["Band"] = "Mr. Big";
   drow["Song"] = "1";
   drow["Album"] = "Japandemonium";
   dtable.Rows.Add(drow);
  }


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

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

   this.dataGrid1.DataSource=dtable.DefaultView;
  
  }

  private void button2_Click(object sender, System.EventArgs e)
  {
   CreateDataTable2();
   this.dataGrid1.DataSource=dtable.DefaultView;
  }

  private void button3_Click(object sender, System.EventArgs e)
  {
   if(this.dataGrid1[0,1].ToString()=="1")
   {
    this.dataGrid1[0,1]=false;
   }
   else
   {
    this.dataGrid1[0,1]=true;

   }
  
  }
 }
}

本文地址:http://com.8s8s.com/it/it43329.htm