如何编辑,更新和删除行的网格视图在ASP.NET [英] How to edit, update and Delete Rows in Grid view in ASP.NET

查看:219
本文介绍了如何编辑,更新和删除行的网格视图在ASP.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

荫检索数据库中的数据进入的GridView。 请帮助我如何编辑和网格视图删除行,它也将更新数据库。 也请告诉我,如果他们是有错在我的code`

 在此输入code


 <头=服务器>
<冠军>< /标题>
<风格类型=文本/ CSS>
    .style1
    {
        宽度:248px;
    }
    .style2
    {
        宽度:100%;
    }
    .style3
    {
        高度:180px;
    }
< /风格>
 < /头>
 <身体GT;
<表格ID =Form1的=服务器>
< D​​IV CLASS =节日礼物>
< H1 ALIGN =中心>学生个人信息
< / H1>
    <表类=蓝紫魅力>
        &其中; TR>
            < TD类=样式1>
                &功放; NBSP;
            < ASP:标签ID =标签1=服务器文本=标签>< / ASP:标签>
            < / TD>
            < TD>
                &功放; NBSP;
                &功放; NBSP;
            < ASP:文本框ID =TextBox1的=服务器>< / ASP:文本框>
            < / TD>
        < / TR>
        &其中; TR>
            < TD类=样式1>
                &功放; NBSP;
            < ASP:标签ID =Label2的=服务器文本=标签>< / ASP:标签>
            < / TD>
            < TD>
                &功放; NBSP;
                &功放; NBSP;
            < ASP:文本框ID =TextBox2中=服务器>< / ASP:文本框>
            < / TD>
        < / TR>
        &其中; TR>
            < TD类=样式1>
                &功放; NBSP;
            < ASP:标签ID =LABEL3=服务器文本=标签>< / ASP:标签>
            < / TD>
            < TD>
                &功放; NBSP;
                &功放; NBSP;
            < ASP:文本框ID =TextBox3=服务器>< / ASP:文本框>
            < / TD>
        < / TR>
        &其中; TR>
            < TD类=样式1>
    < ASP:按钮的ID =Button1的=服务器文本=插入数据
        的onclick =的button1_Click/>
            < / TD>
            < TD>
                &功放; NBSP;&安培; NBSP;&安培; NBSP;&安培; NBSP;
    < ASP:按钮的ID =Button2的=服务器的onclick =Button2_Click
        文本=显示所有学生WIDTH =128像素/>
            < / TD>
        < / TR>
    < /表>

    &功放; NBSP;< BR />
    < BR />
      &功放; NBSP;&安培; NBSP;&安培; NBSP;&安培; NBSP;&安培; NBSP;&安培; NBSP;&安培; NBSP; &功放; NBSP;
    < BR />
    < BR />
    &功放; NBSP;&安培; NBSP;&安培; NBSP;&安培; NBSP;&安培; NBSP;&安培; NBSP;&安培; NBSP;&安培; NBSP;&安培; NBSP;
    < BR />
     < BR />
     &功放; NBSP;&安培; NBSP;&安培; NBSP;&安培; NBSP;&安培; NBSP;&安培; NBSP;&安培; NBSP; &功放; NBSP;
    < BR />
    < BR />

    < BR />
    < BR />
< / DIV>
< BR />
< ASP:标签ID =Label4=服务器>< / ASP:标签>
< BR />
< ASP:GridView控件ID =GridView1=服务器
    onrowcancelingedit =GridView1_RowCancelingEdit
    onrowediting =GridView1_RowEditingonrowupdating =GridView1_RowUpdating>
    <列>
        < ASP:CommandField中按钮类型=按钮ShowEditButton =真/>
    < /列>
< / ASP:GridView控件>
< /形式GT;
 < /身体GT;
   < / HTML>

在这里输入code
 

和我的后端code是

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用的System.Web;
使用System.Web.UI程序;
使用System.Web.UI.WebControls;
使用System.Data.Sql;
使用System.Data.SqlClient的;
使用System.Configuration;
使用System.Data这;

  公共部分类_Default:System.Web.UI.Page
{
 SqlConnection的康恩=新的SqlConnection(数据源= DATA_NET_81_SOF;初始
 目录=学生;集成安全性=真);
的SqlCommand CMD =新的SqlCommand();
保护无效的Page_Load(对象发件人,EventArgs的)
{
    如果(!Page.IsPostBack)
    {
        Label1.Text =学生姓名;
        Label2.Text =学生级;
        Label3.Text =学生的卷号;
    }

}
保护无效的button1_Click(对象发件人,EventArgs的)
{
    尝试
    {
        的SqlCommand CMD =新的SqlCommand(INSERT INTO Personalinfo(StudentName,StudentClass,StudentRollNo)VALUES('+ TextBox1.Text +,+ TextBox2.Text +,+ TextBox3.Text +') ,康涅狄格州);
        conn.Open();
        cmd.Parameters.AddWithValue(StudentName,TextBox1.Text);

        cmd.Parameters.AddWithValue(StudentClass,TextBox2.Text);

        cmd.Parameters.AddWithValue(StudentRollno,TextBox3.Text);

        cmd.ExecuteNonQuery();
        Label4.Text =数据的存储;
    }
    赶上(例外前)
    {
        Label4.Text = ex.Message;
    }

}


保护无效Button2_Click(对象发件人,EventArgs的)
{
    SqlCommand的SQL =新的SqlCommand(选择Personalinfo *,康涅狄格州);
    SqlDataAdapter的DA =新的SqlDataAdapter(SQL);
    的DataSet ds为新的DataSet();
    da.Fill(DS);
    GridView1.DataSource =(DS);
    GridView1.DataBind();


}

保护无效GridView1_RowEditing(对象发件人,GridViewEditEventArgs E)
{
    GridView1.EditIndex = e.NewEditIndex;
    GridView1.DataBind();
}
保护无效GridView1_RowCancelingEdit(对象发件人,GridViewCancelEditEventArgs E)
{
    e.Cancel =真;
    GridView1.EditIndex = -1;

}
 

}

解决方案

 私人字符串连接= @...;

  保护无效的button1_Click(对象发件人,EventArgs的)
        {
         使用(SqlConnection的CON =新的SqlConnection(连接))
    {
            尝试
            {
                的SqlCommand CMD =新的SqlCommand(INSERT INTO Personalinfo(StudentName,StudentClass,StudentRollNo)VALUES('+ TextBox1.Text +,+ TextBox2.Text +,+ TextBox3.Text +') ,CON);
                con.Open();

                cmd.ExecuteNonQuery();
                Label4.Text =数据的存储;
            }
            赶上(例外前)
            {
                Label4.Text = ex.Message;
            }
        }
        }
 

更新 - >

 保护无效Button_Update(对象发件人,EventArgs的){
 使用(SqlConnection的CON =新的SqlConnection(康涅狄格州))
{
  使用(CMD的SqlCommand =新的SqlCommand())
{
  cmd.Connection = CON;
  cmd.CommandText =UPDATE Personalinfo SET StudentName = @ 1 ... WHERE STUDENT_ID = @N;
  cmd.Parameters.Add(@ 1,SqlDbType.NVarChar)。价值= your_value;
  cmd.Para .....
  cmd.Parameters.Add(@ N,......)值= your_student_id。
  con.Open();
  cmd.ExecuteNonQuery();
  con.Close();
}
}
}
 

有关删除 - >>

 保护无效Button_Delete(对象发件人,EventArgs的){
 使用(SqlConnection的CON =新的SqlConnection(康涅狄格州))
{
  使用(CMD的SqlCommand =新的SqlCommand())
{
  cmd.Connection = CON;
  cmd.CommandText =DELETE FROM Personalinfo WHERE StudentName ='+ TextBox1.Text +';
  con.Open();
  cmd.ExecuteNonQuery();
  con.Close();
}
}
}
 

在按钮的每一个事件,你可以做一个BindGrid ...刷新从数据网格的数据......在BindGrid方法,你需要重拍,你只是做了选择方法......如果你有问题告诉我

Iam retrieving Data From Database Into Gridview. Please help me how to edit and Delete row in Grid view and it Also Update in Database. Also please Tell me if their is any mistake in my Code`

