通过网格视图插入数据 [英] insert data through grid view

查看:65
本文介绍了通过网格视图插入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 我想将网格视图用作输入表单....

我想在页面加载时

它显示

名称的文本框::年龄的文本框::保存按钮
名称的文本框::年龄的文本框::保存按钮
名称的文本框::年龄的文本框::保存按钮
名称的文本框::年龄的文本框::保存按钮



....................................

我该怎么做......

我不想将4个文本框用作名称,将4个文本框用作年龄和4个保存按钮...

我只想拿一个..
它应该重复...



安德,我想给
最后添加更多按钮...如果我单击那一行,则应添加.....


我该怎么办......


请建议我..............

解决方案

为gridview组件的页脚设置模板字段.
参见此处 http: //www.asp.net/data-access/tutorials/insertting-a-new-record-from-the-gridview-amp-rsquo-s-footer-vb [ < asp:GridView ID =" runat =" 服务器" 宽度 550px" AutoGenerateColumns =" AlternatingRowStyle-BackColor =" HeaderStyle-BackColor =" ShowFooter =" > < > < asp:TemplateField HeaderText =" < ItemTemplate > <%#Eval(" )%> < /ItemTemplate > < FooterTemplate > < asp:TextBox ID =" runat 服务器" > < /FooterTemplate > < /asp:TemplateField > < asp:TemplateField HeaderText =" < ItemTemplate > <%#Eval(" )%> < /ItemTemplate > < FooterTemplate > < asp:TextBox ID =" runat 服务器" / < /FooterTemplate > < /asp:TemplateField > < asp:TemplateField HeaderText =" < ItemTemplate > <%#Eval(" )%> < /ItemTemplate > < FooterTemplate > < asp:TextBox ID =" runat 服务器" < > < /FooterTemplate > < /asp:TemplateField > < asp:TemplateField > < ItemTemplate > < /ItemTemplate > < FooterTemplate > < asp:Button ID =" runat 服务器" 文本 添加" OnClick 添加" CommandName =" Footer" Footer" span> / &g t; < /FooterTemplate > < /asp:TemplateField > < /列 > < AlternatingRowStyle =" #C2D69B" / < EmptyDataTemplate > < tr =" > < th =" col" < /th > < th =" col" < /th > < th =" col" < /th > < th =" col" < /th > < /tr > < tr > < td > < asp:TextBox ID =" runat 服务器" onkeypress document.getElementById('GridView1').innerHTML = document.getElementById('txtCustomerName').value" / > < /td > < td > < asp:TextBox ID =" runat 服务器" > < /td > < td > < asp:TextBox ID =" runat 服务器" > < /td > < td > < asp:Button ID =" runat 服务器" 文本 添加" OnClick 添加" CommandName =" / < /td > < /tr > < /EmptyDataTemplate > < /asp:GridView >




背后的代码..

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

namespace TestPage
{
    public partial class Gridwithkey : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.BindData();
            }
        }
        private void BindData()
        {
            string constr = ConfigurationManager.ConnectionStrings["SomeDataBase"].ToString();
            //con = new SqlConnection(constr);
            DataTable dt = new DataTable();
            using (SqlConnection con = new SqlConnection(constr))
            {
                string strQuery = "SELECT * FROM Ragi";
                SqlCommand cmd = new SqlCommand(strQuery);
                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    cmd.Connection = con;
                    con.Open();
                    sda.SelectCommand = cmd;
                    sda.Fill(dt);
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                }
            }
        }

        protected void Add(object sender, EventArgs e)
        {
            Control control = null;
            if (GridView1.FooterRow != null)
            {
                control = GridView1.FooterRow;
            }
            else
            {
                control = GridView1.Controls[0].Controls[0];
            }
            string customerName = (control.FindControl("txtCustomerName") as TextBox).Text;
            string companyName = (control.FindControl("txtCompanyName") as TextBox).Text;
            string city = (control.FindControl("txtCity") as TextBox).Text;
            //string strConnString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
            string constr = ConfigurationManager.ConnectionStrings["SomeDataBase"].ToString();
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.Connection = con;
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = "INSERT INTO [Ragi] VALUES(@CustomerName, @CompanyName, @City)";
                    cmd.Parameters.AddWithValue("@CustomerName", customerName);
                    cmd.Parameters.AddWithValue("@CompanyName", companyName);
                    cmd.Parameters.AddWithValue("@City", city);
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
            }
            Response.Redirect(Request.Url.AbsoluteUri);
        }
       
        
    }
    }


