帮助编写和删除gridview中的代码 [英] Help in write edit and delete code in gridview

查看:70
本文介绍了帮助编写和删除gridview中的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,



我在gridview代码中编写了编辑和删除,但它不起作用,请帮助修改。



谢谢





Dear All,

I had write the edit and delete in gridview code, but it doesnt work, kindly help to amend.

thank you


<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="acmkweb.WM.WebForm2" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>



<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">



<asp:TextBox ID="TextBox1" runat="server"/>
<asp:TextBox ID="TextBox2" runat="server"/>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:GridView ID="GridView1" runat="server" AutoGenerateEditButton="True" AutoGenerateDeleteButton="True"  ></asp:GridView>


<asp:Button ID="Button2" runat="server" Text="Button" OnClick="Button2_Click"/>


 <asp:TableRow ID="TableRow1" runat="server" ForeColor="Teal">
            <asp:TableHeaderCell Font-Bold="true" ForeColor="#0C0C25" ColumnSpan="4">
                <asp:label id="StatLabel" runat="server">
                </asp:label>
                </asp:TableHeaderCell>
              </asp:TableRow>

</asp:Content>







代码背后








Code Behind


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.Collections;
using System.Configuration;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Text;
using System.IO;


namespace acmkweb.WM
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {

                FillContainerType();
            }



        }







        private void BindGrid(int rowcount)
        {

            DataTable dt = new DataTable();

            DataRow dr;

            dt.Columns.Add(new System.Data.DataColumn("Test1", typeof(String)));

            dt.Columns.Add(new System.Data.DataColumn("Test2", typeof(String)));

            dt.Columns.Add(new System.Data.DataColumn("Test3", typeof(String)));



            if (ViewState["CurrentData"] != null)
            {

                for (int i = 0; i < rowcount + 1; i++)
                {

                    dt = (DataTable)ViewState["CurrentData"];

                    if (dt.Rows.Count > 0)
                    {

                        dr = dt.NewRow();

                        dr[0] = dt.Rows[0][0].ToString();



                    }

                }

                dr = dt.NewRow();

                dr[0] = TextBox1.Text;

                dr[1] = TextBox2.Text;

                dr[2] = Drop1.SelectedValue;

                dt.Rows.Add(dr);



            }

      else
           {

              dr = dt.NewRow();

               dr[0] = TextBox1.Text;

               dr[1] = TextBox2.Text;

               dr[2] = Drop1.SelectedValue;



               dt.Rows.Add(dr);



           }



            // If ViewState has a data then use the value as the DataSource

            if (ViewState["CurrentData"] != null)
            {

                GridView1.DataSource = (DataTable)ViewState["CurrentData"];

                GridView1.DataBind();

                CommandField cf = new CommandField { AutoGenerateDeleteButton = true, AutoGenerateDeleteButton = true };
                GridView1.Columns.Add(cf);


            }

            else
            {



                // Bind GridView with the initial data assocaited in the DataTable

                GridView1.DataSource = dt;

                GridView1.DataBind();



            }

            // Store the DataTable in ViewState to retain the values

            ViewState["CurrentData"] = dt;



        }




        protected void Button1_Click(object sender, EventArgs e)
        {
            // Check if the ViewState has a data assoiciated within it. If
            if (ViewState["CurrentData"] != null)
            {
                DataTable dt = (DataTable)ViewState["CurrentData"];
                int count = dt.Rows.Count;
                BindGrid(count);
            }
            else
            {
                BindGrid(1);
            }
            TextBox1.Text = string.Empty;
            TextBox2.Text = string.Empty;

            TextBox1.Focus();
            TextBox2.Focus();

        }



     protected void Button2_Click(object sender, EventArgs e)
        {


       foreach (GridViewRow oItem in GridView1.Rows)
         {
          string str1 = oItem.Cells[0].Text;
          string str2 = oItem.Cells[1].Text;
          string str3 = oItem.Cells[2].Text;
          insertData(str1, str2, str3);
    }

    Response.Redirect("WebForm2.aspx?stat=insert");
}


     public void insertData(string str1,string str2,string str3)

     {
                SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["CIMProRPT01ConnectionString"].ConnectionString);
                 string sql = "insert into test (test1,test2,test3) values ('" + str1 + "','" + str2 + "','" + str3 + "')";



                con.Open();

                SqlCommand cmd = new SqlCommand(sql, con);


                cmd.ExecuteNonQuery();

                con.Close();


            }

        }

    }

推荐答案

请参阅以下链接获取类似文章:

Asp.net在gridview中插入,编辑,更新,删除数据 [ ^ ]

插入更新编辑删除GridView中的行记录 [ ^ ]

Gridview编辑ASP.NET中的删除和更新 [ ^ ]

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

插入,更新,使用Gridview删除....简单方法 [ ^ ]

更新,删除记录使用GridView控件 [ ^ ]

网格行编辑,删除和更新 [ ^ ]



MSDN:

在GridView控件中使用TemplateFields [ ^ ]

ASP.NET的GridView示例:使用TemplateFields [ ^ ]



更多关于 CodeProject [ ^ ]





- Amit
Refer the links below for similar articles:
Asp.net insert, Edit, update, delete data in gridview[^]
Insert Update Edit Delete Rows Record In GridView[^]
Gridview Edit Delete and Update in ASP.NET[^]
Simple Insert Select Edit Update and Delete in ASP.Net GridView control[^]
Insert, Update, Delete with Gridview....simple way[^]
Updating, Deleting records using GridView control[^]
GridView Row Edit, Delete and Update[^]

MSDN:
Using TemplateFields in the GridView Control[^]
GridView Examples for ASP.NET: Working with TemplateFields[^]

And more on CodeProject[^]


--Amit


嗨...

一旦尝试此链接。

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

Thnak u。
Hi...
Once try this link.
http://www.aspdotnet-suresh.com/2011/02/how-to-inserteditupdate-and-delete-data.html[^]
Thnak u.


亲爱的所有,



我从谷歌搜索得到的所有示例代码都是与DB的链接,但根据我的编码是保存数据处于查看状态,我可以知道如何删除处于查看状态的数据?



任何一个例子?
Dear all,

im searching from Google get the all example code is link with DB, but based on my coding is save the data in view state, May i know how to delete the data in view state ?

any example ?


这篇关于帮助编写和删除gridview中的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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