enter code here


 <head runat="server">
<title></title>
<style type="text/css">
    .style1
    {
        width: 248px;
    }
    .style2
    {
        width: 100%;
    }
    .style3
    {
        height: 180px;
    }
</style>
 </head>
 <body>
<form id="form1" runat="server">
<div class="style3">
<h1 align="center">Students Personal Information
</h1>
    <table class="style2">
        <tr>
            <td class="style1">
                &nbsp;
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
            </td>
            <td>
                &nbsp;
                &nbsp;
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style1">
                &nbsp;
            <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
            </td>
            <td>
                &nbsp;
                &nbsp;
            <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style1">
                &nbsp;
            <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
            </td>
            <td>
                &nbsp;
                &nbsp;
            <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style1">
    <asp:Button ID="Button1" runat="server" Text="Insert Data" 
        onclick="Button1_Click" />
            </td>
            <td>
                &nbsp;&nbsp;&nbsp;&nbsp;
    <asp:Button ID="Button2" runat="server" onclick="Button2_Click" 
        Text="Show All Students" Width="128px" />
            </td>
        </tr>
    </table>

    &nbsp;<br />
    <br />
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;
    <br />
    <br />
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <br />
     <br />
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;
    <br />
    <br />

    <br />
    <br />
</div>
<br />
<asp:Label ID="Label4" runat="server"></asp:Label>
<br />
<asp:GridView ID="GridView1" runat="server" 
    onrowcancelingedit="GridView1_RowCancelingEdit" 
    onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating">
    <Columns>
        <asp:CommandField ButtonType="Button" ShowEditButton="True" />
    </Columns>