hi i want to use my grid view as entry form....

i want when my page load

it show

textbox for name :: textbox for age :: save button
textbox for name :: textbox for age :: save button
textbox for name :: textbox for age :: save button
textbox for name :: textbox for age :: save button



....................................

How i can do this....

I not want to take 4 textbox for name and 4 textbox for age and 4 save button...

I only want to take 1 ..
and it should repeat...



Andd i want to give
add more button at last... if i click on that one more row should added.....


How can i do this....


Plse suggest me........

set templatefields for the footerrow of a gridview component.
see here
http://www.asp.net/data-access/tutorials/inserting-a-new-record-from-the-gridview-amp-rsquo-s-footer-vb[^] for more details


You try this sample..

Aspx Page...

<asp:GridView ID="GridView1" runat="server" Width="550px" AutoGenerateColumns="false"

    AlternatingRowStyle-BackColor="#C2D69B" HeaderStyle-BackColor="green" ShowFooter="true">
    <Columns>
        <asp:TemplateField HeaderText="Customer Name">
            <ItemTemplate>
                <%# Eval("CustomerName")%>
            </ItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="txtCustomerName" runat="server"/>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Company Name">
            <ItemTemplate>
                <%# Eval("CompanyName")%>
            </ItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="txtCompanyName" runat="server" />
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="City">
            <ItemTemplate>
                <%# Eval("City")%>
            </ItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="txtCity" runat="server"></asp:TextBox>
            </FooterTemplate>
        </asp:TemplateField>
            <asp:TemplateField>
            <ItemTemplate>
            </ItemTemplate>
            <FooterTemplate>
                <asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="Add" CommandName = "Footer" />
            </FooterTemplate>
        </asp:TemplateField>
    </Columns>
    <AlternatingRowStyle BackColor="#C2D69B" />
    <EmptyDataTemplate>
        <tr style="background-color: Green;">
            <th scope="col">
                Customer Name
            </th>
            <th scope="col">
                Company Name
            </th>
            <th scope="col">
                City
            </th>
            <th scope="col">

            </th>
        </tr>
        <tr>
            <td>
                <asp:TextBox ID="txtCustomerName" runat="server" onkeypress="document.getElementById('GridView1').innerHTML = document.getElementById('txtCustomerName').value" />
            </td>
            <td>
                <asp:TextBox ID="txtCompanyName" runat="server"/>
            </td>
            <td>
                <asp:TextBox ID="txtCity" runat="server"/>
            </td>
            <td>
                <asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="Add" CommandName = "EmptyDataTemplate" />
            </td>
        </tr>
    </EmptyDataTemplate>
</asp:GridView>




Code Behind..

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

namespace TestPage
{
    public partial class Gridwithkey : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.BindData();
            }
        }
        private void BindData()
        {
            string constr = ConfigurationManager.ConnectionStrings["SomeDataBase"].ToString();
            //con = new SqlConnection(constr);
            DataTable dt = new DataTable();
            using (SqlConnection con = new SqlConnection(constr))
            {
                string strQuery = "SELECT * FROM Ragi";
                SqlCommand cmd = new SqlCommand(strQuery);
                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    cmd.Connection = con;
                    con.Open();
                    sda.SelectCommand = cmd;
                    sda.Fill(dt);
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                }
            }
        }

        protected void Add(object sender, EventArgs e)
        {
            Control control = null;
            if (GridView1.FooterRow != null)
            {
                control = GridView1.FooterRow;
            }
            else
            {
                control = GridView1.Controls[0].Controls[0];
            }
            string customerName = (control.FindControl("txtCustomerName") as TextBox).Text;
            string companyName = (control.FindControl("txtCompanyName") as TextBox).Text;
            string city = (control.FindControl("txtCity") as TextBox).Text;
            //string strConnString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
            string constr = ConfigurationManager.ConnectionStrings["SomeDataBase"].ToString();
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.Connection = con;
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = "INSERT INTO [Ragi] VALUES(@CustomerName, @CompanyName, @City)";
                    cmd.Parameters.AddWithValue("@CustomerName", customerName);
                    cmd.Parameters.AddWithValue("@CompanyName", companyName);
                    cmd.Parameters.AddWithValue("@City", city);
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
            }
            Response.Redirect(Request.Url.AbsoluteUri);
        }
       
        
    }
    }


这篇关于通过网格视图插入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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