我想在gridview中添加新行,请看我的代码,请帮助我 [英] i want to add new row in a gridview see my code kindly help me

查看:112
本文介绍了我想在gridview中添加新行,请看我的代码,请帮助我的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<%@ page language="C#" autoeventwireup="true" codebehind="WebForm1.aspx.cs" inherits="WebApplication25.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
	<title></title>
</head>
<body>
	<form id="form1" runat="server">
		<asp:gridview id="GridView1" runat="server" showfooter="true" autogeneratecolumns="false"

			datakeynames="PRODUCTID" cellpadding="3" gridlines="Both" onrowediting="gridOnRowEditing"

			onrowcancelingedit="gridOnRowCancelingEdit" onrowupdating="gridOnRowUpdating">
			<HeaderStyle BackColor="Cyan" ForeColor="White" BorderColor="Red" />
			<asp:CommandField ButtonType="Button" ShowCancelButton="true" ShowEditButton="true" HeaderText="Edit & Update" />
			<asp:BoundField DataField="PRODUCTID" HeaderText="PRODUCT ID" ReadOnly="true" />
			<asp:TemplateField HeaderText="PRODUCT NAME">
			<%# Eval("PRODUCTNAME") %>
			<asp:TextBox runat="server" ID="txtProduct" Text='<%# Eval("PRODUCTNAME") %>'>
			<asp:TemplateField HeaderText="UNIT PRICE">
			<%# Eval("UNITPRICE") %>
			<asp:TextBox runat="server" ReadOnly="true" ID="txtPrice" Text='<%# Eval("UNITPRICE") %>'>
			<asp:Button runat="server" ID="button1" Text="Add New Row" OnClick="button1_click"/>
	</form>
</body>
</html>



C#CODE


C# CODE

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

namespace WebApplication25
{
	public partial class WebForm1 : System.Web.UI.Page
	{
		SqlConnection con = new SqlConnection( "Data Source=localhost;Initial Catalog=practice;Integrated Security=true;" );

		protected void Page_Load ( object sender, EventArgs e )
		{
			if ( !Page.IsPostBack )
			{
				BindData( );
			}
		}

		protected void BindData ( )
		{
			SqlDataAdapter sda = new SqlDataAdapter( "Select * From PRODUCTDETAILS order by PRODUCTID asc", con );
			DataTable dt = new DataTable( );
			sda.Fill( dt );
			GridView1.DataSource = dt;
			GridView1.DataBind( );
			ViewState[ "CurrentTable" ] = dt;
		}

		protected void gridOnRowEditing ( object sender, GridViewEditEventArgs e )
		{
			GridView1.EditIndex = e.NewEditIndex;
			BindData( );
		}

		protected void gridOnRowCancelingEdit ( object sender, GridViewCancelEditEventArgs e )
		{
			GridView1.EditIndex = -1;
			BindData( );
		}

		protected void gridOnRowUpdating ( object sender, GridViewUpdateEventArgs e )
		{
			GridViewRow row = ( GridViewRow )GridView1.Rows[ e.RowIndex ];

			TextBox txtProductName = ( TextBox )row.FindControl( "txtProduct" );
			TextBox UNITPRICE = ( TextBox )row.FindControl( "txtPrice" );

			string ProductName = txtProductName.Text;
			string txtUnitPrice = UNITPRICE.Text;
			int ProductID = Convert.ToInt32( GridView1.DataKeys[ e.RowIndex ].Value );
			UpdateRecord( ProductID, ProductName, txtUnitPrice );
		}

		protected void UpdateRecord ( int ProductID, string ProductName, string UnitPrice )
		{
			try
			{
				SqlCommand cmd = new SqlCommand( "Update PRODUCTDETAILS set ProductName=@ProductName,UnitPrice=@UnitPrice WHERE ProductID=@ProductID", con );

				cmd.Parameters.Add( new SqlParameter( "@ProductID", ProductID ) );
				cmd.Parameters.Add( new SqlParameter( "@ProductName", ProductName ) );
				cmd.Parameters.Add( new SqlParameter( "@UnitPrice", UnitPrice ) );
				con.Open( );
				cmd.ExecuteNonQuery( );
				con.Close( );

				GridView1.EditIndex = -1;
				BindData( );
			}
			catch ( Exception ex )
			{
				throw ex;
			}
		}

		protected void AddNewRow ( )
		{

		}

		protected void button1_click ( object sender, EventArgs e )
		{
			AddNewRow( );
		}
	}
}

推荐答案

http://www.aspsnippets.com/Articles/Add-new-Row-to-GridView -on-Button-Click-in-ASPNet.aspx [ ^ ]


查看此:



从ASP.NET动态添加和删除行GridView [ ^ ]


在Addnewrow中尝试此代码()



{

SqlConnection con = new SqlConnection(Data Source = localhost; Initial Catalog = practice; Integrated Secu rity = true;);

数据集ds =新数据集();

SqlCommand scom = new SqlCommand(insert query,con);

con.open();

scom.ExecuteNonQuery();

con.Close();

}



在sqlcommand fire insert查询中。
Try this code in Addnewrow ()

{
SqlConnection con = new SqlConnection( "Data Source=localhost;Initial Catalog=practice;Integrated Security=true;" );
Dataset ds=new dataset ();
SqlCommand scom=new SqlCommand ("insert query", con);
con.open ();
scom.ExecuteNonQuery ();
con.Close();
}

In sqlcommand fire insert query.


这篇关于我想在gridview中添加新行,请看我的代码,请帮助我的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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