0

I'm new to C#. I'm trying to build an WPF application which takes data from an Excel Sheet and insert into an DataTable.

The Window will be having multiple textboxes with Search Button. On clicking the Search Button, the Program should search for a value in column ID that equals with Text in TextBox, then return all the corresponding row values that gets displayed in each specific non-editable TextBox.

Since I don't want an actual DataGrid into the UI, I just created an DataGrid Object and loaded all the Data. After I used the following code from here:

for (int i = 0; i < dataGrid.Items.Count; i++)
{
    DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(i);
    TextBlock cellContent = dataGrid.Columns[0].GetCellContent(row) as TextBlock;
    if (cellContent != null && cellContent.Text.Equals(textBox1.Text))
    {
        object item = dataGrid.Items[i];
        dataGrid.SelectedItem = item;
        dataGrid.ScrollIntoView(item);
        row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
        break;
    }
}

But the above code doesn't work unless I add an DataGrid to XAML and fill it up. Most of the solutions I found are requiring me to add it to the XAML. Thought of posting it as a new question. Kindly help me.

PS: I already built this application in VB.NET (It was not so tough) Now I want to move over to C#.

Dante123
  • 45
  • 7
  • If you don't need the GUI portion at all, use a `DataTable` or `DataSet`. This does NOT require doing anything in XAML. – Zer0 Jun 23 '18 at 07:25
  • Yeah I know that. I have an DataTable indeed but was unable to find an code that could do such task. Can you help me out? – Dante123 Jun 23 '18 at 07:35
  • You can simply iterate `DataTable.Rows` which is of type `DataRow` much like you're already doing. And you can iterate over the columns using `DataRow.ItemArray` which are all the columns in that row. Once you find a match, you already have the row. – Zer0 Jun 23 '18 at 07:39
  • Can you post an code to it? To help me get started. – Dante123 Jun 23 '18 at 07:44

1 Answers1

0

Let's assume you have a DataTable called myData. This will search only the column called "ID" for a match.

DataRow match = null;
foreach (DataRow row in myData.Rows)
{
    object cell = row["ID"];
    if (cell != null && cell.ToString() == textBox1.Text)
    {
        match = row;
        break;
    }
}
if (match != null)
{
    //You have found a row that contains the search text
    //Do whatever you want with it here
}
else
    //No match found

Or you can use LINQ:

DataRow match = myData.AsEnumerable().FirstOrDefault(x => x["ID"] == textBox1.Text);
if (match != null)
    //Here's your row
else
    //No match found
Zer0
  • 7,191
  • 1
  • 20
  • 34
  • Thank you. I shall give it a try. – Dante123 Jun 23 '18 at 08:34
  • It working great. Thank you so much :) But seems like it's searching in the whole DataGrid ( Because of Rows ). Isn't there anyway to search only in the Column "ID" as it can be more faster. – Dante123 Jun 23 '18 at 08:56
  • @Dante123 My answer has been updated to only search the "ID" column – Zer0 Jun 23 '18 at 09:06