求dataGrid和Sql绑定的代码,可以添加,修改,删除记录

解决方案 »

  1.   

    说起来好多,给你个示例看看吧:
    http://blog.csdn.net/voodgen/archive/2004/09/25/116821.aspx
      

  2.   

    晕   给你这个吧  很久以前我写的  呵呵  供参考
    //公共函数BindGrid,绑定DataGrid
    private void BindGrid()
    {
    this.sqlDataAdapter1.Fill(this.dataSet11);
    this.DataGrid1.DataBind();
    } private void Page_Load(object sender, System.EventArgs e)
    {
    if(this.IsPostBack)
    return;
    BindGrid();   //调用绑定函数


    //取得数据库编号列的行数和总数

    string xx = "select count(categoryCode) from TP_ProjectCategory ";
    SqlCommand aaaCommand = new SqlCommand();
    aaaCommand.Connection = this.sqlConnection1;
    aaaCommand.CommandType = CommandType.Text;
    aaaCommand.CommandText = xx;
    aaaCommand.Connection.Close();
    aaaCommand.Connection.Open();
    string cxscxs =aaaCommand.ExecuteScalar().ToString();
    this.Label6.Text = "现在共有"+cxscxs+"条记录";

    string ww = "select max(categoryCode) from TP_ProjectCategory ";
    SqlCommand myCommand = new SqlCommand();
    myCommand.Connection = this.sqlConnection1;
    myCommand.CommandType = CommandType.Text;
    myCommand.CommandText = ww;
    myCommand.Connection.Close();
    myCommand.Connection.Open();
    string cxs = myCommand.ExecuteScalar().ToString();
    this.Label5.Text = "编号最大为"+cxs;
    }
      

  3.   

    private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
    {

    } //DataGrid分页控制
    private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
    {
    this.sqlDataAdapter1.Fill(this.dataSet11);
    this.DataGrid1.CurrentPageIndex=e.NewPageIndex;
    this.DataGrid1.DataBind();
    } //编辑DataGrid的行记录
    private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    this.DataGrid1.EditItemIndex=e.Item.ItemIndex;
    this.DataGrid1.EditItemStyle.Width = '5';
    BindGrid();
    } //更新DataGrid的行记录
    private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    this.sqlDataAdapter1.UpdateCommand.Parameters["@Original_categoryCode"].Value=this.DataGrid1.DataKeys[e.Item.ItemIndex];
    this.sqlDataAdapter1.UpdateCommand.Parameters["@categoryName"].Value=((TextBox)e.Item.Cells[1].Controls[0]).Text;
    this.sqlDataAdapter1.UpdateCommand.Connection.Open();
    this.sqlDataAdapter1.UpdateCommand.ExecuteNonQuery();
    this.sqlDataAdapter1.UpdateCommand.Connection.Close();
    this.DataGrid1.EditItemIndex=-1;
    BindGrid();
    }
      

  4.   

    //DataGrid分页控制
    private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
    {
    this.sqlDataAdapter1.Fill(this.dataSet11);
    this.DataGrid1.CurrentPageIndex=e.NewPageIndex;
    this.DataGrid1.DataBind();
    }//编辑DataGrid的行记录
    private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    this.DataGrid1.EditItemIndex=e.Item.ItemIndex;
    this.DataGrid1.EditItemStyle.Width = '5';
    BindGrid();
    } //更新DataGrid的行记录
    private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    this.sqlDataAdapter1.UpdateCommand.Parameters["@Original_categoryCode"].Value=this.DataGrid1.DataKeys[e.Item.ItemIndex];
    this.sqlDataAdapter1.UpdateCommand.Parameters["@categoryName"].Value=((TextBox)e.Item.Cells[1].Controls[0]).Text;
    this.sqlDataAdapter1.UpdateCommand.Connection.Open();
    this.sqlDataAdapter1.UpdateCommand.ExecuteNonQuery();
    this.sqlDataAdapter1.UpdateCommand.Connection.Close();
    this.DataGrid1.EditItemIndex=-1;
    BindGrid();
    } //添加DataGrid的新记录
    private void Button1_Click(object sender, System.EventArgs e)
    {

    //验证输入框
    if((this.TextBox1.Text=="")||(this.TextBox2.Text=="")) Response.Write("<script>alert('必须输入编号和名称!')</script>");
    else
    {
    string kk = "select count(categoryCode) from TP_ProjectCategory where categoryCode='"+ this.TextBox1.Text.Trim() + "'";
    this.sqlDataAdapter2.SelectCommand.CommandText = kk;
    this.sqlDataAdapter2.SelectCommand.Connection.Close();
    this.sqlDataAdapter2.SelectCommand.Connection.Open();

      
    int num = (int)this.sqlDataAdapter2.SelectCommand.ExecuteScalar();

    if(num>0) Response.Write("<script>alert('方案类别编号已存在!请重新输入!')</script>");

    //写入数据库
    else
    {
    this.sqlDataAdapter1.InsertCommand.Parameters["@categoryCode"].Value=this.TextBox1.Text;
    this.sqlDataAdapter1.InsertCommand.Parameters["@categoryName"].Value=this.TextBox2.Text;
    //this.sqlDataAdapter1.InsertCommand.Connection.Open();
    this.sqlDataAdapter1.InsertCommand.ExecuteNonQuery();
    this.sqlDataAdapter1.InsertCommand.Connection.Close();
    BindGrid();
    this.Label4.Text=""+ this.TextBox2.Text.Trim() + "输入成功!!!";

    //更新DataGrid编号列最大值和行总数
    string xx = "select count(categoryCode) from TP_ProjectCategory ";
    SqlCommand aaaCommand = new SqlCommand();
    aaaCommand.Connection = this.sqlConnection1;
    aaaCommand.CommandType = CommandType.Text;
    aaaCommand.CommandText = xx;
    aaaCommand.Connection.Close();
    aaaCommand.Connection.Open();
    string cxscxs = aaaCommand.ExecuteScalar().ToString();
    this.Label6.Text = "现在共有"+cxscxs+"条记录";

    string ww = "select max(categoryCode) from TP_ProjectCategory ";
    SqlCommand myCommand = new SqlCommand();
    myCommand.Connection = this.sqlConnection1;
    myCommand.CommandType = CommandType.Text;
    myCommand.CommandText = ww;
    myCommand.Connection.Close();
    myCommand.Connection.Open();
    string cxs = myCommand.ExecuteScalar().ToString();
    this.Label5.Text = "编号最大为"+cxs;
    }
    }
        
    }
      

  5.   

    晕  连续的回复不能超过三次 我只好用我MM的号来回了  吼吼
    //删除DataGrid的当前行记录
    private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
       this.sqlDataAdapter1.DeleteCommand.Parameters["@categoryCode"].Value=this.DataGrid1.DataKeys[e.Item.ItemIndex];
       this.sqlDataAdapter1.DeleteCommand.Connection.Open();
       this.sqlDataAdapter1.DeleteCommand.ExecuteNonQuery();
       this.sqlDataAdapter1.DeleteCommand.Connection.Close();
       
    if(this.DataGrid1.Items.Count==1)
      this.DataGrid1.CurrentPageIndex=this.DataGrid1.CurrentPageIndex-1;

    BindGrid(); //更新DataGrid编号列最大值和行总数
    string xx = "select count(categoryCode) from TP_ProjectCategory ";
    SqlCommand aaaCommand = new SqlCommand();
    aaaCommand.Connection = this.sqlConnection1;
    aaaCommand.CommandType = CommandType.Text;
    aaaCommand.CommandText = xx;
    aaaCommand.Connection.Close();
    aaaCommand.Connection.Open();
    string cxscxs = aaaCommand.ExecuteScalar().ToString();
    this.Label6.Text = "现在共有"+cxscxs+"条记录";

    string ww = "select max(categoryCode) from TP_ProjectCategory ";
    SqlCommand myCommand = new SqlCommand();
    myCommand.Connection = this.sqlConnection1;
    myCommand.CommandType = CommandType.Text;
    myCommand.CommandText = ww;
    myCommand.Connection.Close();
    myCommand.Connection.Open();
    string cxs = myCommand.ExecuteScalar().ToString();
    this.Label5.Text = "编号最大为"+cxs;
    } //取消编辑状态
    private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    this.DataGrid1.EditItemIndex=-1;
    BindGrid();
    }基本上就是这些了  改改加加的就可以用了
      

  6.   

    '分类不同,提取不同绑定显示
        Sub selectdown(ByVal sender As System.Object, ByVal e As EventArgs)
            If MenuClass.SelectedValue = "0" Then
                Response.Write("<script>alert('请选择分类!')</script>")
            End If
            If MenuClass.SelectedValue = "1" Then
                listfirstclass.Visible = True
                ' Try
                bind()
                ' Catch ex As Exception
                'End Try
            Else
                listfirstclass.Visible = False
            End If
            If MenuClass.SelectedValue = "2" Then
                se_panel.Visible = True
                ' Try
                bind()
                ' Catch ex As Exception
                'End Try
            Else
                If MenuClass.SelectedValue = "3" Then
                    se_panel.Visible = True
                Else
                    se_panel.Visible = False
                End If
            End If
            If MenuClass.SelectedValue = "3" Then
                se_panel.Visible = True
                ' Try
                bind()
                ' Catch ex As Exception
                'End Try
            Else
                If MenuClass.SelectedValue = "2" Then
                    se_panel.Visible = True
                Else
                    se_panel.Visible = False
                End If
            End If
        End Sub
        '编辑状态
        Sub editdata(ByVal obj As Object, ByVal e As DataGridCommandEventArgs)
            Select Case MenuClass.SelectedValue
                Case "1"
                    firstclass.EditItemIndex = e.Item.ItemIndex
                    bind()
                Case "2"
                    Sedatagrid.EditItemIndex = e.Item.ItemIndex
                    bind()
                Case "3"
                    Sedatagrid.EditItemIndex = e.Item.ItemIndex
                    bind()
            End Select
        End Sub
        '更新数据
        Sub updata(ByVal obj As Object, ByVal e As DataGridCommandEventArgs)
            Dim sqlstr As String
            Select Case MenuClass.SelectedValue
                Case "1"
                    sqlstr = "update SysMenuClass set ClassName='" & CType(e.Item.FindControl("ClassName"), TextBox).Text _
                   & "',MenuOrder=" & CType(e.Item.FindControl("menuorder"), TextBox).Text & ",Logo='" _
                   & CType(e.Item.FindControl("Logo"), TextBox).Text & "' where Id=" & CType(e.Item.FindControl("Id"), Label).Text
                    '// Response.Write(CType(e.Item.FindControl("Id"), Label).Text)
                Case "2"
                    sqlstr = "update SysFirstMenu set ClassName='" & CType(e.Item.FindControl("Se_ClassName"), TextBox).Text _
                              & "',MenuOrder=" & CType(e.Item.FindControl("Se_MenuOrder"), TextBox).Text & ",Link='" _
                          & CType(e.Item.FindControl("Se_Link"), TextBox).Text & "',UpClass=" & CType(e.Item.FindControl("seupclass"), DropDownList).SelectedValue _
                          & " where Id=" & CType(e.Item.FindControl("Se_Id"), Label).Text
                    '// Response.Write(sqlstr)
                Case "3"
                    sqlstr = "update SysSecondMenu set ClassName='" & CType(e.Item.FindControl("Se_ClassName"), TextBox).Text _
                                   & "',MenuOrder=" & CType(e.Item.FindControl("Se_MenuOrder"), TextBox).Text & ",Link='" _
                               & CType(e.Item.FindControl("Se_Link"), TextBox).Text & "',UpClass=" & CType(e.Item.FindControl("seupclass"), DropDownList).SelectedValue _
                               & " where Id=" & CType(e.Item.FindControl("Se_Id"), Label).Text
            End Select
            Try
                db.sybaseopen()
                cmd = New Odbc.OdbcCommand(sqlstr, db.conn)
                cmd.ExecuteScalar()
                Select Case MenuClass.SelectedValue
                    Case "1"
                        firstclass.EditItemIndex = -1
                        bind()
                        Response.Write("<script>top.left.location.reload();</script>")
                    Case "2"
                        Sedatagrid.EditItemIndex = -1
                        bind()
                        Response.Write("<script>top.left.location.reload();</script>")
                    Case "3"
                        Sedatagrid.EditItemIndex = -1
                        bind()
                        Response.Write("<script>top.rtop.location.reload();</script>")
                End Select
            Catch ex As Exception
                Throw New System.Exception("更新数据出错!")
            End Try
        End Sub
        '退出编辑状态
        Sub canceldata(ByVal obj As Object, ByVal e As DataGridCommandEventArgs)
            Select Case MenuClass.SelectedValue
                Case "1"
                    firstclass.EditItemIndex = -1
                    bind()
                Case "2"
                    Sedatagrid.EditItemIndex = -1
                    bind()
                Case "3"
                    Sedatagrid.EditItemIndex = -1
                    bind()
            End Select    End Sub
        '删除数据
        Sub deletedata(ByVal obj As Object, ByVal e As DataGridCommandEventArgs)
            Dim sqlstr As String
            Select Case MenuClass.SelectedValue
                Case "1"
                    sqlstr = "delete SysMenuClass where Id=" & CType(e.Item.FindControl("Id"), Label).Text
                Case "2"
                    sqlstr = "delete SysFirstMenu where Id=" & CType(e.Item.FindControl("Se_Id"), Label).Text
                Case "3"
                    sqlstr = "delete SysSecondMenu where Id=" & CType(e.Item.FindControl("Se_Id"), Label).Text
            End Select
            Try
                db.sybaseopen()
                cmd = New Odbc.OdbcCommand(sqlstr, db.conn)
                cmd.ExecuteScalar()
                bind()
                Select Case MenuClass.SelectedValue
                    Case "1"
                        Response.Write("<script>top.left.location.reload();</script>")
                    Case "2"
                        Response.Write("<script>top.left.location.reload();</script>")
                    Case "3"
                        Response.Write("<script>top.rtop.location.reload();</script>")
                End Select
            Catch ex As Exception
                Throw New System.Exception("删除数据出错!")
            End Try
        End Sub
        '绑定数据
        Sub bind()
            db.sybaseopen()
            Select Case MenuClass.SelectedValue
                Case "1"
                    cmd = New Odbc.OdbcCommand("select * from SysMenuClass", db.conn)
                    dr = cmd.ExecuteReader
                    firstclass.DataSource = dr
                    firstclass.DataBind()
                    dr.Close()
                Case "2"
                    cmd = New Odbc.OdbcCommand("select a.Id,a.ClassName,a.MenuOrder,a.Link,a.UpClass as oldclass,b.ClassName as UpClass from SysFirstMenu a,SysMenuClass b where b.Id=a.UpClass", db.conn)
                    dr = cmd.ExecuteReader
                    Sedatagrid.DataSource = dr
                    Sedatagrid.DataBind()
                    dr.Close()
                Case "3"
                    cmd = New Odbc.OdbcCommand("select a.Id,a.ClassName,a.MenuOrder,a.Link,a.UpClass as oldclass,b.ClassName as UpClass from SysSecondMenu a,SysFirstMenu b where b.Id=a.UpClass", db.conn)
                    dr = cmd.ExecuteReader
                    Sedatagrid.DataSource = dr
                    Sedatagrid.DataBind()
                    dr.Close()
            End Select
            db.conn.Close()
        End Sub
      

  7.   

    学习一下如何使用DataSet就会明白了