大家好,如果可能的話,我希望你能得到幫助:
在第一個表單中,當我單擊Télécharger PDF按鈕而不選擇行時,它會在第二個表單中將資料發送到另一個表單,它會自動選擇所有DataGridView行TextBoxes:DataGridView
- 問題是我想在第二個單元格中添加第一列的所有值,例如“Arduino scotch”
DataGridView。 - 我認為這與我想將 Sum 添加到 form2 的一個單元格中的總數相同。


表格1:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
string firstcol = textBox3.Text;
string secondcol = textBox1.Text;
string thirdtcol = dataGridView1.CurrentRow.Cells[0].ToString();
string fourcol = dataGridView1.CurrentRow.Cells[4].ToString();
//thirdcole and fourcol are not working even for 1 row
string[] rows = { firstcol, secondcol, thirdtcol, fourcol };
f2.dataGridView1.Rows.Add(rows);
f2.Show();
this.Hide();
}
表格2:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void A_commande_etat_Load(object sender, EventArgs e)
{
}
}
uj5u.com熱心網友回復:
Form2添加一個方法,該方法采用fromdataGridView1并Form1遍歷 deciered 列并創建所需的連接/求和值,然后將其添加為dataGridViewin的新行Form2。
表格1
public partial class Form1 : Form
{
Form2 f2; //Don't create a new f2 object each time button is pressed
public Form1()
{
InitializeComponent();
f2 = new Form2();
}
private void button1_Click(object sender, EventArgs e)
{
f2.Show();
//Send the datagridview of Form1 to Form 2, let Form2 do all the logic
f2.loadColumns(this.dataGridView1);
}
}
表格2
using System.Text;
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public void loadColumns(DataGridView dataGridView)
{
StringBuilder buildArticleString = new StringBuilder();
int articleColIndex = 0; //Your article col index in Form1
int totalColIndex = 4; //Your total col index in Form1
int totalSum = 0;
foreach(DataGridViewRow currentRow in dataGridView.Rows)
{
if (currentRow.Cells[articleColIndex].Value == null || currentRow.Cells[totalColIndex].Value == null)
continue;
//Retrive cell value at article column for current row, append to stringbuilder
buildArticleString.Append(currentRow.Cells[articleColIndex].Value.ToString());
buildArticleString.Append(" ");
int currentTotalValue = 0;
//Checks if value in cell can be converted to int, skips if not
if (Int32.TryParse(currentRow.Cells[totalColIndex].Value.ToString(), out currentTotalValue))
{
totalSum = currentTotalValue;
}
}
buildArticleString.Remove(buildArticleString.Length - 3, 3); //Remove trailing " ";
//Make sure this matches your columns in Form2
string[] rowValues = new string[] {"", "", buildArticleString.ToString(), totalSum.ToString(), "", "" };
dataGridView1.Rows.Add(rowValues); //Add new row to Form2
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/432582.html
上一篇:如何參考ListBox項