</asp:GridView>
</form>
 </body>
   </html>

enter code here

And my back-end code is

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

  public partial class _Default : System.Web.UI.Page
{
 SqlConnection conn = new SqlConnection("Data Source=DATA_NET_81_SOF;Initial                
 Catalog=Students;Integrated Security=True");     
SqlCommand cmd = new SqlCommand();
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        Label1.Text = "Student's Name";
        Label2.Text = "Student's Class";
        Label3.Text = "Student's Roll Number";
    }

}
protected void Button1_Click(object sender, EventArgs e)
{
    try
    {
        SqlCommand cmd = new SqlCommand("Insert INTO Personalinfo(StudentName,StudentClass,StudentRollNo)values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "')", conn);
        conn.Open();
        cmd.Parameters.AddWithValue("StudentName", TextBox1.Text);

        cmd.Parameters.AddWithValue("StudentClass", TextBox2.Text);

        cmd.Parameters.AddWithValue("StudentRollno", TextBox3.Text);

        cmd.ExecuteNonQuery();
        Label4.Text = "Data Is Stored";
    }
    catch (Exception ex)
    {
        Label4.Text = ex.Message;
    }

}


protected void Button2_Click(object sender, EventArgs e)
{
    SqlCommand sql = new SqlCommand("Select * from Personalinfo", conn);
    SqlDataAdapter da = new SqlDataAdapter(sql);
    DataSet ds = new DataSet();
    da.Fill(ds);
    GridView1.DataSource = (ds);
    GridView1.DataBind();


}

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
    GridView1.EditIndex = e.NewEditIndex;
    GridView1.DataBind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
    e.Cancel = true;
    GridView1.EditIndex = -1;

}

}

解决方案

private string connection = @"...";

  protected void Button1_Click(object sender, EventArgs e)
        {
         using(SqlConnection con = new SqlConnection(connection))
    {
            try
            {
                SqlCommand cmd = new SqlCommand("Insert INTO Personalinfo(StudentName,StudentClass,StudentRollNo)values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "')", con);
                con.Open();

                cmd.ExecuteNonQuery();
                Label4.Text = "Data Is Stored";
            }
            catch (Exception ex)
            {
                Label4.Text = ex.Message;
            }
        }
        }

For Update -->

protected void Button_Update(object sender, EventArgs e){
 using(SqlConnection con = new SqlConnection(conn))
{
  using(SqlCommand cmd = new SqlCommand())
{
  cmd.Connection = con;
  cmd.CommandText = "UPDATE Personalinfo SET StudentName = @1 ... WHERE Student_Id= @N";
  cmd.Parameters.Add("@1",SqlDbType.NVarChar).Value = your_value;
  cmd.Para.....
  cmd.Parameters.Add("@N",.....).Value = your_student_id;
  con.Open();
  cmd.ExecuteNonQuery();
  con.Close();
}
}
}

For Delete -->>

protected void Button_Delete(object sender, EventArgs e){
 using(SqlConnection con = new SqlConnection(conn))
{
  using(SqlCommand cmd = new SqlCommand())
{
  cmd.Connection = con;
  cmd.CommandText = "DELETE FROM Personalinfo WHERE StudentName = '"+TextBox1.Text+"'";
  con.Open();
  cmd.ExecuteNonQuery();
  con.Close();
}
}
}

After every event of button you can make a BindGrid ... to refresh your data from data grid... in BindGrid method you need to remake the select method you just did ... if you have issue tell me

这篇关于如何编辑,更新和删除行的网格视图在ASP.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