|
|
|
|
Forum
Please
Log In
to post a new message or reply to an existing one. If you are not registered, please
register.
NOTE: Some forums may be read-only if you are not currently subscribed to
our technical support services.
Subject |
Author |
Date |
|
Paddy
|
Dec 30, 2010 - 11:06 PM
|
Hi, Is there any chance of a code sample for being able to search an unbound grid for a value upon a button click? I need to make sure a value added to the grid by use of a textbox isnt present in the grid prior to adding the value. thanks.
|
|
Technical Support
|
Jan 5, 2011 - 9:52 AM
|
Please take a look at the following example:
string searchText = "MyText";
foreach (Row row in unboundGridControl1.Rows)
{
DataRow dataRow = row as DataRow;
if (dataRow != null)
{
foreach (TextCell cell in dataRow.Cells)
{
object o = cell.DataValue;
if (o != null)
{
if (((string)o).Contains(searchText))
{
MessageBox.Show("Found.");
return;
}
}
}
}
}
MessageBox.Show("Not Found.");
|
|
Paddy
|
Jan 5, 2011 - 7:33 PM
|
Thanks for the sample. If I only wanted to check the first column and disregard the others, how would I do that? This is what I have currently. Private Sub cmdSearchComputerType_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSearchComputerType.Click
’Search the grid for the item and then highlight it.
Dim TextToSearch As String = txtSearchComputerType.Text.ToString
For Each MyRow As Row In gridComputerType.Rows
Dim MyDataRow As DataRow = TryCast(MyRow, DataRow)
If MyDataRow IsNot Nothing Then
For Each MyCell As TextCell In MyDataRow.Cells
Dim objDataValue As Object = MyCell.DataValue
If objDataValue IsNot Nothing Then
If DirectCast(objDataValue, String).Contains(TextToSearch) Then
’Text is found. Highlight the item.
Return
’Focus the search field.
txtSearchComputerType.Focus()
Else ’Not found.
If MessageBox.Show("The search criteria of " & TextToSearch & " could not be found. " & _
"You might want to consider adding " & TextToSearch & " as an option. Click Yes to add the " & _
"option and No to not add the option.", _
TextToSearch & " Not Found", _
MessageBoxButtons.YesNo, _
MessageBoxIcon.Information, _
MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.Yes Then
’Yes was clicked on by the end user. Auto insert the value.
txtComputerType.Text = txtSearchComputerType.Text
’Focus the add button.
cmdComputerTypeAdd.Focus()
Else ’No was clicked on.
’Clear and focus the search field.
With txtSearchComputerType
.Clear()
.Focus()
End With
End If
End If
End If
Next MyCell
End If
Next MyRow
End Sub
|
|
Technical Support
|
Jan 6, 2011 - 3:34 AM
|
Please replace the loop For Each MyCell As TextCell In MyDataRow.Cells with the line Dim MyCell As Cell = MyDataRow.Cells(YourFirstColumn)
|
|
Paddy
|
Jan 6, 2011 - 8:28 AM
|
I can’t get this to work. I quit with this control! Gonna use Microsoft instead, it’s easier to code with.
|
|