我有*。我将读入GridView的Mdf数据库......然后我将为用户提供搜索人员的能力(通过姓氏)。代码工作正常,直到搜索。当我输入姓氏并考虑搜索时,则会收到来自Catch up的错误消息。
我是C#的初学者,谦虚地请求认真的答案。
提前致谢。
我的代码
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'baseballDataSet.Players' table. You can move, or remove it, as needed.
this.playersTableAdapter.Fill(this.baseballDataSet.Players);
}
private void search_Click(object sender, EventArgs e)
{
string searchValue = sInput.Text;
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
try
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells[3].Value.ToString().Equals(searchValue))
{
row.Selected = true;
break;
}
}
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
}
答案 0 :(得分:1)
你应该在你的foreach中跳过row.IsNewRow == true的行... .Value在那里为空...
try
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.IsNewRow) continue;
if (row.Cells[3].Value.ToString().Equals(searchValue))
{
....