[英]Get Values from Datagridview related to to certain values in another column c#
So here is my problem. 所以这是我的问题。 I have not been coding very long and i have been pulling my hair out on this issue for 2 days now and cant get an answer that works...
我没有编码很长时间,并且我已经花了两天的时间解决这个问题,并且无法获得有效的答案...
Form1 has a datagridview with 3 columns and multiple rows of data. Form1的datagridview具有3列和多行数据。 If i press a button on Form1 i need it to populate all the values in column 1 that has a certain value in column[2] and then display it in a textbox on Form2.
如果我在Form1上按下按钮,则需要它填充列1中在column [2]中具有某个值的所有值,然后将其显示在Form2的文本框中。
Example: 例:
So if I click a button i want to pull all the amounts for car1 and display the total in a textbox on form2. 因此,如果我单击一个按钮,我想提取car1的所有金额,并在form2的文本框中显示总计。
Form2 obj = new Form2();
public void button4_Click(object sender, EventArgs e)
{
String searchValue = "Car1";
int rowIndex = 1;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells[2].Value.ToString().Equals(searchValue))
{
rowIndex = row.Index;
obj.Controls["TextBox1"].Text = dataGridView1.Rows[rowIndex].Selected.ToString();
obj.Show();
}
else
{
break;
}
}
}
I just get the answer "true" in my textbox 我刚在文本框中得到答案“ true”
I hope this makes sense 我希望这是有道理的
Thanks in advance 提前致谢
Why do you break foreach
loop. 为什么要中断
foreach
循环。 As i understand, you want all other amounts of Car1. 据我了解,您需要其他数量的Car1。 Right?
对? Check this out:
看一下这个:
Form2 obj = new Form2();
public void button4_Click(object sender, EventArgs e)
{
String searchValue = "Car1";
int amount = 0;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if(row.Cells["Type"].Value.ToString().Equals(searchValue)
{
amount+ = Convert.ToInt32(row.Cells["Amount"].Value.ToString())
}
}
if(amount > 0 )
{
obj.Controls["TextBox1"].Text = amount.ToString();
}
else
{
obj.Controls["TextBox1"].Text = "No car found!";
}
obj.Show();
}
Also you need to control if Amount is a number or not. 另外,您还需要控制“金额”是否为数字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.