public class ShowOra : Page { public Label labShow; public DataGrid MyDataGrid; public OleDbConnection Conn; public OleDbCommand Cmd; public OleDbDataReader Dr;
OleDbDataAdapter myCommand = new OleDbDataAdapter("select * from UserMain order by ID", Conn); DataSet ds = new DataSet(); myCommand.Fill(ds, "UserMain"); MyDataGrid.DataSource=ds.Tables["UserMain"].DefaultView; //MyDataGrid.DataSource=CreateDataSource(Cmd.ExecuteReader());
MyDataGrid.DataBind(); } public void BindGrid() { OleDbDataAdapter myCommand = new OleDbDataAdapter("select * from UserMain order by ID", Conn); DataSet ds = new DataSet(); myCommand.Fill(ds, "UserMain"); MyDataGrid.DataSource=ds.Tables["UserMain"].DefaultView; MyDataGrid.DataBind(); } ICollection CreateDataSource(OleDbDataReader tRs) { DataTable dt = new DataTable(); DataRow dr;
{
public Label labShow;
public DataGrid MyDataGrid;
public OleDbConnection Conn;
public OleDbCommand Cmd;
public OleDbDataReader Dr;
public void Page_Load(Object str,EventArgs e){
string tStr="";
Int32 tCs=0;
//string sConn="Provider=MSDAORA.1;Password=yhgl;Persist Security Info=True;User ID=yhgl;Data Source=HcDbForWork;Extended Properties=''";
string connectionString ="Provider=MSDAORA.1;Password=yhgl;User ID=yhgl;Data Source=HcDbForWork;";
Conn=new OleDbConnection(connectionString);
string sSql="Select * From UserMain Where (1=1)";
//sSql=sSql + " order by Int(ID)";
Cmd=new OleDbCommand(sSql,Conn);
Cmd.Connection.Open();
Dr=Cmd.ExecuteReader();
Int32 i=0;
while(Dr.Read()){
tStr=tStr + "编号:" + Dr["ID"].ToString();
tStr=tStr + "用户名:" + Dr["UserName"].ToString();
tStr=tStr + "用户密码:" + Dr["UserPassWord"].ToString();
tStr=tStr + "部门:" + Dr["bm"].ToString()+"<br>";
i++;
}
tCs++;
Dr.Close();
labShow.Text=tStr;
OleDbDataAdapter myCommand = new OleDbDataAdapter("select * from UserMain order by ID", Conn);
DataSet ds = new DataSet();
myCommand.Fill(ds, "UserMain");
MyDataGrid.DataSource=ds.Tables["UserMain"].DefaultView;
//MyDataGrid.DataSource=CreateDataSource(Cmd.ExecuteReader());
MyDataGrid.DataBind();
}
public void BindGrid()
{
OleDbDataAdapter myCommand = new OleDbDataAdapter("select * from UserMain order by ID", Conn);
DataSet ds = new DataSet();
myCommand.Fill(ds, "UserMain");
MyDataGrid.DataSource=ds.Tables["UserMain"].DefaultView;
MyDataGrid.DataBind();
}
ICollection CreateDataSource(OleDbDataReader tRs)
{
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add(new DataColumn("编号", typeof(string)));
dt.Columns.Add(new DataColumn("用户名", typeof(string)));
dt.Columns.Add(new DataColumn("用户密码", typeof(string)));
dt.Columns.Add(new DataColumn("部门", typeof(string)));
dt.Columns.Add(new DataColumn("Select", typeof(string))); while(tRs.Read())
{
dr = dt.NewRow();
dr[0] = tRs["ID"].ToString();
dr[1] = tRs["UserName"].ToString();
dr[2] = tRs["UserPassWord"].ToString();
dr[3] = tRs["bm"].ToString();
dr[4] = tRs["ID"].ToString(); dt.Rows.Add(dr);
}
DataView dv = new DataView(dt);
return dv;
}
public void MyDataGrid_onItemCommand(Object sender,DataGridCommandEventArgs e)
{
TableRow tr=e.Item;
String tStr=tr.Cells[1].Text;
tStr=tStr + tr.Cells[2].Text;
tStr=tStr + tr.Cells[3].Text;
tStr=tStr + tr.Cells[4].Text;
labShow.Text=tStr;
}
public void MyDataGrid_EditCommand(Object sender,DataGridCommandEventArgs e)
{
MyDataGrid.EditItemIndex=(int)e.Item.ItemIndex;
BindGrid();
labShow.Text="Edit";
}
public void MyDataGrid_CancelCommand(Object sender,DataGridCommandEventArgs e)
{
MyDataGrid.EditItemIndex=-1;
BindGrid();
labShow.Text="Cancel";
}
public void MyDataGrid_UpdateCommand(Object sender,DataGridCommandEventArgs e)
{
TextBox tName=(TextBox)e.Item.Cells[2].Controls[0];
TextBox tPwd=(TextBox)e.Item.Cells[3].Controls[0];
TextBox tBm=(TextBox)e.Item.Cells[4].Controls[0];
String tSql="Update UserMain Set "+
"UserName='"+tName.Text+"',"+
"Bm='"+tBm.Text+"',"+
"UserPassWord='"+tPwd.Text+"' Where ID='"+e.Item.Cells[2].Text+"'";
labShow.Text=tSql +"<br>Update";
Cmd.CommandText=tSql;
Cmd.ExecuteNonQuery();
MyDataGrid.EditItemIndex=-1;
BindGrid();
}
// 构造函数
public ShowOra()
{
Page.Init += new System.EventHandler(Page_Init);
}
private void Page_Init(object sender, EventArgs e)
{
InitializeComponent();
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
MyDataGrid.CancelCommand +=new DataGridCommandEventHandler(this.MyDataGrid_CancelCommand);
MyDataGrid.EditCommand +=new DataGridCommandEventHandler(this.MyDataGrid_EditCommand);
MyDataGrid.UpdateCommand +=new DataGridCommandEventHandler(this.MyDataGrid_UpdateCommand);
//MyDataGrid.ItemCommand +=new DataGridCommandEventHandler(this.MyDataGrid_onItemCommand);
} }这是我的程序的完整代码
MyDataGrid.UpdateCommand +=new DataGridCommandEventHandler(this.MyDataGrid_UpdateCommand);
我这样定义事件后,事件没有被执行。
但是MyDataGrid.EditCommand却可以执行,怎样解决?
MyDataGrid.EditItemIndex=(int)e.Item.ItemIndex;
BindGrid();
labShow.Text="Edit";
}
去了这句话就可以!