Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
223 views
in Technique[技术] by (71.8m points)

c# - How to find a string in DataGridView from TextBox?

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

enter image description here

after uncheck it will be like this

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...