每次运行foreach循环时,将其添加到SelectedCells [0] [英] Adding to the SelectedCells[0] each time the foreach loops is run
问题描述
我在C#程序中为每个loopp都有一个,并且希望每次循环运行时都将其添加到SelectedCells [*].value中.
第一次运行时,我需要运行以下命令:dgv.SelectedCells [0] .Value;
在下次运行时,我希望它运行以下命令:dgv.SelectedCells [1] .Value;
以此类推,直到循环中没有更多行可以通过.
这是我当前的代码,该代码仅获取第一个单元格值.我还需要它来检查rows_w_checked_column并仅使用这些行.
Hi There,
I have a for each loopp in my C# program and would like to add to the SelectedCells[*].value each time the loop runs.
For the first time it runs I need to run the following: dgv.SelectedCells[0].Value;
The on the next time it runs I want it to run the following : dgv.SelectedCells[1].Value;
As so on until the loop has no more rows to go through.
Here is my current code, which only picks up the first cell value. I also need this to check the rows_w_checked_column and only use these rows.
private void button5_Click(object sender, EventArgs e)
{
if (dgv.Rows.Count <= 0)
{
MessageBox.Show("No List to save.", "Empty Selection", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
if (dgv.Rows.Count >= 1)
{
DialogResult dr = MessageBox.Show("Are you sure you want to save the data", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (dr == DialogResult.Yes)
{
List<datagridviewrow> rows_w_checked_column = new List<datagridviewrow>();
foreach (DataGridViewRow row in dgv.Rows)
{
if (Convert.ToBoolean(row.Cells[colPrint.Name].Value) == true)
{
rows_w_checked_column.Add(row);
int x;
DateTime createdDate;
createdDate = Convert.ToDateTime(DateTime.Now.ToString("dd-MMM-yyyy"));
// Database connection
SqlConnection frsCon = new SqlConnection();
frsCon.ConnectionString = "Data Source=DEVSERVER;Initial Catalog=FRS;Integrated Security=True";
SqlDataAdapter frsApt = new SqlDataAdapter();
SqlCommandBuilder frsCmdBlr = new SqlCommandBuilder();
DataTable frsDGV = new DataTable();
BindingSource frsBS = new BindingSource();
SqlCommand frsCommand = new SqlCommand("INSERT INTO tblAddress (colAddress, createdDate) VALUES (@colAddress, @createdDate)", frsCon);
frsCommand.Parameters.Add("@colAddress", SqlDbType.VarChar).Value = dgv.SelectedCells[0].Value;
frsCommand.Parameters.Add("@createdDate", SqlDbType.DateTime).Value = createdDate;
frsCon.Open();
x = frsCommand.ExecuteNonQuery();
frsCon.Close();
}
}
}
}
}
当前,它将添加所有已在复选框列中打勾的记录,但显示每个条目的第一个单元格中的值.
请帮助:-)
[edit]已添加代码块,HTML编码-OriginalGriff [/edit]
Currently it will add all of the records which have been ticked in the checkbox column, but displays the value from the first cell for each entry.
Please help :-)
[edit]Code block added, HTML encoded - OriginalGriff[/edit]
推荐答案
使用您设置foreach
所使用的row
参数.目前,您始终使用相同的数据:
Use therow
parameter you set up theforeach
to use. At present you are always using the same data:
frsCommand.Parameters.Add("@colAddress", SqlDbType.VarChar).Value = dgv.SelectedCells[0].Value;
Try:
Try:
frsCommand.Parameters.Add("@colAddress", SqlDbType.VarChar).Value = row.Cells[0].Value;
这篇关于每次运行foreach循环时,将其添加到SelectedCells [0]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!