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);
声明:本站部分数据来源于网络,仅供参考,如有版权问题,请联系我们,我们将及时删除!转载本站请注明来源
|