如何从TextBox中的DataGridView中找到一个字符串? [英] How to find a string in DataGridView from TextBox?

查看:124
本文介绍了如何从TextBox中的DataGridView中找到一个字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Linq Queries的新手,而这个代码是从网络上复制的。

 对象cells = from data in dataGridView1。 Rows.Cast< DataGridViewRow>()其中!r.IsNewRowArray.ConvertAll< DataGridViewCell,string>(r.Cells.Cast< DataGridViewCell>()。ToArray,dgvc => dgvc.Value.ToString)其中c.Contains(textBox1 .text)
选择新单元格{
rowIndex = r.Index,
columnIndex = Array.IndexOf(c,_textBox1.Text)};

并通过使用以下代码,我在datagridview单元格中处理字符串。

  object cells = from data in dataGridView1.Rows.Cast< DataGridViewRow>()其中!r.IsNewRowArray.ConvertAll< DataGridViewCell, string>(r.Cells.Cast< DataGridViewCell>()。ToArray,dgvc => dgvc.Value.ToString)其中c.Contains(_txt)
选择新单元格{
rowIndex = r.Index ,
columnIndex = Array.IndexOf(c,_txt)
};

foreach(dataGridView1.Rows.Cast< DataGridViewRow>())中的DataGridViewRow r {
foreach(r.Cells.Cast< DataGridViewCell>()中的DataGridViewCell c){
if(!r.IsNewRow){
c.Style.BackColor = Color.White;
}
}
}

foreach(单元格中的对象c_loopVariable){
c = c_loopVariable;
DataGridView1.Rows(c.rowIndex).Cells(c.columnIndex).Style.BackColor = Color.Red;
}

并更改该单元格的颜色。



我想要在datagridview中找到第一个空单元格(12列和8行),我的datagridview是按列显示的。当它进入第8行时,它会自动转到下一列,代码写在下面。_col和_row被全局声明

  dataGridView1 [_col,_row] .Value = lvI.Text; 
_row = _row + 1;
if(_row == 8)
{
_row = 0;
_col = _col + 1;
}

其实我正在从listview填充datagridview。当我查看listview项目时,datagridview将根据列明显填充。当我取消选中它会清除包含该文本的datagridview单元格。



我可以显示我的datagridview的图片,以便更好地了解





之后取消选中它将像这样



解决方案

如果要获取包含一些文本的所有单元格,请尝试此代码,您的旧查询具有一些相关性,你错过了这一部分。

  var cells = dataGridView1.Rows.Cast< DataGridViewRow>()
.SelectMany(row => dataGridView1.Columns.Cast< DataGridViewColumn>()
.Select(col => row.Cells [col.Name])
.Where(cell => ; Convert.ToString(cell.Value).Contains(textBox1.Text));

单元格是一个 IEnumerable< DataGridViewCell>



然后更改 BackColor 的代码可以是如下所示:

  foreach(单元格中的var单元格){
cell.Style.BackColor = Color.Red;
}

请注意,您复制的所有代码都是无用的。



要获取第一个空单元格,您可以执行相同但具有不同的条件,如下所示:

  var firstEmptyCell = dataGridView1.Rows.Cast< DataGridViewRow>()
.SelectMany(row => dataGridView1.Columns
.Cast< DataGridViewColumn>()
.Select(col => ; row.Cells [col.Name]))
.OrderBy(cell => cell.ColumnIndex)
.ThenBy(cell => cell.RowIndex)
.FirstOrDefault(cell = > Convert.ToString(cell.Value)==);
//如果没有空单元格,则firstEmptyCell的值可以为null


hi i am new to Linq Queries, And this code is copied from internet

     object cells = from r in dataGridView1.Rows.Cast<DataGridViewRow>()where !r.IsNewRowArray.ConvertAll<DataGridViewCell, string>(r.Cells.Cast<DataGridViewCell>().ToArray, dgvc => dgvc.Value.ToString)where c.Contains(textBox1.Text)
                             select new  cell {
                            rowIndex = r.Index, 
                           columnIndex = Array.IndexOf(c, _textBox1.Text)};

and by using the following code i am fiding the string in a datagridview cell.

 object cells = from r in dataGridView1.Rows.Cast<DataGridViewRow>()where !r.IsNewRowArray.ConvertAll<DataGridViewCell, string>(r.Cells.Cast<DataGridViewCell>().ToArray, dgvc => dgvc.Value.ToString)where c.Contains(_txt)
                             select new  cell {
                            rowIndex = r.Index, 
                           columnIndex = Array.IndexOf(c, _txt) 
 };

foreach (DataGridViewRow r in dataGridView1.Rows.Cast<DataGridViewRow>()) {
    foreach (DataGridViewCell c in r.Cells.Cast<DataGridViewCell>()) {
        if (!r.IsNewRow) {
            c.Style.BackColor = Color.White;
        }
    }
}

foreach (object c_loopVariable in cells) {
    c = c_loopVariable;
    DataGridView1.Rows(c.rowIndex).Cells(c.columnIndex).Style.BackColor = Color.Red;
}

and changing the color of that cell.

And i want to find the first empty cell in datagridview i have (12 columns and 8 rows) my datagridview is filled by column wise. when it entering to the 8th row it is automatically goes to next column, Code is written below.Where _col and _row is declared globally

        dataGridView1[_col, _row].Value = lvI.Text;
                _row = _row + 1;
                if (_row == 8)
                {
                    _row = 0;
                    _col = _col + 1;
                }

actually i am filling datagridview from listview. when i check the listview item the datagridview is filled based on column wise. when i uncheck it clears the datagridview cell which contains that text.

i can show the picture of my datagridview for better understanding

after uncheck it will be like this

解决方案

If you want to get all the cells containing some text, try this code, your old query has some dependency and you missed that part.

var cells = dataGridView1.Rows.Cast<DataGridViewRow>()
             .SelectMany(row=>dataGridView1.Columns.Cast<DataGridViewColumn>()
                              .Select(col=>row.Cells[col.Name]))
             .Where(cell=>Convert.ToString(cell.Value).Contains(textBox1.Text));

The cells is an IEnumerable<DataGridViewCell>

Then the code to change the BackColor can be like this:

foreach (var cell in cells) {
   cell.Style.BackColor = Color.Red;
}

Note that all the code you copied is useless.

To get the first empty cells, you do the same but with a different condition like this:

var firstEmptyCell = dataGridView1.Rows.Cast<DataGridViewRow>()
                    .SelectMany(row=>dataGridView1.Columns
                                    .Cast<DataGridViewColumn>()
                                    .Select(col=>row.Cells[col.Name]))
                    .OrderBy(cell=>cell.ColumnIndex)
                    .ThenBy(cell=>cell.RowIndex)
                    .FirstOrDefault(cell=>Convert.ToString(cell.Value) == "");
//The value of firstEmptyCell may be null if there is not any empty cell

这篇关于如何从TextBox中的DataGridView中找到一个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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