动态Gridview行插入 [英] dynamic Gridview row insert

查看:97
本文介绍了动态Gridview行插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在gridview.as中插入数据,当我插入它时,应该在gridview中对其进行更新,我的意思是说应该在gridview的末尾添加行. ,我该怎么办,代码在下面给出...

aspx页面

i want to insert data in gridview.as n when i insert it,it should be updated in gridview i mean to say row should be added at the end of the gridview.prblem is that data is getting inserted but not at last,what should i do,the code is given below...

aspx page

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <div>
     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
     AllowPaging="True" PageSize="5" 
     onpageindexchanging="GridView1_PageIndexChanging" >
        <columns>
             <asp:CommandField ShowSelectButton="True" ShowEditButton="True" />
             <asp:BoundField DataField="Id" HeaderText="Id" />
             <asp:BoundField DataField="Name" HeaderText="Name" />
             <asp:BoundField DataField="Phone_No" HeaderText="Phone_No" />
        </columns>
     
        <table>
            <tr>
                <td>
                    <asp:Label ID="Label5" runat="server" Text="Id">
                </td>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server">
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="Label1" runat="server" Text="Name">
                </td>
                <td>
                    <asp:TextBox ID="TextBox2" runat="server">
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="Label2" runat="server" Text="Phone No">
                </td>
                <td>
                    <asp:TextBox ID="TextBox3" runat="server" >
                </td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Add" />
                </td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                   </td>
            </tr>
        </table>
    </div>
    
    </div>
    </form>
</body>
</html>


代码文件


code file

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections;
using System.Data.SqlClient;

public partial class Default2 : System.Web.UI.Page
    {
        DataSet ds;
        SqlConnection conn = new SqlConnection();
        SqlCommand cmd = new SqlCommand();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                fillgrid();


            }
        }

        public void fillgrid()
        {
            conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["conname1"].ConnectionString;
            conn.Open();
            SqlCommand cmd = new SqlCommand("select * from Crud", conn);

            SqlDataAdapter da = new SqlDataAdapter(cmd);


            ds = new DataSet();


            da.Fill(ds, "Crud");

            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();

            conn.Close();
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["conname1"].ConnectionString;

            conn.Open();

            cmd.Connection = conn;

            cmd.CommandText = "INSERT INTO Crud (Id,Name,Phone_No) VALUES(''" + TextBox1.Text + "'',''" + TextBox2.Text + "'',''" + TextBox3.Text + "'')";

            cmd.ExecuteNonQuery();
            conn.Close();

            fillgrid();
            //DataTable dtable = new DataTable();
            //dtable.Columns.Add(new DataColumn("Id"));
            //dtable.Columns.Add(new DataColumn("Name"));
            //dtable.Columns.Add(new DataColumn("Phone_No"));
            //object[] RowValues = { "", "", "" };
            //RowValues[0] = TextBox1.Text;  //chaged here....
            //RowValues[1] = TextBox2.Text;
            //RowValues[2] = TextBox3.Text;
            //DataRow dRow;
            //dRow = dtable.Rows.Add(RowValues);
            //dtable.AcceptChanges();
            //GridView1.DataSource = dtable;//added here....
            //GridView1.DataBind();

        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            fillgrid();
        }
    }

推荐答案

^ ]


在您的aspx页面中,我没有找到关闭gridview标记首先添加它.
如果我没看错,则在您的代码中首先在第一次回发时在页面加载时绑定gridview.此后,在add按钮上单击,首先将数据更新到数据库,然后再次获取数据,然后绑定到gridview.您正在获取数据,我认为数据集中的数据是按特定顺序排列的,或者在将数据填充到Gridview中时,它以特定的顺序插入,这可能是默认顺序.您还可以明智地使用按日期顺序将数据插入表中的列,例如datetime列或具有自动增量数据的列n.希望对您有所帮助.
In your aspx page i am didn''t found closing gridview tag add it first.
If i am not wrong ,in your code first you are binding gridview at page load at first post back.After this, on add button click there is firstly data updated to the database and again data has fetched and then bind to the gridview.When you are fetching the data i think data in the dataset is in particular order or while filling data to the gridview it insert in particular order which may be default order.To resolve it use order by clause with ascending direction on identity column in your table if you have other wise use the column which gives data in the order in which you are inserting it in the table like datetime column or column with autoincrement data n all.i hope this will helps you.


尝试跟随URL

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

谢谢
try following URL

Dynamically Adding and Deleting rows from ASP.NET Gridview [^]

Thanks


这篇关于动态Gridview行插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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