在DataGridView中搜索(没有数据库或查询绑定) [英] Search in DataGridView (without database or query binded)

查看:201
本文介绍了在DataGridView中搜索(没有数据库或查询绑定)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天;

我想通过DataTable在空白DataGridView中添加行,此添加应遵循1条规则(伪代码):

IF项目在添加之前,通过refrenced TextBox更新'Number'单元格(DataGridView Cell:FName,LName,Number)ELSE添加新行。



但是有两个问题:

1.第一行无法通过以下代码添加(无错误)。

(第一行应该由另一个按钮或默认值添加)

2.比较找到一个类似于正在添加的行的现有行,只播放DataGridView的第一行。



Good day;
I want to add row in blank DataGridView by DataTable, this adding should follow 1 rule(in pseudo code):
"IF item before added, update 'Number' cell by refrenced TextBox (DataGridView Cell's: FName, LName, Number) ELSE add new row".

But there are two problems:
1. First row could not be added by below code (no error).
(first row should be add by another button or default value)
2. Compare to find an existent row similar to the row being added, played only the first row of the DataGridView.

dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
    if ((row.Cells[1].Value.ToString().Equals(txt_Name.Text)) &&   (row.Cells[2].Value.ToString().Equals(txt_Family.Text)))
    {           
        row.Cells[3].Value = txt_Number.Text;
        dataGridView1.DataSource = dt;          
        break;
    }
    else
    {           
        dt.Rows.Add(txt_Code.Text, txt_Name.Text, txt_Family.Text, txt_Number.Text);
        dataGridView1.DataSource = dt;
        break;          
    }
}

推荐答案

我在看这个:

I was looking this:
try
{
    for (int i = 0; i < dataGridView1.RowCount; i++)
    {
        if (dataGridView1.Rows[i].Cells[0].Value.ToString() == txt_FName.Text)
        {
            dataGridView1.Rows[i].Cells[2].Value = [any proccess] ; // Update Row
            return;
        }
    }
}
catch(Exception ex)
{
    MessageBox.Show(ex.Message);
}
dataGridView1.Rows.Add(txt_FName.Text, txt_LName.Text, txt_Number.Text); // New Row


这篇关于在DataGridView中搜索(没有数据库或查询绑定)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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