如何一次保存多个textBoxes和dataGridView [英] How can I save multiple textBoxes and dataGridView at once

查看:82
本文介绍了如何一次保存多个textBoxes和dataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我创建了一个名为NewInvoice的表单,该表单包含 3个文本框(InvoiceID,日期和总计),一个 dataGridView和保存按钮 .我的问题是如何一次保存 textBoxes和包含多行的dataGridView.

----------
InvoiceID
----------
----------
日期
----------

-------------------------------------------------- ------------------

dataGridView


-------------------------------------------------- -------------------
-------
总计
-------

保存(按钮)

任何的想法? :confused:

在此先感谢,
Zabamkd

Hi

I created one form called NewInvoice and the form contains 3 textBoxes (InvoiceID, Date and Total), one dataGridView and save button. My problem is how can I save at once textBoxes and the dataGridView that contains multiple rows.

----------
InvoiceID
----------
----------
Date
----------

--------------------------------------------------------------------

dataGridView


---------------------------------------------------------------------
-------
Total
-------

SAVE(Button)

Any Idea? :confused:

Thanks in Advance,
Zabamkd

推荐答案

这个问题根本没有道理.您要保存网格,还是显示值?您在哪里保存它们?你尝试了什么?您面临什么问题?请编辑您的问题以反映此信息,然后我们也许可以尝试为您提供帮助.
This question makes no sense at all. Are you saving the grid, or does it show the values ? Where are you saving them ? What have you tried ? What problems are you facing ? Please edit your question to reflect this information and then perhaps we can try to help you.


该问题很有意义:).像以前一样,我的问题是保存3个文本框和一个可以包含几行的dataGridView.我在数据库中制作了2个表,分别为 tableHead(发票ID,日期和总计)和tableBody(产品ID,发票ID,产品名称,数量和价格),这些表之间的关系=> tableHead.InvoiceID = tableBody.InvoiceID.我正在为应用程序使用sql数据库和语言c#.

我尝试编写一些代码,但仍然无法保存表单.代码是:


私有void SaveButton_Click(对象发送者,EventArgs e)
{

SqlConnection cnn =新的SqlConnection();
cnn.ConnectionString = @数据源= IVAN-PC;初始目录=模型;集成安全性=真";
SqlCommand cmd =新的SqlCommand();
DataSet ds = new DataSet();
SqlDataAdapter da =新的SqlDataAdapter();

//没有InvoiceID的标题文本框,因为它是增量...
字符串head ="INSERT INTO tableHead(Date)values(''" + dateTextBox.Text +''";

//选择InvoiceID ...
字符串Invoice =从表头中选择SELECT InvoiceID";

//不使用ProductID填充datagridview ...
//也许我必须创建for或其他内容才能正确地逐行填充...
字符串InvoiceID = this.DataGridView.CurrentRow.Cells [imeIndex] .Value.ToString().Trim();
字符串ProductName = this.DataGridView.CurrentRow.Cells [imeIndex] .Value.ToString().Trim();
字符串数量= this.DataGridView.CurrentRow.Cells [imeIndex] .Value.ToString().Trim();
字符串价格= this.DataGridView.CurrentRow.Cells [imeIndex] .Value.ToString().Trim();

字符串正文="INSERT INTO tableBody(InvoiceID,ProductName,Quantity,Price)值(""+ InvoiceID +"''," + ProductName +'',""+数量+"'','' "+过程+")";

cmd.CommandText = head;
cmd2.CommandText =正文;
cmd.Connection = cnn;
cnn.Open();
cmd =新的SqlCommand(head,cnn);
cmd =新的SqlCommand(Body,cnn);
cmd.ExecuteNonQuery();
cmd2.ExecuteNonQuery();
cnn.Close();

如果(MessageBox.Show(成功保存!",保存",MessageBoxButtons.OK)== DialogResult.OK)
{
this.Close();
}
}

有人可以帮忙吗?

预先感谢,
zabamkd
The question make sense:) . Like before my problem is to save 3 textboxes and one dataGridView that can contain several rows. I made 2 tables in the database called tableHead(InvoiceID, Date and Total) and tableBody (ProductID, InvoiceID, ProductName, Quantity and Price) the tables are in relationship => tableHead.InvoiceID = tableBody.InvoiceID. I am using sql database and language c# for the application.

I try to program some code but I still can not save the form. The code is:


private void SaveButton_Click(object sender, EventArgs e)
{

SqlConnection cnn = new SqlConnection();
cnn.ConnectionString = @"Data Source=IVAN-PC;Initial Catalog=model;Integrated Security=True";
SqlCommand cmd = new SqlCommand();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter();

//Head textbox without InvoiceID because is increment...
string head = "INSERT INTO tableHead (Date) values(''" + dateTextBox.Text +"''";

//InvoiceID select...
string Invoice = "SELECT InvoiceID FROM tableHead";

//Populate datagridview without ProductID...
//Maybe I have to create for or something else to correctly populate row by row...
string InvoiceID = this.DataGridView.CurrentRow.Cells[imeIndex].Value.ToString().Trim();
string ProductName = this.DataGridView.CurrentRow.Cells[imeIndex].Value.ToString().Trim();
string Quantity = this.DataGridView.CurrentRow.Cells[imeIndex].Value.ToString().Trim();
string Price = this.DataGridView.CurrentRow.Cells[imeIndex].Value.ToString().Trim();

string body = "INSERT INTO tableBody(InvoiceID, ProductName, Quantity, Price) values (''" + InvoiceID + "'',''" + ProductName + "'',''" + Quantity + "'',''" + Proce + "'')";

cmd.CommandText = head;
cmd2.CommandText = Body;
cmd.Connection = cnn;
cnn.Open();
cmd = new SqlCommand(head, cnn);
cmd = new SqlCommand(Body, cnn);
cmd.ExecuteNonQuery();
cmd2.ExecuteNonQuery();
cnn.Close();

if (MessageBox.Show("Successfully saved! ", "Saving", MessageBoxButtons.OK) == DialogResult.OK)
{
this.Close();
}
}

Can someone Help?

Thanks in advance,
zabamkd


嗨Zabamkd,
如果您有一个包含三个文本框的屏幕以及一个包含多行的网格,则可能会将多个记录保存到数据库中的多个表中.您可能想看一下使用数据适配器对象一次执行多条记录.您可以尝试搜索 [ ^ ]或在CP上搜索文章以为您提供帮助.以下是我发现对您有所帮助的一些信息:
使用DataSet和DataAdapter插入关系数据 [为初学者使用ADO.NET构建数据库应用程序. [
Hi Zabamkd,
If you have a screen with three textboxes and also a grid with multiple rows you are probably saving multiple records to multiple tables in your database. You probably want to look at using a Data Adapter object to do multiple records at a time. You could try googling [^]or searching CP for articles to help you. Here are some that I found that you may find helpful:
Inserting relational data using DataSet and DataAdapter[^]
Building database application with ADO.NET for a beginner.[^]

Also, about the code you posted. You said that it didn''t work. Please let us know what error message you get or what is happening that is incorrect and we can try to help you more from there.

Hope this helps.


这篇关于如何一次保存多个textBoxes和dataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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