Data GridView
can anyone help me how to search data from gridview?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Hirendra SisodiyaPosted Mar 19, 2010, 3:57 AM
DataGridViewRow DataGridrow = default(DataGridViewRow);
DataTable dt = new DataTable();
for (int j = 0; j <= DataGridView1.Columns.Count - 1; j++) {
string columnName = DataGridView1.Columns(j).HeaderText.ToString();
dt.Columns.Add(columnName, typeof(string));
}
foreach (var DataGridrow in DataGridView1.Rows) {
if ((DataGridrow != null)) {
for (int i = 0; i <= DataGridrow.Cells.Count - 1; i++) {
if ((DataGridrow.Cells(i).Value != null)) {
if (DataGridrow.Cells(i).Value.ToString().Contains("Specified Text")) {
DataRow dtrow = dt.NewRow;
for (int k = 0; k <= dtrow.Table.Columns.Count - 1; k++) {
dtrow.Item(k) = DataGridrow.Cells(k).Value.ToString();
}
dt.Rows.Add(dtrow);
break; // TODO: might not be correct. Was : Exit For
}
}
}
}
}
'thanks
'please mark as answere if you like my answere
Hirendra SisodiyaPosted Mar 19, 2010, 6:39 AM
for (int j = 0; j <= DataGridView1.Columns.Count - 1; j++) {
string columnName = DataGridView1.Columns(j).Name.ToString();
string columnText = DataGridView1.Columns(j).HeaderText.ToString();
tempGridView.Columns.Add(columnName, columnText);
}
foreach (DataGridViewRow row in DataGridView1.Rows) {
DataGridViewRow grdrow = default(DataGridViewRow);
grdrow = row.Clone;
tempGridView.Rows.Add(grdrow);
}
DataGridView1.Rows.Clear();
thanks
please mark "Do you like this answer" check box if you like my answere
kombsh mPosted Mar 19, 2010, 4:14 AM
i got it, i need one more help. how can we get the local copy of gridview?
i am using the following code to copy
DataGridView tempGridView = dataGridView2;
when i clear dataGridView2 like dataGridView2.Rows.Clear() i lose the tempGridView.Rows also;
kombsh mPosted Mar 19, 2010, 3:45 AM
i am working in C#. but i got answer in the way of your code,one more thank u for your help.
Hirendra SisodiyaPosted Mar 19, 2010, 2:45 AM
Hello Kombsh
try this:
Dim DataGridrow As DataGridViewRow
Dim dt As New DataTable
For j As Integer = 0 To DataGridView1.Columns.Count - 1
Dim columnName As String = DataGridView1.Columns(j).HeaderText.ToString()
dt.Columns.Add(columnName, GetType(String))
Next
For Each DataGridrow In DataGridView1.Rows
If Not DataGridrow Is Nothing Then
For i As Integer = 0 To DataGridrow.Cells.Count - 1
If Not DataGridrow.Cells(i).Value Is Nothing Then
If DataGridrow.Cells(i).Value.ToString().Contains("Specified Text") Then
Dim dtrow As DataRow = dt.NewRow
For k As Integer = 0 To dtrow.Table.Columns.Count - 1
dtrow.Item(k) = DataGridrow.Cells(k).Value.ToString()
Next
dt.Rows.Add(dtrow)
Exit For
End If
End If
Next
End If
Next
' you can find your searched rows in table 'dt'
'thanks
'please mark as answere if you like my answere
kombsh mPosted Mar 18, 2010, 6:29 AM
in my application i have the datagridview with set of records and i have the option to search records by "specified text", so
here i need to get the row collection from gridview which are contains the "specified text".
Hirendra SisodiyaPosted Mar 18, 2010, 6:20 AM
there are many ways to search data in datagridview,depending on what you want, so please determines and explain your problem or need.
thanks