已经把数据库的数据显示在dataGrid里面了,我现在要实现对某一行的删除操作,也就是实现鼠标点击后任一行的删除操作,这是怎么实现的?麻烦高手写些代码举例一下(包括一系列的操作)

解决方案 »

  1.   

    里面有个DELETE 按钮。根据ID写个DELETE SQL 
      

  2.   

    没找到啊?我要在按键里实现这个删除操作的,但是首先要选中dataGrid中的指定某一行
      

  3.   

    datagrid有个属性autodeletecolumn..什么之类的,即显示自动删除列,你找下看看
      

  4.   

    我用的是.Net2003的,怎么都没找到你们说的那些属性呢?
      

  5.   

    31    //删除
    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的。
      

  6.   

    其实方法是多样的,你可以添加一下select 列还它的selectindexchange事件里用e.item就表示你当透明选择的一行了,
      

  7.   

      <asp:GridView ID="mGrid" runat="server" AutoGenerateColumns="False" OnRowCommand="mGrid_RowCommand" Width=100%
            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")
            {
                       }
        }
      

  8.   

    楼上的怎么做成ASP的了,我只是用C#做的,是C/S的而已,怎么有些人讲得很简单,但是我又找不到,有些人写了一些代码,但是有好像不符合我的要求,郁闷!!
      

  9.   

    兄弟,你连ASP跟asp.net的代码都分不清,你还是多看看书吧,别人给你代码对你也没有用的,每个人都有自己的方法,你只有自己掌握了知识,才能真正的使用编程这项技术