在dgv中输入数据,然后单击保存,保存导数据库中,我现在sql里是空的,在dgv中输入数据,然后点击保存才能存入数据,现在的问题是,我不知道怎么讲dgv中的数据保存的数据库中,忘大侠们帮忙,小弟刚刚学习c#不久,往大侠们帮助,最好是代码,小弟感激不尽,谢谢了!!!
解决方案 »
- winform图片切换
- C#实现单点登陆(ADO.net)
- 想给Label添加一个新属性
- 怎样将一块SQL语句自动格式化为一个字符串当作一个字符串变量,便于在C#中作为参数,省得人工用许多加号“+”和分号“"”连接各个SQL句子。
- datagridview 中的数据导出到excel
- 求教拖动窗体内控件的问题
- 谁用的是WINDOWS SERVER 2003 ,帮个忙啊
- 没有可用分的进来,我还有一点。全捐出去。(哎,看了张爱玲的〈〈十八春〉〉内心感动加郁闷ING,因此捐分)
- 关于Windows Service的一个古怪的错误 1+1!=2???
- 很简单的一个关于字符串的操作问题
- datatable 写入记事本
- datagirdview 单元格问题 急!
DataSet ds = new DataSet();
SqlDataAdapter sda;SqlCommandBuilder scb = new SqlCommandBuilder(sda);
sda.Update(ds);
this.dataGridView1.DataSource = ds.Tables[0];
<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();
}
}
}
this.dgv.DataSource = ds.Tables["table"];
DataTable table = (DataTable)dgv.DataSource;
zGDA_LUTableAdapter.Update(zGDA_LU_DataSet);
MessageBox.Show("Successfully insert into!");我这样 就能 在dgv中输入数据 然后点击 保存到数据库中 没用intertinto....
我想这种简单些吧..
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.