如何通过Datagridview迭代并搜索特定字符串。然后移动到右侧的下一个单元格并保存单元格中的值 [英] How Do I Iterate Through A Datagridview And Search For A Specific String. Then Move To The Next Cell On The Right And Save The Value In The Cell

查看:72
本文介绍了如何通过Datagridview迭代并搜索特定字符串。然后移动到右侧的下一个单元格并保存单元格中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void button8_Click(object sender, EventArgs e)
        {

            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                foreach (DataGridViewCell cell in row.Cells)
                {
                    if (cell.Value == DBNull.Value || cell.Value == null)
                        continue;
                    if (cell.Value.ToString().Contains("Payment"))
                        {

                           " move to the next cell on the right and save value"
                           " go to the next row and do same"
                            " sum up all values and output to textbox"

                       }

推荐答案

最简单的方法是不使用 foreach - 如果你使用代替循环,并通过单元格索引,则下一个单元格是索引+ 1(假设你不是'在行的最后一个单元格中 - 可以通过循环到numbe来确保r减去一列。

The easiest way is to not use foreach on the Cells - if you use a for loop instead, and index through the cells, then the next cell is the index + 1 (assuming you aren't in the last cell of the row - which can be assured by looping to the number of columns minus one).
foreach (DataGridViewRow row in dataGridView1.Rows)
    {
    for (int index = 0; index < dataGridView1.ColumnCount - 1; index++)
        {
        DataGridViewCell cell = row.Cells[index];
        if (cell.Value == DBNull.Value || cell.Value == null)
            continue;
        if (cell.Value.ToString().Contains("Payment"))
            {
            DataGridViewCell next = row.Cells[index + 1];


终于搞定了!谢谢您的帮助。解决方案下面





private void button_Click(对象发送者,EventArgs e)

{

十进制值= 0;

十进制和= 0;



foreach(dataGridViewRow中的dataGridView1.Rows行)

{

for(int index = 0; index< dataGridView1.ColumnCount - 1; index ++)

{

DataGridViewCell cell = row.Cells [index];

if(cell.Value == DBNull.Value || cell.Value == null)

继续;

if(cell.Value.ToString()。包含(付款))

{

DataGridViewCell next = row.Cells [index + 1]; < br $>


value = Convert.ToDecimal(row.Cells [index + 1] .Value);

sum + = value;



textBox22.Text = sum.ToString();







}
Finally got it! Thanks for the help. Solution below


private void button_Click(object sender, EventArgs e)
{
decimal value=0;
decimal sum=0;

foreach (DataGridViewRow row in dataGridView1.Rows)
{
for (int index = 0; index < dataGridView1.ColumnCount - 1; index++)
{
DataGridViewCell cell = row.Cells[index];
if (cell.Value == DBNull.Value || cell.Value == null)
continue;
if (cell.Value.ToString().Contains("Payment"))
{
DataGridViewCell next = row.Cells[index + 1];

value = Convert.ToDecimal(row.Cells[index + 1].Value);
sum += value;

textBox22.Text = sum.ToString();



}


这篇关于如何通过Datagridview迭代并搜索特定字符串。然后移动到右侧的下一个单元格并保存单元格中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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