如何使用带有gridview的C#编辑数据 [英] How to Edit the data using C# with gridview

查看:50
本文介绍了如何使用带有gridview的C#编辑数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我正在使用网格视图来显示数据,并希望更新或删除数据,并希望以网格视图显示,因为我已经做了一些代码:

我在更新按钮中编写代码请给我一些建议

 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.UI;
使用 System.Web.UI.WebControls;
使用 System.Data.Sql;
使用 System.Data.SqlClient;
使用 System.Data;

命名空间 asp2
{
public partial class formtest:System.Web.UI.Page
{
SqlConnection conn = < span class =code-keyword> new SqlConnection( @ data source = .\SQLEXPRESS; database = Registration; trusted_connection = yes);
SqlDataAdapter sd = new SqlDataAdapter();
DataSet ds = new DataSet();

受保护 void Page_Load(对象发​​件人,EventArgs e)
{

}

受保护 void Btnadd_Click( object sender,EventArgs e)
{
sd.InsertCommand = new SqlCommand( INSERT INTO formtest(firstname,username,pass,email_id )VALUES(@ firstname,@ username,@ pass,@ email_id),conn);
sd.InsertCommand.Parameters.AddWithValue( @ firstname,Tbfname.Text);
sd.InsertCommand.Parameters.AddWithValue( @ username,TbUname.Text);
sd.InsertCommand.Parameters.AddWithValue( @ pass,TbPass.Text);
sd.InsertCommand.Parameters.AddWithValue( @ email_id,Tbemail.Text);
conn.Open();
sd.InsertCommand.ExecuteNonQuery();
conn.Close();
}

受保护 void Btndisplay_Click( object sender,EventArgs e)
{
sd.UpdateCommand = new SqlCommand( update formtest set firstname = @ firstname,username = @ username,pass = @ pass,email_id = @ email_id where ID = @ID,康涅狄格州);
sd.UpdateCommand.Parameters.AddWithValue( @ firstname,Tbfname.Text);
sd.UpdateCommand.Parameters.AddWithValue( @ username,TbUname.Text);
sd.UpdateCommand.Parameters.AddWithValue( @ pass,TbPass.Text);
sd.UpdateCommand.Parameters.AddWithValue( @ email_id,Tbemail.Text);
sd.UpdateCommand.Parameters.AddWithValue( @ ID,); #STUCKED HERE


}
}
}

解决方案

尝试

http://www.dotnetspark.com/kb/643-how-to-editupdatedelete-gridview.aspx [ ^ ]

http://www.c-sharpcorner.com/UploadFile/mgold/EditableGridView11222005225133PM/EditableGridView.aspx [ ^ ]

全功能可编辑GridView控件 [ ^ ]


Abhinav已经为处理单行提供了一些很好的链接。如果您正在寻找批量更新,您还可以尝试以下方法:



演练:对绑定到GridView Web服务器控件的行执行批量更新

使用没有xxxDataSource的GridView进行批量编辑(SqlDataSource,ObjectDataSource等)

