在dgv中输入数据,然后单击保存,保存导数据库中,我现在sql里是空的,在dgv中输入数据,然后点击保存才能存入数据,现在的问题是,我不知道怎么讲dgv中的数据保存的数据库中,忘大侠们帮忙,小弟刚刚学习c#不久,往大侠们帮助,最好是代码,小弟感激不尽,谢谢了!!!

解决方案 »

  1.   

    更新datagridview
    DataSet ds = new DataSet();
    SqlDataAdapter sda;SqlCommandBuilder scb = new SqlCommandBuilder(sda);
    sda.Update(ds);
    this.dataGridView1.DataSource = ds.Tables[0];
      

  2.   

    <%@ 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>无标题页</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
            <Columns>
                <asp:TemplateField HeaderText="aaa">
                <ItemTemplate><asp:TextBox ID="aaa" runat="server" Text='<%# Eval("aaa") %>'></asp:TextBox></ItemTemplate>
                </asp:TemplateField>
                            <asp:TemplateField HeaderText="bbb">
                <ItemTemplate><asp:TextBox ID="aaa" runat="server" Text='<%# Eval("bbb") %>'></asp:TextBox></ItemTemplate>
                </asp:TemplateField>
            </Columns>
            </asp:GridView>
            <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
            
        </div>
        </form>
    </body>
    </html>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.SqlClient;public partial class Default2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("aaa");
                dt.Columns.Add("bbb");
                for (int i = 0; i < 10; i++)
                {
                    DataRow dr = dt.NewRow();
                    dr["aaa"] = "aaa" + i;
                    dr["bbb"] = "bbb" + i;
                    dt.Rows.Add(dr);
                }
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow gvr in GridView1.Rows)
            {
                string straaa = (((TextBox)gvr.FindControl("aaa")).Text);
                string strbbb = (((TextBox)gvr.FindControl("bbb")).Text);
                SqlConnection conn = new SqlConnection("连接字符串");
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandText = "insert into table1(aaa,bbb) values('" + straaa + "','" + strbbb + "')";
                conn.Open();
                int result = cmd.ExecuteNonQuery();//
                conn.Close();
            }
            
        }
    }
      

  3.   

    不是要把Dgv中的数据放到数据库里面吗?
      

  4.   

    对呀 是在dgv中输入数据 然后 导入到sql啊!
      

  5.   

    想这样的  不用interinto了吧?
      

  6.   

    其实数据导入数据库的思路,不就是把数据插进去吗?插入数据就必须用insert
      

  7.   

    那你的数据库是oracle还是sql server什么呢,你把连接字符串改成你的服务器
      

  8.   

    你的意思是不是想在DataGridView里面搞几个输入框+一个按钮 ,点击按钮的时候把把这一行的数据保存到数据库去啊!~ 
      

  9.   

    其实我的想法是 dgv与dataset建立联系  然后直接更新数据源 不就能直接的更新到数据库了吗  所以 才没考虑  intert  我想 不用那个应该也可以的吧
      

  10.   

    DataSet ds = new DataSet();
    this.dgv.DataSource = ds.Tables["table"];
    DataTable table = (DataTable)dgv.DataSource;
    zGDA_LUTableAdapter.Update(zGDA_LU_DataSet);
    MessageBox.Show("Successfully insert into!");我这样 就能 在dgv中输入数据 然后点击 保存到数据库中   没用intertinto....
    我想这种简单些吧.. 
      

  11.   

    现在 我就没用 intertinto  就可以了 但是 不知道 这种 好不好....
      

  12.   

    耐心看看这篇文章,会对你有帮助!
    http://www.switchonthecode.com/tutorials/csharp-tutorial-binding-a-datagridview-to-a-databaseby the user in the DataGridView will automically be made to the DataTable, dTable. Now we need a way to get the changes back into the database. All you have to do is call the Update function of the OleDbDataAdapter with the DataTable as the argument to accomplish this.dAdapter.Update(dTable);This call will use the OleDbCommandBuilder to create all of the necessary SQL code to synchronize your database with the changes made to dTable. But, when should you call this function? There's lots of different answers to that. If you have a save button, call it when the user pushes save. If you want the database updated in real-time, I like to call it on the DataGridView's Validating event.