Click here to Skip to main content
15,892,298 members
Home / Discussions / C#
   

C#

 
QuestionDataTable output to string with filter Pin
Blubbo17-Apr-15 8:58
Blubbo17-Apr-15 8:58 
GeneralRe: DataTable output to string with filter Pin
PIEBALDconsult17-Apr-15 9:06
mvePIEBALDconsult17-Apr-15 9:06 
GeneralRe: DataTable output to string with filter Pin
Blubbo17-Apr-15 9:35
Blubbo17-Apr-15 9:35 
GeneralRe: DataTable output to string with filter Pin
Blubbo17-Apr-15 9:49
Blubbo17-Apr-15 9:49 
GeneralRe: DataTable output to string with filter Pin
Sascha Lefèvre17-Apr-15 9:53
professionalSascha Lefèvre17-Apr-15 9:53 
GeneralRe: DataTable output to string with filter Pin
Blubbo17-Apr-15 9:56
Blubbo17-Apr-15 9:56 
GeneralRe: DataTable output to string with filter Pin
Sascha Lefèvre17-Apr-15 11:52
professionalSascha Lefèvre17-Apr-15 11:52 
AnswerRe: DataTable output to string with filter Pin
Sascha Lefèvre17-Apr-15 10:26
professionalSascha Lefèvre17-Apr-15 10:26 
Suggestions:

dataTable.Select(..) is meant to specify a filter-expression. If you don't specify one, it has basically the same effect as AsEnumerable()[^]. This would be the "cleaner" way in this case. (See code block below.)

Only resort to exception-catching if really neccessary. For the case that's there no matching "Hex Value" for the user-input, there's a way to do it without and you're already half-way there: FirstOrDefault(..) returns a default value if no matching value is found. If you don't use DefaultIfEmpty(..)[^] before FirstOrDefault(..) to specify a custom default-value, then the returned default-value is null*. So instead of catching the exception which will be thrown when accessing the null-valued selectedRow on the next line, just check if selectedRow is null.
* : for reference-types. (0 for value types)

If there can be only one matching value at most, use SingleOrDefault(..) instead of FirstOrDefault(..).
C#
DataRow selectedRow = dataTable.AsEnumerable().SingleOrDefault(x => (Int64)x["Hex Value"] == Convert.ToInt64(tb_Input.Text, 16));
lbl_Result.Text = selectedRow != null ? selectedRow["Value"].ToString() : "Unknown";


If you want to write a "real" Excel-file (and not just a CSV-file) you will probably use some library for that. If that library accepts a DataTable as input, your approach from your reworked code (building a new DataTable with the desired content) is alright. A more generic approach would be the following (not neccessarily much better but I think you'll get some ideas from it):
C#
var filtered = dataTable.AsEnumerable()
                        .Where(row => !(bool)row["Hide"])
                        .Select(row => new { variable = (string)row["Variable"], value = (string)row["Value"] });
// => "filtered" is a collection of an "anonymous type" with the properties "variable" and "value"
// (an "anonymous type" is a type that the compiler built for you)

// build a CSV-"file":
StringBuilder sb = new StringBuilder();
// output headers:
sb.AppendLine("\"Variable\";\"Value\"");
// output rows:
foreach (var row in filtered)
{
   sb.Append('"').Append(row.variable).Append('"');
   sb.Append(';');
   sb.Append('"').Append(row.value).Append('"');
   sb.AppendLine();
}
string output = sb.ToString();


And a more compact version:
C#
var filtered = dataTable.AsEnumerable()
                        .Where(row => !(bool)row["Hide"])
                        .Select(row => String.Concat("\"", row["Variable"], "\";\"", row["Value"], "\""));
// => here "filtered" is just a collection of strings

StringBuilder sb = new StringBuilder();
sb.AppendLine("\"Variable\";\"Value\"");
sb.Append(String.Join(Environment.NewLine, filtered));
string output = sb.ToString();

If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson


modified 17-Apr-15 18:04pm.

GeneralRe: DataTable output to string with filter Pin
Blubbo20-Apr-15 1:52
Blubbo20-Apr-15 1:52 
GeneralRe: DataTable output to string with filter Pin
Sascha Lefèvre20-Apr-15 1:57
professionalSascha Lefèvre20-Apr-15 1:57 
QuestionTime delay without freezing the GUI Pin
Member 1085025317-Apr-15 5:24
Member 1085025317-Apr-15 5:24 
AnswerRe: Time delay without freezing the GUI Pin
OriginalGriff17-Apr-15 5:46
mveOriginalGriff17-Apr-15 5:46 
GeneralRe: Time delay without freezing the GUI Pin
PIEBALDconsult17-Apr-15 6:46
mvePIEBALDconsult17-Apr-15 6:46 
GeneralRe: Time delay without freezing the GUI Pin
Member 1085025317-Apr-15 7:03
Member 1085025317-Apr-15 7:03 
GeneralRe: Time delay without freezing the GUI Pin
Sascha Lefèvre17-Apr-15 7:37
professionalSascha Lefèvre17-Apr-15 7:37 
GeneralRe: Time delay without freezing the GUI Pin
Eddy Vluggen17-Apr-15 8:23
professionalEddy Vluggen17-Apr-15 8:23 
AnswerRe: Time delay without freezing the GUI Pin
Richard MacCutchan17-Apr-15 5:46
mveRichard MacCutchan17-Apr-15 5:46 
QuestionLock client application (controlled) on call for Form Pin
Member 998884817-Apr-15 4:03
Member 998884817-Apr-15 4:03 
AnswerRe: Lock client application (controlled) on call for Form Pin
Dave Kreskowiak17-Apr-15 4:11
mveDave Kreskowiak17-Apr-15 4:11 
GeneralRe: Lock client application (controlled) on call for Form Pin
Member 998884817-Apr-15 4:19
Member 998884817-Apr-15 4:19 
GeneralRe: Lock client application (controlled) on call for Form Pin
Dave Kreskowiak17-Apr-15 4:24
mveDave Kreskowiak17-Apr-15 4:24 
QuestionHow to solve this Error?? Error_2_Cannot implicitly convert type 'int' to 'int[]' Pin
Member 1099471216-Apr-15 22:42
Member 1099471216-Apr-15 22:42 
AnswerRe: How to solve this Error?? Error_2_Cannot implicitly convert type 'int' to 'int[]' Pin
OriginalGriff16-Apr-15 23:16
mveOriginalGriff16-Apr-15 23:16 
GeneralRe: How to solve this Error?? Error_2_Cannot implicitly convert type 'int' to 'int[]' Pin
Member 1099471216-Apr-15 23:29
Member 1099471216-Apr-15 23:29 
GeneralRe: How to solve this Error?? Error_2_Cannot implicitly convert type 'int' to 'int[]' Pin
Pete O'Hanlon16-Apr-15 23:40
mvePete O'Hanlon16-Apr-15 23:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.