在buttonclick c#上替换datagridview中的文本 [英] Replacement of text in datagridview on buttonclick c#

查看:167
本文介绍了在buttonclick c#上替换datagridview中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索并替换了datagridview中的文本。但问题是并不是所有的字符串行都被替换了按钮点击只有一些行被替换然后如果我再次单击该按钮然后再多一行被替换。现在我想在单击按钮上替换字符串中的文本。这是我的代码。

I have searched and replaced the text in the datagridview..But the problem is that not all the rows of the string are replaced on the button click only some rows are replaced then if i click the button again then again some more rows are replaced.Now i want to replace the text in a string on a single button click.Here is my code.

private void button7_Click_3(object sender, EventArgs e)
    {
        string filterBy;
        filterBy = "Stringtext Like '%" + textBox6.Text + "%'";
        ((DataTable)dataGridView1.DataSource).DefaultView.RowFilter = filterBy;
        int lent = ((DataTable)dataGridView1.DataSource).DefaultView.RowFilter.Length;
        int rows = dataGridView1.Rows.Count;
        int col = dataGridView1.ColumnCount;
        //   for(int i=0
        //  for (int i = 0; i < rows/2; i++)
        //  {
        //   dataGridView1[2,i].Value=((dataGridView1[2, i].Value.ToString()).ToLower()).Replace(textBox6.Text.ToLower(), textBox7.Text);       //     ((DataTable)dataGridView1.DataSource).DefaultView.RowFilter = String.Format("Stringtext = '{0}'",textBox6.Text.Replace(
        //    dataGridView1[2, i].Value = ((dataGridView1[2, i].Value.ToString()).ToLower());
        //    dataGridView1[2, i].Value = dataGridView1[2, i].Value.ToString().Replace(textBox6.Text.ToLower(), textBox7.Text);
        //    dataGridView1[2, i].Style.ForeColor = Color.Red;
        //   }
        int i = 0;

        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            dataGridView1[2,i].Value = (row.Cells[2].Value.ToString().ToLower()).Replace(textBox6.Text.ToLower(), textBox7.Text);
            i++;
            dataGridView1.Refresh();
        }

    }





有什么想法吗?



Any ideas?

推荐答案



像这样改变你的代码。改变第一行然后colum dataGridView1 [2,i] .Value应该是dataGridView1 [i,2] .Value。

Hi
Change your code like this.Change first row and then colum dataGridView1[2,i].Value should be as dataGridView1[i,2].Value.
foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            dataGridView1[i,2].Value = (row.Cells[2].Value.ToString().ToLower()).Replace(textBox6.Text.ToLower(), textBox7.Text);
            i++;
            dataGridView1.Refresh();
        }


这篇关于在buttonclick c#上替换datagridview中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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