搜索墙首页  编程技术  java  asp  ajax  php  c/c#/c++  数据库  oracle  mysql  db2  操作系统  windows  linux  股吧
首页 > 编程 > C#/C/C++
 1         
1楼  ivanl 2007-10-03 12:18:49

以下程序当在datagridview加一行时就是没反应! 已我经将datatable与dataset关联,dataset通过bindingsource控件与datagridview关联.

using System;
using System.Data;
using System.Windows.Forms;

class Test : Form
{
  static void Main()
  {
      Application.Run(new Test());
  }
 
  Test()
  {
      DataTable DataTable1 = new DataTable();
      DataTable1.TableNewRow += new DataTableNewRowEventHandler(MyTableNewRow);
  }
 
  void MyTableNewRow(Object sender,    DataTableNewRowEventArgs e)
  {
          MessageBox.Show("hello");
  }
}
2楼  applethink 2007-10-03 14:01:29

应该不会啊,看下面示例:
C# code
namespace WindowsApplication3 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } // Put the next line into the Declarations section. private System.Data.DataSet dataSet; private void MakeDataTables() { MakeParentTable(); BindToDataGrid(); } private void MakeParentTable() { // Create a new DataTable. System.Data.DataTable table = new DataTable("ParentTable"); table.TableNewRow += new DataTableNewRowEventHandler(table_TableNewRow); // Declare variables for DataColumn and DataRow objects. DataColumn column; DataRow row; // Create new DataColumn, set DataType, // ColumnName and add to DataTable. column = new DataColumn(); column.DataType = System.Type.GetType("System.Int32"); column.ColumnName = "id"; column.ReadOnly = false; column.Unique = true; // Add the Column to the DataColumnCollection. table.Columns.Add(column); // Create second column. column = new DataColumn(); column.DataType = System.Type.GetType("System.String"); column.ColumnName = "ParentItem"; column.AutoIncrement = false; column.Caption = "ParentItem"; column.ReadOnly = false; column.Unique = false; // Add the column to the table. table.Columns.Add(column); // Make the ID column the primary key column. DataColumn[] PrimaryKeyColumns = new DataColumn[1]; PrimaryKeyColumns[0] = table.Columns["id"]; table.PrimaryKey = PrimaryKeyColumns; // Instantiate the DataSet variable. dataSet = new DataSet(); // Add the new DataTable to the DataSet. dataSet.Tables.Add(table); // Create three new DataRow objects and add // them to the DataTable for (int i = 0; i <= 2; i++) { row = table.NewRow(); row["id"] = i; row["ParentItem"] = "ParentItem " + i; table.Rows.Add(row); } } private void BindToDataGrid() { // Instruct the DataGrid to bind to the DataSet, with the dataGridView1.DataSource = this.dataSet.Tables[0]; } void table_TableNewRow(object sender, DataTableNewRowEventArgs e) { MessageBox.Show("hello"); } private void Form1_Load(object sender, EventArgs e) { this.MakeDataTables(); } } }
3楼  ldarmy 2007-10-03 14:31:44

测了一下,没出现楼主所说的情况,当点DataGridView的最后一行时会触发事件
C# code
{ DataTable dt = new DataTable(); dt.Columns.Add(new DataColumn("id")); dt.Columns.Add(new DataColumn("name")); DataRow dr = null; dr = dt.NewRow(); dr["id"] = "001"; dr["name"] = "Name1"; dt.Rows.Add(dr); dr = dt.NewRow(); dr["id"] = "002"; dr["name"] = "Name2"; dt.Rows.Add(dr); dt.TableNewRow += new DataTableNewRowEventHandler(dt_NewTableRow); dataGridView1.DataSource = dt; } protected void dt_NewTableRow(Object sender, DataTableNewRowEventArgs e) { MessageBox.Show("hello"); }
4楼  qingniaoIT 2007-10-03 14:56:14

这个事件本来就是在控件上新增一行时发生, 但好像不针对数据绑定这种情况.
5楼  ivanl 2007-10-04 01:16:26

知了道,谢谢!原来我的问题是出在datable还没关联到数据之前就定义了dt.TableNewRow += new DataTableNewRowEventHandler(dt_NewTableRow);

把这句话放在datatable关联dataset之后就有反应了.这跟vb.net有很大差别呀!
dtInmovd = dsINF001.Tables["inmovd"];

this.dtInmovd.TableNewRow += new System.Data.DataTableNewRowEventHandler(this.dtInmovd_NewRow);
 1         
您的发言将按有关规定都会存档,您须为所发表后果负责,请您遵纪守法并注意语言文明。
标题:请教大家怎样使用datatable中已有的事件?C# 中怎么没反应?
热门关注
标题回复点击
c# 匿名方法 泛型委托 List<string>101615
我今年28了开始学C#,有前途吗?97831
c++ 输出图像到网页0519
OnServerclick 如何用ctrl+enter提交表单(分不够了)0489
C#技术资料 教程0488
c++ 中使用ChartDirector输出图表的网页0462
在C#中如何通过拼IP地址检测某台机器的网络连接是否畅通4457
各位哪里有下载 Visio 20051448
XPath Example<2>0378
做个调查,有多少人用C#做WINFORM?199349
搜索墙@2009 www.pkwall.com all rights reserved QQ:276471788 [京ICP备09111534号]
声明:本站部分数据来源于网络,仅供参考,如有版权问题,请联系我们,我们将及时删除!转载本站请注明来源