如何在C#中更新和添加gridview中的新行。 [英] How to update and add new line in gridview in C# .

查看:527
本文介绍了如何在C#中更新和添加gridview中的新行。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想法是:编辑,更新,在gridview中添加新行。



我已经编写了编辑按钮的命令,但在更新查询中出错了。

以及如何在gridview中添加新的下一行。



以下我已经提到了我到目前为止所尝试的内容。我收到执行错误

protected void GridView2_RowUpdating



我尝试了什么:



到目前为止的计划:



Idea is to : Edit,Update, add new row in gridview.

I have written the command for edit button but went wrong in update query.
And also how to add new next line in gridview.

below I have mentioned what I have tried so far. I'm getting execution error in
protected void GridView2_RowUpdating

What I have tried:

Program so far:

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;
using System.Drawing;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            addins();
        }
    }

    protected void addins()
    {
        SqlConnection cn = new SqlConnection("Server=WINDOWS-PC\\SQLEXPRESS;database=payroll;Trusted_Connection=Yes;");
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "select * from amount";
        cmd.Connection = cn;
        cn.Open();


        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        int count = ds.Tables[0].Rows.Count;
        cn.Close();

        if (ds.Tables[0].Rows.Count > 0)
        {
            GridView2.DataSource = ds;
            GridView2.DataBind();
        }
        else
        {
            ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
            GridView2.DataSource = ds;
            GridView2.DataBind();
            int cc = GridView2.Rows[0].Cells.Count;

            Label1.Text = "No Record";
        }
    }

    protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView2.EditIndex = e.NewEditIndex;
        addins();
    }

    protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int sno = Convert.ToInt32(GridView2.DataKeys[e.RowIndex].Value.ToString());
        GridViewRow row = (GridViewRow)GridView2.Rows[e.RowIndex];
       // TextBox sno = (TextBox)GridView2.Rows[e.RowIndex].FindControl("sno");
        TextBox particulars = (TextBox)GridView2.Rows[e.RowIndex].FindControl("part");
        TextBox quantity = (TextBox)GridView2.Rows[e.RowIndex].FindControl("qty");
        TextBox rate = (TextBox)GridView2.Rows[e.RowIndex].FindControl("rate");
        TextBox amount = (TextBox)GridView2.Rows[e.RowIndex].FindControl("amt");
        GridView2.EditIndex = -1;

        SqlConnection cn = new SqlConnection("Server=WINDOWS-PC\\SQLEXPRESS;database=payroll;Trusted_Connection=Yes;");
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "update amount set part='" + particulars.Text + "', qty = '" + quantity.Text + "', rate = '" + rate.Text + "', amt = '" + amount.Text + "' where  sno ='" + sno + "',cn)";
        cmd.Connection = cn;
        cn.Open();
        
        cmd.ExecuteNonQuery();
        cn.Close();
        addins();
    }

    protected void GridView2_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView2.EditIndex = -1;
            addins();
    }

推荐答案

请按照以下链接.....



简单插入选择编辑在ASP.Net GridView控件中更新和删除 [ ^ ]

http://www.aspdotnet-suresh.com/2011/02/how-to-inserteditupdate-and-delete-data.html [ ^ ]

在GridView中插入更新编辑删除记录在ASP.Net中唱出SqlDataSource [ ^ ]





问候,

AARIF SHAIKH
Please follow below links.....

Simple Insert Select Edit Update and Delete in ASP.Net GridView control[^]
http://www.aspdotnet-suresh.com/2011/02/how-to-inserteditupdate-and-delete-data.html[^]
Insert Update Edit Delete record in GridView using SqlDataSource in ASP.Net[^]


Regards,
AARIF SHAIKH


这篇关于如何在C#中更新和添加gridview中的新行。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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