首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

關于DataTable中,有設置Expression屬性的列不會激發ColumnChanging事件解决办法

2012-01-28 
關于DataTable中,有設置Expression屬性的列不會激發ColumnChanging事件usingSystemusingSystem.Drawingu

關于DataTable中,有設置Expression屬性的列不會激發ColumnChanging事件
using   System;
using   System.Drawing;
using   System.Collections;
using   System.ComponentModel;
using   System.Windows.Forms;
using   System.Data;
using   System.Data.SqlClient;

namespace   test
{
///   <summary>
///   Form4   的摘要描述。
///   </summary>
public   class   Form4   :   System.Windows.Forms.Form
{
private   System.Windows.Forms.DataGrid   dataGrid1;
private   DataSet   myDataSet=new   DataSet();
///   <summary>
///   設計工具所需的變數。
///   </summary>
private   System.ComponentModel.Container   components   =   null;

public   Form4()
{
InitializeComponent();
}

///   <summary>
///   清除任何使用中的資源。
///   </summary>
protected   override   void   Dispose(   bool   disposing   )
{
if(   disposing   )
{
if(components   !=   null)
{
components.Dispose();
}
}
base.Dispose(   disposing   );
}

#region   Windows   Form   設計工具產生的程式碼
///   <summary>
///   此為設計工具支援所必須的方法   -   請勿使用程式碼編輯器修改
///   這個方法的內容。
///   </summary>
private   void   InitializeComponent()
{
this.dataGrid1   =   new   System.Windows.Forms.DataGrid();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//  
//   dataGrid1
//  
this.dataGrid1.DataMember   =   " ";
this.dataGrid1.Dock   =   System.Windows.Forms.DockStyle.Fill;
this.dataGrid1.HeaderForeColor   =   System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location   =   new   System.Drawing.Point(0,   0);
this.dataGrid1.Name   =   "dataGrid1 ";
this.dataGrid1.Size   =   new   System.Drawing.Size(568,   357);
this.dataGrid1.TabIndex   =   0;
//  
//   Form4
//  
this.AutoScaleBaseSize   =   new   System.Drawing.Size(5,   15);
this.ClientSize   =   new   System.Drawing.Size(568,   357);
this.Controls.Add(this.dataGrid1);
this.Name   =   "Form4 ";
this.Text   =   "Form4 ";
this.Load   +=   new   System.EventHandler(this.Form4_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);

}
#endregion

private   void   Form4_Load(object   sender,   System.EventArgs   e)
{
using(SqlDataAdapter   ada=new   SqlDataAdapter( "   SELECT   *, ' '   as   tmp   FROM   Categories ", "data   Source=.;database=Northwind;user   id=sa;pwd=; "))
{
ada.Fill(this.myDataSet, "tt ");
this.myDataSet.Tables[0].Columns[ "tmp "].Expression= "iif(CategoryID> 3, 'small ', 'big ') ";
this.myDataSet.Tables[0].Columns.Add( "tmp1 ",typeof(string));
this.myDataSet.Tables[0].Columns.Add( "tmp2 ",typeof(string), "iif(CategoryID> 3, 'small ', 'big ') ");
this.myDataSet.Tables[0].ColumnChanging+=new   DataColumnChangeEventHandler(this.DataColumn_ColumnChanging);


this.dataGrid1.DataSource=this.myDataSet.Tables[0];
#region   將增加的tmp2列修改為可讀寫
DataGridTableStyle   dgts=new   DataGridTableStyle();
dgts.MappingName=this.myDataSet.Tables[0].TableName;
this.dataGrid1.TableStyles.Clear();
this.dataGrid1.TableStyles.Add(dgts);
DataGridColumnStyle   dgcs   =   dgts.GridColumnStyles[ "tmp "];
(dgcs   as   DataGridTextBoxColumn).ReadOnly=false;
dgcs   =   dgts.GridColumnStyles[ "tmp2 "];
(dgcs   as   DataGridTextBoxColumn).ReadOnly=false;
#endregion
}
}

private   void   DataColumn_ColumnChanging(object   sender,System.Data.DataColumnChangeEventArgs   e)
{
#region   輸入資料檢測
try
{
if(e.Row[e.Column.ColumnName,(e.Row.RowState==DataRowState.Detached?DataRowVersion.Proposed:(e.Row.RowState==DataRowState.Added?DataRowVersion.Default:DataRowVersion.Original))]!=e.ProposedValue)
{
switch(e.Column.ColumnName.ToLower())
{
case   "tmp1 ":
MessageBox.Show(e.Column.ColumnName+ "\r\n\r\n "+e.ProposedValue.ToString());
break;
case   "tmp2 ":
MessageBox.Show(e.Column.ColumnName+ "\r\n\r\n "+e.ProposedValue.ToString());
break;
default:
MessageBox.Show(e.Column.ColumnName);
break;
}
}
}
catch(Exception   ex)
{
MessageBox.Show(ex.Message, "Error ",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
#endregion
}
}
}
tmp和tmp列有設置Expression屬性.
說明:
1.增加時新行時,會引發有設置Expression屬性的列的DataColukning事件;
2.修改行時,不會引發有設置Expression屬性的列的DataColukning事件.

如何才能在修改有設置Expression屬性列的值時引發DataColumning事件?

[解决办法]
不结贴可不是好孩子呀

热点排行