将所有数据从 dataGridView 传输到文本框
Transferring all data from dataGridView to a textbox
问题来了。我有一个餐厅程序,客户可以在其中选择他想吃的东西,并且该数据显示在 dataGridView 中。例如,6 种不同的菜肴 = 6 行不同的行,每行包含名称、数量和价格。毕竟我需要打印账单,所以我想将所有信息从 dataGridView 获取到文本框。所有行。我该怎么做?
P.S。我搜索了很多,但只有关于如何将 CurrentRow 数据传输到我找到的文本框的信息。
private void Form4_Load(object sender, EventArgs e)
{
Form1 f1 = new Form1();
string billinfo = string.Empty;
foreach (DataGridViewRow row in f1.dataGridView1.Rows)
{
billinfo = string.Format("{0}{1} {2} {3}{4}", billinfo, row.Cells["Name"].Value, row.Cells["Amount"].Value, row.Cells["Price"].Value, Environment.NewLine);
}
textBox1.Text = billinfo;
}
只需遍历每一行并将列值格式化为字符串,添加 System.Environment.NewLine
以分隔 TextBox
中的条目。
string billInfo = string.Empty;
foreach (DataGridViewRow row in this.dataGridView1.Rows)
{
billInfo = string.Format("{0}{1} {2} {3}{4}", billInfo, row.Cells["Name"].Value, row.Cells["Amount"].Value, row.Cells["Price"].Value, Environment.NewLine);
}
this.textBox1.Text = billInfo;
看起来你有多个表格。每个表单都有自己的 "Context" 以跟踪 "controls" 属于每个表单的内容。
在您的代码中"Form4_Load" 是 "Form4" 启动期间触发的事件。然后在这个方法中创建 "new Form1()"。这是 Form1 类型的新空窗体。但它与 "Form1" 的第一个实例没有任何关联或链接。您必须使用已经创建的 "f1".
修复程序的粗略方法如下(如下),但获取对象的全局实例并使用并不是一个好习惯。很容易忘记哪些对象是有效的。无论如何,这是我的简单修复。
enter code here
private void Form4_Load(object sender, EventArgs e)
{
//I am assuming f1 is the name of your original Form1
String f1DataGridViewName = f1.dataGridView1.Name.ToString();
int f1RowCount = f1.RowCount;
string billinfo = string.Empty;
Console.Writeline("Form1 f1 Datagridview name is: {0}
,Form1 f1 DataGridview row count is : {1}"
,f1DataGridViewName, f1RowCount );
foreach (DataGridViewRow row in f1.dataGridView1.Rows)
{
billinfo = string.Format("{0}{1} {2} {3}{4}", billinfo
, row.Cells["Name"].Value
, row.Cells["Amount"].Value
, row.Cells["Price"].Value
, Environment.NewLine);
}
textBox1.Text = billinfo;
}
问题来了。我有一个餐厅程序,客户可以在其中选择他想吃的东西,并且该数据显示在 dataGridView 中。例如,6 种不同的菜肴 = 6 行不同的行,每行包含名称、数量和价格。毕竟我需要打印账单,所以我想将所有信息从 dataGridView 获取到文本框。所有行。我该怎么做?
P.S。我搜索了很多,但只有关于如何将 CurrentRow 数据传输到我找到的文本框的信息。
private void Form4_Load(object sender, EventArgs e)
{
Form1 f1 = new Form1();
string billinfo = string.Empty;
foreach (DataGridViewRow row in f1.dataGridView1.Rows)
{
billinfo = string.Format("{0}{1} {2} {3}{4}", billinfo, row.Cells["Name"].Value, row.Cells["Amount"].Value, row.Cells["Price"].Value, Environment.NewLine);
}
textBox1.Text = billinfo;
}
只需遍历每一行并将列值格式化为字符串,添加 System.Environment.NewLine
以分隔 TextBox
中的条目。
string billInfo = string.Empty;
foreach (DataGridViewRow row in this.dataGridView1.Rows)
{
billInfo = string.Format("{0}{1} {2} {3}{4}", billInfo, row.Cells["Name"].Value, row.Cells["Amount"].Value, row.Cells["Price"].Value, Environment.NewLine);
}
this.textBox1.Text = billInfo;
看起来你有多个表格。每个表单都有自己的 "Context" 以跟踪 "controls" 属于每个表单的内容。
在您的代码中"Form4_Load" 是 "Form4" 启动期间触发的事件。然后在这个方法中创建 "new Form1()"。这是 Form1 类型的新空窗体。但它与 "Form1" 的第一个实例没有任何关联或链接。您必须使用已经创建的 "f1".
修复程序的粗略方法如下(如下),但获取对象的全局实例并使用并不是一个好习惯。很容易忘记哪些对象是有效的。无论如何,这是我的简单修复。
enter code here
private void Form4_Load(object sender, EventArgs e)
{
//I am assuming f1 is the name of your original Form1
String f1DataGridViewName = f1.dataGridView1.Name.ToString();
int f1RowCount = f1.RowCount;
string billinfo = string.Empty;
Console.Writeline("Form1 f1 Datagridview name is: {0}
,Form1 f1 DataGridview row count is : {1}"
,f1DataGridViewName, f1RowCount );
foreach (DataGridViewRow row in f1.dataGridView1.Rows)
{
billinfo = string.Format("{0}{1} {2} {3}{4}", billinfo
, row.Cells["Name"].Value
, row.Cells["Amount"].Value
, row.Cells["Price"].Value
, Environment.NewLine);
}
textBox1.Text = billinfo;
}