已经把数据库的数据显示在dataGrid里面了,我现在要实现对某一行的删除操作,也就是实现鼠标点击后任一行的删除操作,这是怎么实现的?麻烦高手写些代码举例一下(包括一系列的操作)
解决方案 »
- UpdateLayeredWindow贴图效率问题(局部刷新)
- (200分)关于线程锁的问题!!!谢谢
- 大家是否可以给我一份SiteMapPath的事例,谢谢
- 还请大家一起在这里跟贴,写出你们自己知道的C#图书下载地址,供学习!!!!!!!!!!!
- 关于sqlce判断表是否存在的问题
- Windows2000下如何运行C#编写的程序?
- Web应用问题
- 如何打印图片,数据库中存放的是图片的路径.
- 急死了,搞了一天没搞定,看尽了CSDN上的问题也没有试成功,各位老大帮忙啊!!!
- 如何不用COPSSH这个软件搭建git服务器
- 分是少点,大家帮帮手C#数据库的问题
- 怎么样使Winfom上的控件,随窗体的大小来按比例改变大小?
32 protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
33 {
34 string sqlstr = "delete from 表 where id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
35 sqlcon = new SqlConnection(strCon);
36 sqlcom = new SqlCommand(sqlstr,sqlcon);
37 sqlcon.Open();
38 sqlcom.ExecuteNonQuery();
39 sqlcon.Close();
40 bind();
41 }
42
参考着改成datagrid的。
BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3" OnRowDeleting="mGrid_RowDeleting" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" OnRowUpdating="mGrid_RowUpdating" OnRowDataBound="mGrid_RowDataBound">
<Columns>
<asp:BoundField DataField="DateEx" HeaderText="日期" >
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" ForeColor="Black" />
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:BoundField>
<asp:BoundField DataField="NoEx" HeaderText="No">
<ItemStyle ForeColor="Black" HorizontalAlign="Center" VerticalAlign="Middle" />
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:BoundField>
<asp:BoundField DataField="StartTimeEx" HeaderText="开始时间" >
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" ForeColor="Black" />
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:BoundField>
<asp:BoundField DataField="EndTimeEx" HeaderText="结束时间" >
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" ForeColor="Black" />
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:BoundField>
<asp:BoundField DataField="AmtEx" HeaderText="外出费用" >
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" ForeColor="Black" />
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:BoundField>
<asp:BoundField DataField="Status" HeaderText="是否审核">
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:BoundField>
<asp:BoundField DataField="Re" HeaderText="备注" >
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" ForeColor="Black" />
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="70px" />
</asp:BoundField>
<asp:ButtonField CommandName="Update" Text="编辑">
<ItemStyle ForeColor="Black" HorizontalAlign="Center" VerticalAlign="Middle" />
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:ButtonField>
<asp:ButtonField CommandName="Delete" Text="删除" >
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" ForeColor="Black" />
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="50px" />
</asp:ButtonField>
</Columns>
<FooterStyle BackColor="White" ForeColor="#000066" />
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
</asp:GridView>
后台
/// <summary>
/// 删除记录
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void mGrid_RowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
int RowNo;
RowNo = Convert.ToInt32(e.CommandArgument);
lblErrMsg.Text = "";
Att_BusinessRule_AttGoOut GoOutBusiness = new Att_BusinessRule_AttGoOut();
AttendanceGoOutEntity GoOutData = new AttendanceGoOutEntity();
SessionEntity session = (SessionEntity)Session["SysSessionData"];
if (e.CommandName == "Update")
{
GoOutData.Date = Convert.ToDateTime(mGrid.Rows[RowNo].Cells[0].Text);
GoOutData.No = Convert.ToInt32(mGrid.Rows[RowNo].Cells[1].Text);
GoOutData.StartTime = Convert.ToDateTime(mGrid.Rows[RowNo].Cells[2].Text);
GoOutData.EndTime = Convert.ToDateTime(mGrid.Rows[RowNo].Cells[3].Text);
GoOutData.Amt = Convert.ToDouble(mGrid.Rows[RowNo].Cells[4].Text);
GoOutData.Re = mGrid.Rows[RowNo].Cells[6].Text;
session.BusinessData = GoOutData; }
if (e.CommandName == "Delete")
{
GoOutData.Date = Convert.ToDateTime(mGrid.Rows[RowNo].Cells[0].Text);
GoOutData.No = Convert.ToInt32(mGrid.Rows[RowNo].Cells[1].Text);
GoOutData.StartTime = Convert.ToDateTime(mGrid.Rows[RowNo].Cells[2].Text);
GoOutData.EndTime = Convert.ToDateTime(mGrid.Rows[RowNo].Cells[3].Text);
GoOutData.UpdateType = 3;
session.BusinessData = GoOutData;
GoOutBusiness.SessionData = session;
GoOutBusiness.GlobalData = (GlobalEntity)Application["SysGlobalData"];
if (mGrid.Rows[RowNo].Cells[5].Text == "1")
{
GoOutBusiness.UpdateData();
}
else
{
this.lblErrMsg.Text = "此记录已被审核,不能删除!";
}
IList<AttendanceGoOutEntity> GoOutdata = (IList<AttendanceGoOutEntity>)GoOutBusiness.GetDataByKeys();
SetmGridData(GoOutdata);
}
}
catch(Exception ex)
{
this.lblErrMsg.Text = ex.Message;
}
if(lblErrMsg.Text=="" && e.CommandName == "Update")
{
}
}