Datagridview
how to find a particular value from datagridview when it is connected to a database
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Roei BarRank #1,271 · 1.4K reputationPosted Sep 4, 2009, 3:58 AM EDT
hope this helps
Kirtan PatelRank #292 · 8.6K reputationPosted Sep 4, 2009, 6:54 AM EDT
Here is What you want
it will Search The DataGridView and Tell you in Which Row it is Located :)
dont forget to mark "Do you like answer" please it will give me some credits :)
private void btnSearch_Click(object sender, EventArgs e)
{
bool Found = false;
int FoundRowIndex=0 , FoundColIndex=0 ;
string SearchText = txtSearch.Text.Trim();
foreach (DataGridViewRow r in dataGridView1.Rows)
{
if (Found == false)
{
int rindex = r.Index;
int columnCount = dataGridView1.Columns.Count-1;
for (int i = 0; i < columnCount; i++)
{
if (dataGridView1.Rows[rindex].Cells[i].Value.ToString() == SearchText)
{
FoundRowIndex = r.Index;
FoundColIndex = i;
Found = true;
}
}
}
else
{
break;
}
}
if (Found == true)
{
MessageBox.Show("Found at Row =" + FoundRowIndex);
}
else
{
MessageBox.Show("Value Not Found");
}
}