0
votes

So I've done a lot of searching for this one and can't figure it out. I have a csv file that i'm writing off to a DataTable and populating a combo box from that same datatable. The Idea is to search a user selected value in the data table and return the ID of that selection from the same data table. The problem I'm having is that the selections all have spaces since they are capacity environments. Is there a way to take the string and search the datatable column "Description" and return the column "ID"? here is the code:

internal static void envRequest(string e)
    {
        DataRow[] foundRows;

         foundRows = variables.capEnvTable.Select(e);
        //variables.envID = foundRows[0].ToString();

        Thread.Sleep(200);
        Console.WriteLine(foundRows.ToString());
    }
}

The DataTable is formated as "ID" - "Name" - "Description"

The value of e is the user selected value such as "Buckeye Hosting Zone 2 Aries"

Right now I'm getting a System.Data.SyntaxErrorException: 'Syntax error: Missing operand after 'Hosting' operator.' on the foundRows = variables.capEnvTable.Select(e);

1
1. Thread.Sleep is bad 2. You say return column ID from the column description... how does a result from the datatable look? - EpicKip
It's throwing the exception before i get a return. I have a datagridview added that I'm using for testing to display the DataTable contents and everything is showing there flawlessly. - Josh
you're not getting selected one you are setting it. Is that correct? - EpicKip
Id have to know what variables is too. - EpicKip
Yes the value from the dropdown is being sent to the method as "e" correctly. I'm pretty sure it's an issue with the spaces in the string that's causing the issue. When i look at the inner exception is just says null. variables. is just a class that i'm using to store global variables in - Josh

1 Answers

1
votes

Ok lets say the first column (index 0) is the value you want to search and the 2nd column (index 1) is the value you want.

DataRow dataRow;
dataRow = 
    myDataGridView.Rows.Cast<DataRow>()
    .FirstOrDefault( row => row[0] == "ValueYouWantToSearch" );
var value = dataRow[1];