.net2003中对DataGrid的常用操作
在DataGrid中,实行多选删除操作:在模板列中添加CheckBox工具即可
在后台代码中可以这样写:
/// <summary>
/// 删除所选车辆信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button2_Click(object sender, EventArgs e) //Button2这是删除按钮
{
foreach (DataGridItem dele in this.dgCar.Items)
{
CheckBox cb = (CheckBox)dele.FindControl("CheckBox1");
if (cb.Checked)
{
///执行删除方法就可以了
int i_ID = int.Parse(dele.Cells[0].Text);//表中位置id获得
SYJ.sqlServer corp_list_del = new SYJ.sqlServer(SYJ.common.gDbConnStr(""));
string del_sql = "delete from Ai_ObjComm where ID=" + i_ID;
corp_list_del.sqlInsertUpdateDelete(del_sql);
}
}
SYJ.common.msgBox(this, "所选车辆删除成功!");
this.car_infor_bind();
}
当然,可以为删除事件增加一个提示对话框:
就在Pageload事件的!Page.postback补充一句即可: this.btDel.Attributes.Add("OnClick", "javascript:return confirm('确认要删除吗?')");
2、DataGrid的删除事件:
protected void DataGrid1_DeleteCommand(object source, DataGridCommandEventArgs e)
{
int i = int.Parse(this.dg1.Items[e.Item.ItemIndex].Cells[0].Text);
SYJ.sqlServer del = new SYJ.sqlServer(SYJ.common.gDbConnStr(""));
string del_sql = "delete from Gps_User_List where User_ID=" + i;
if (del.sqlInsertUpdateDelete(del_sql) > 0)
{
SYJ.common.msgBox(this, "你已经删除用户" + this.dg1.Items[e.Item.ItemIndex].Cells[1].Text);
}
else
{
SYJ.common.msgBox(this, "删除失败");
}
this.user_bind();
}
3、删除确认对话框:
protected void DataGrid1_ItemCreated(object sender, DataGridItemEventArgs e)
{
switch (e.Item.ItemType)
{
case ListItemType.Item:
case ListItemType.AlternatingItem:
case ListItemType.EditItem:
TableCell myTableCell;
myTableCell = e.Item.Cells[10];
LinkButton myDeleteButton;
myDeleteButton = (LinkButton)myTableCell.Controls[0];
myDeleteButton.Attributes.Add("onclick", "return confirm('您是否确定要删除此用户吗?');"); break;
default: break;
}
}
4、DataGrid的选择事件:
private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
{
this.TextBox1.Text=this.DataGrid1.Items[this.DataGrid1.SelectedIndex].Cells[6].Text;
this.Label1.Text=this.DataGrid1.Items[this.DataGrid1.SelectedIndex].Cells[0].Text;
}
5、DataGrid的分页事件:
protected void DataGrid1_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
{
this.dg1.CurrentPageIndex = e.NewPageIndex;
this.user_bind();
}
6、//DataGrid的更新事件
private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
int a_id=int.Parse(this.DataGrid1.Items[e.Item.ItemIndex].Cells[0].Text);获取修改id号
string pwd=this.DataGrid1.Items[e.Item.ItemIndex].Cells[2].Text;//原密码
TextBox Admin_name_txt=(TextBox)e.Item.Cells[1].Controls[0];
Admin_name_txt.Width=100;
TextBox Admin_pwd_txt=(TextBox)e.Item.Cells[2].Controls[0];
TextBox Admin_right_txt=(TextBox)e.Item.Cells[3].Controls[0];
Admin_right_txt.Width=50;
string Admin_name=Admin_name_txt.Text;
string Admin_pwd=WebTools.WebTools.EncryptPassword(Admin_pwd_txt.Text,"MD5");
string Admin_right=Admin_right_txt.Text;
BusinessEntity.CAdmin c_admin=new BusinessEntity.CAdmin();
c_admin.A_id=a_id;
c_admin.Admin_name=Admin_name;
c_admin.Admin_right=Convert.ToInt32(Admin_right);
BusinessLogic.CAdmin ca_admin=new BusinessLogic.CAdmin();
if(ca_admin.update_Admin(c_admin)>0)
{
WebTools.WebTools.MessageBox("恭喜你,修改成功");
}
else
{
WebTools.WebTools.MessageBox("对不起,系统忙!");
}
this.DataGrid1_CancelCommand(null,null);
}
7、//DataGrid中的(编辑)取消事件
private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
this.DataGrid1.EditItemIndex=-1;
this.Admin_Bind();
}
8、//DataGrid中的编辑事件
private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
this.DataGrid1.EditItemIndex=e.Item.ItemIndex;
this.Admin_Bind();
}