damla.yilmazer

Hi,

I want to search a string value in datagridview. But i don't know how i do it. please help me. I try to do it and finally wrote a code like it:

string aa= cozellik.SelectedItem.ToString();

if(aa == dataGridView1.Rows[0].Cells[1].Value.ToString())
{MessageBox.Show("There is this record.");}
else{}
but this code searching in Rows[0]. I want it search in all rows at datagridview.
Sorry for my bad english. I hope that i can tell my problem.


Re: Windows Forms Data Controls and Databinding find a string in datagridview

CLR_cn

damla.yilmazer wrote:

Hi,

I want to search a string value in datagridview. But i don't know how i do it. please help me. I try to do it and finally wrote a code like it:

string aa= cozellik.SelectedItem.ToString();

if(aa == dataGridView1.Rows[0].Cells[1].Value.ToString())
{MessageBox.Show("There is this record.");}
else{}
but this code searching in Rows[0]. I want it search in all rows at datagridview.
Sorry for my bad english. I hope that i can tell my problem.

Your english is better than me!

....

foreach(DataRow row in dataGridView1.Rows)

{

if(aa == row.Cells[1].Value.ToString())

{

MessageBox.Show ...............

break;

}

}




Re: Windows Forms Data Controls and Databinding find a string in datagridview

damla.yilmazer

Thanks for ur reply.

but i give an error that: System.Data.DataRow does not contain a defination for 'Cells'.

with my best wishes..





Re: Windows Forms Data Controls and Databinding find a string in datagridview

timvw

That should have been:

Code Snippet

foreach(DataGridViewRow row in dataGridView1.Rows)
{
// in case you want to find the value as it appears on the screen, you might want to use the
// FormattedValue property instead of Value
if (row.Cells[1].Value.ToString() == "something")
{
//
}
}







Re: Windows Forms Data Controls and Databinding find a string in datagridview

Gavin Jin - MSFT

Hi,if you want to fielter in datagridview,you can also do in this way

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

Dim myView As DataView = New DataView()

myView.Table = Me.ds.Tables(0)

myView.RowFilter = "LastName='" & TextBox1.Text & "'"

DataGridView1.DataSource = myView

If myView.Count = 0 Then

MessageBox.Show("Find nothing")

Else

MessageBox.Show("Find it")

End If

End Sub

Regars.

Gavin






Re: Windows Forms Data Controls and Databinding find a string in datagridview

Biswajitghosh25

Hi Tim,

Already seen the problem that you mention but not resolving my problem. I tried it just see the example.

The string is removing but the messagebox is showing till the last position. Can you tell me what is the error in my code: here is my code snippets:

Code Snippet

if (strSearch != "")

{

pTable.DefaultView.Sort = "fldID";

foreach (DataGridViewRow Row in grdData.Rows)

{

if (strSearch == Row.Cells[0].Value.ToString())

{

int row = pTable.DefaultView.Find(strSearch);

pTable.Rows.RemoveAt(row);

}

else

{

MessageBox.Show("The ID entered is wrong");

}

}

}

The messageBox is showing till the last row. Can you tell me how can i resolve it.

Thanks,

Biswajit





Re: Windows Forms Data Controls and Databinding find a string in datagridview

Biswajitghosh25

Hi Tim,

Already seen the problem that you mention but not resolving my problem. I tried it just see the example.

The string is removing but the messagebox is showing till the last position. Can you tell me what is the error in my code: here is my code snippets:

Code Snippet

if (strSearch != "")

{

pTable.DefaultView.Sort = "fldID";

foreach (DataGridViewRow Row in grdData.Rows)

{

if (strSearch == Row.Cells[0].Value.ToString())

{

int row = pTable.DefaultView.Find(strSearch);

pTable.Rows.RemoveAt(row);

}

else

{

MessageBox.Show("The ID entered is wrong");

}

}

}

The messageBox is showing till the last row. Can you tell me how can i resolve it.

Thanks,

Biswajit





Re: Windows Forms Data Controls and Databinding find a string in datagridview

damla.yilmazer

I resolve problem like this:

this is code snippet

Code Snippet

string aa = ccompany.SelectedItem.ToString();

bool varmi= false;

for (int k = 0; k < dataGridView4.Rows.Count - 1; k++)

{

if (aa == dataGridView4.Rows[k].Cells[0].Value.ToString())

{

MessageBox.Show("there is this record");

varmi = true;

break;

}

}





Re: Windows Forms Data Controls and Databinding find a string in datagridview

Biswajitghosh25

Hi,

I am being able to find and delete the string from the datagridview by the above code that i posted. But I am not getting the

correct answer at all. My problem is:

I have 500 records in myn ID column and I want to search through this item by InputDialoge.Text. If it matches with the string in the ID column then it will delete the records from the datagridview. But if it does not match with the strings of the ID column. It will prompt for message i.e. "Wrong ID Entered". But in my above code, suppose I entered a value in my InputDialogue that mathches the 5th item if the ID column of the datagridview. But when I am pressing the delete button the message box (Wrong ID Entered") is appearing for 4 times. then its deleting the string from the datagridview.

Can you tell me how can i resolve the probem. Its giving me a lots of problem.

Regards,

Biswajit