Real World GridView:批量编辑


  protected  < span class =code-keyword> void  grdPersonsDetails_RowCommand( object  sender,GridViewCommandEventArgs e)
{
cmd.Connection = con ;
int PersonId = int .Parse(Request.QueryString [ Id]。ToString());
if (e.CommandName == EditContactPerson
{

btnUpdate.Visible = true ;
btnSave.Visible = false ;

cmd.CommandType = CommandType.Text;
cmd.CommandText = SELECT ContactPersonId,Name,Address,Place,Phone,ImagePath FROM tbl_ContactPerson WHERE Con​​tactPersonId = @ContactPersonId;
cmd.Parameters.AddWithValue( @ ContactPersonId,PersonId);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
txtName.Text = dr [ 名称]。ToString();
txtAddress.Text = dr [ Address]。ToString();
txtPlace.Text = dr [ 放置]。ToString();
txtPhone.Text = dr [ Phone]。ToString();
hdfContactPersonId.Value = dr [ ContactPersonId]。ToString();
}
con.Close();
}
if (e.CommandName == 删除
{
cmd.CommandType = CommandType.Text;
cmd.CommandText = DELETE FROM tbl_ContactPerson WHERE Con​​tactPersonId = @ ContactPersonId;
cmd.Parameters.AddWithValue( @ ContactPersonId,PersonId);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
FillData();

}
}







更新和删除函数onRowCommand事件


Hello,

I am using grid view for displaying the data and want to update or delete the data and want to display in grid view as i had done some code:
I am writing the code in button for Update please give me some suggestion

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.Data;

namespace asp2
{
    public partial class formtest : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection(@"data source=.\SQLEXPRESS;database= Registration; trusted_connection=yes");
        SqlDataAdapter sd = new SqlDataAdapter();
        DataSet ds = new DataSet();
        
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Btnadd_Click(object sender, EventArgs e)
        {
            sd.InsertCommand = new SqlCommand("INSERT INTO formtest(firstname, username, pass, email_id) VALUES(@firstname, @username, @pass, @email_id)",conn);
            sd.InsertCommand.Parameters.AddWithValue("@firstname",Tbfname.Text);
            sd.InsertCommand.Parameters.AddWithValue("@username",TbUname.Text);
            sd.InsertCommand.Parameters.AddWithValue("@pass",TbPass.Text);
            sd.InsertCommand.Parameters.AddWithValue("@email_id", Tbemail.Text);
            conn.Open();
            sd.InsertCommand.ExecuteNonQuery();
            conn.Close();
        }

        protected void Btndisplay_Click(object sender, EventArgs e)
        {
            sd.UpdateCommand = new SqlCommand("update formtest set firstname=@firstname, username=@username, pass=@pass, email_id=@email_id where ID=@ID",conn);
            sd.UpdateCommand.Parameters.AddWithValue("@firstname", Tbfname.Text);
            sd.UpdateCommand.Parameters.AddWithValue("@username", TbUname.Text);
            sd.UpdateCommand.Parameters.AddWithValue("@pass", TbPass.Text);
            sd.UpdateCommand.Parameters.AddWithValue("@email_id", Tbemail.Text);
            sd.UpdateCommand.Parameters.AddWithValue("@ID", );#STUCKED HERE

            
        }
    }
}

解决方案

Try
http://www.dotnetspark.com/kb/643-how-to-editupdatedelete-gridview.aspx[^]
http://www.c-sharpcorner.com/UploadFile/mgold/EditableGridView11222005225133PM/EditableGridView.aspx[^]
Full-featured Editable GridView Control[^]


Abhinav already provided some good links for dealing with single rows. If you are looking for bulk updates, you can also try following:

Walkthrough: Performing Bulk Updates to Rows Bound to a GridView Web Server Control
Bulk Edit with GridView without xxxDataSource (SqlDataSource, ObjectDataSource, etc.)
Real World GridView: Bulk Editing


protected void grdPersonsDetails_RowCommand(object sender, GridViewCommandEventArgs e)
       {
           cmd.Connection = con;
           int PersonId = int.Parse(Request.QueryString["Id"].ToString());
           if (e.CommandName == "EditContactPerson")
           {

               btnUpdate.Visible = true;
               btnSave.Visible = false;

               cmd.CommandType = CommandType.Text;
               cmd.CommandText = "SELECT ContactPersonId,Name,Address,Place,Phone,ImagePath FROM tbl_ContactPerson WHERE ContactPersonId=@ContactPersonId";
               cmd.Parameters.AddWithValue("@ContactPersonId", PersonId);
               con.Open();
               SqlDataReader dr = cmd.ExecuteReader();
               while(dr.Read())
               {
                   txtName.Text = dr["Name"].ToString();
                   txtAddress.Text = dr["Address"].ToString();
                   txtPlace.Text = dr["Place"].ToString();
                   txtPhone.Text = dr["Phone"].ToString();
                   hdfContactPersonId.Value = dr["ContactPersonId"].ToString();
               }
               con.Close();
           }
           if (e.CommandName == "Delete")
           {
               cmd.CommandType = CommandType.Text;
               cmd.CommandText = "DELETE FROM tbl_ContactPerson WHERE ContactPersonId=@ContactPersonId";
               cmd.Parameters.AddWithValue("@ContactPersonId", PersonId);
               con.Open();
               cmd.ExecuteNonQuery();
               con.Close();
               FillData();

            }
       }




Update and Delete function onRowCommand event


这篇关于如何使用带有gridview的C#编辑数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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