DataTable output to string with filter
-
What I am trying to accomplish is that I am making the datatable to interpret large data. I only wanted to print out the header, then the data. Also only to show "Variable" and "Value" columns. I'm not sure how to weed out the "Hex Value" column. Also to list the rows that are (Hidden = false). I'm not sure how to accomplish this. On the bottom code (with variable "res" does show all of the rows with all of the columns, but no header... :sigh:
DataTable dataTable = new DataTable();
dataTable.Columns.Add("Variable", typeof(string));
dataTable.Columns.Add("Hex Value", typeof(Int64));
dataTable.Columns.Add("Value", typeof(string));
dataTable.Columns.Add("Hidden", typeof(bool));dataTable.Rows.Add("Ronald", 0x0, "Value = 0", false);
dataTable.Rows.Add("Ronald", 0x1, "Value = 1", false);
dataTable.Rows.Add("Ronald", 0x2, "Value = 2", true);
dataTable.Rows.Add("Ronald", 0x4, "Value = 4", true);
dataTable.Rows.Add("Ronald", 0x8, "Value = 8", false);
dataTable.Rows.Add("Ronald", 0x16, "Value = 16", true);
dataTable.Rows.Add("Ronald", 0x32, "Value = 32", false);
dataTable.Rows.Add("Ronald", 0x64, "Value = 64", false);
dataTable.Rows.Add("Ronald", 0x128, "Value = 128", false);
dataTable.Rows.Add("Ronald", 0xFF, "Value = 255", true);try
{
DataRow selectedRow = dataTable.Select("").FirstOrDefault(x => (Int64)x["Hex Value"] == Convert.ToInt64(tb_Input.Text, 16));
lbl_Result.Text = selectedRow["Value"].ToString();
}
catch
{
lbl_Result.Text = "Unknown";
}string res = String.Join(Environment.NewLine, dataTable.Rows.OfType().Select(x => String.Join(" ; ", x.ItemArray)));
MessageBox.Show(res);
-
What I am trying to accomplish is that I am making the datatable to interpret large data. I only wanted to print out the header, then the data. Also only to show "Variable" and "Value" columns. I'm not sure how to weed out the "Hex Value" column. Also to list the rows that are (Hidden = false). I'm not sure how to accomplish this. On the bottom code (with variable "res" does show all of the rows with all of the columns, but no header... :sigh:
DataTable dataTable = new DataTable();
dataTable.Columns.Add("Variable", typeof(string));
dataTable.Columns.Add("Hex Value", typeof(Int64));
dataTable.Columns.Add("Value", typeof(string));
dataTable.Columns.Add("Hidden", typeof(bool));dataTable.Rows.Add("Ronald", 0x0, "Value = 0", false);
dataTable.Rows.Add("Ronald", 0x1, "Value = 1", false);
dataTable.Rows.Add("Ronald", 0x2, "Value = 2", true);
dataTable.Rows.Add("Ronald", 0x4, "Value = 4", true);
dataTable.Rows.Add("Ronald", 0x8, "Value = 8", false);
dataTable.Rows.Add("Ronald", 0x16, "Value = 16", true);
dataTable.Rows.Add("Ronald", 0x32, "Value = 32", false);
dataTable.Rows.Add("Ronald", 0x64, "Value = 64", false);
dataTable.Rows.Add("Ronald", 0x128, "Value = 128", false);
dataTable.Rows.Add("Ronald", 0xFF, "Value = 255", true);try
{
DataRow selectedRow = dataTable.Select("").FirstOrDefault(x => (Int64)x["Hex Value"] == Convert.ToInt64(tb_Input.Text, 16));
lbl_Result.Text = selectedRow["Value"].ToString();
}
catch
{
lbl_Result.Text = "Unknown";
}string res = String.Join(Environment.NewLine, dataTable.Rows.OfType().Select(x => String.Join(" ; ", x.ItemArray)));
MessageBox.Show(res);
If dataTable is a System.Data.DataTable, then look into the DefaultView property.
-
If dataTable is a System.Data.DataTable, then look into the DefaultView property.
-
If dataTable is a System.Data.DataTable, then look into the DefaultView property.
Updated code... Seems to be working... Any suggestion for a better approach?
DataTable dataTable = new DataTable();
dataTable.Columns.Add("Variable", typeof(string));
dataTable.Columns.Add("Hex Value", typeof(Int64));
dataTable.Columns.Add("Value", typeof(string));
dataTable.Columns.Add("Hide", typeof(bool));dataTable.Rows.Add("Ronald", 0x0, "Value = 0", false);
dataTable.Rows.Add("Ronald", 0x1, "Value = 1", true);
dataTable.Rows.Add("Ronald", 0x2, "Value = 2", false);
dataTable.Rows.Add("Ronald", 0x4, "Value = 4", true);
dataTable.Rows.Add("Ronald", 0x8, "Value = 8", false);
dataTable.Rows.Add("Ronald", 0x16, "Value = 16", false);
dataTable.Rows.Add("Ronald", 0x32, "Value = 32", false);
dataTable.Rows.Add("Ronald", 0x64, "Value = 64", true);
dataTable.Rows.Add("Ronald", 0x128, "Value = 128", false);
dataTable.Rows.Add("Ronald", 0xFF, "Value = 255", true);try
{
DataRow selectedRow = dataTable.Select("").FirstOrDefault(x => (Int64)x["Hex Value"] == Convert.ToInt64(tb_Input.Text, 16));
lbl_Result.Text = selectedRow["Value"].ToString();
}
catch
{
lbl_Result.Text = "Unknown";
}DataRow[] drArrRow = dataTable.Select("Hide = False");
DataTable filteredDataTable = new DataTable();filteredDataTable.Columns.Add("Variable", typeof(string));
filteredDataTable.Columns.Add("Value", typeof(string));foreach (DataRow dr in drArrRow)
{
filteredDataTable.Rows.Add(dr["Variable"], dr["Value"]);
}string res = String.Join(Environment.NewLine, filteredDataTable.Rows.OfType().Select(x => String.Join(" ; ", x.ItemArray)));
MessageBox.Show(res);
-
Updated code... Seems to be working... Any suggestion for a better approach?
DataTable dataTable = new DataTable();
dataTable.Columns.Add("Variable", typeof(string));
dataTable.Columns.Add("Hex Value", typeof(Int64));
dataTable.Columns.Add("Value", typeof(string));
dataTable.Columns.Add("Hide", typeof(bool));dataTable.Rows.Add("Ronald", 0x0, "Value = 0", false);
dataTable.Rows.Add("Ronald", 0x1, "Value = 1", true);
dataTable.Rows.Add("Ronald", 0x2, "Value = 2", false);
dataTable.Rows.Add("Ronald", 0x4, "Value = 4", true);
dataTable.Rows.Add("Ronald", 0x8, "Value = 8", false);
dataTable.Rows.Add("Ronald", 0x16, "Value = 16", false);
dataTable.Rows.Add("Ronald", 0x32, "Value = 32", false);
dataTable.Rows.Add("Ronald", 0x64, "Value = 64", true);
dataTable.Rows.Add("Ronald", 0x128, "Value = 128", false);
dataTable.Rows.Add("Ronald", 0xFF, "Value = 255", true);try
{
DataRow selectedRow = dataTable.Select("").FirstOrDefault(x => (Int64)x["Hex Value"] == Convert.ToInt64(tb_Input.Text, 16));
lbl_Result.Text = selectedRow["Value"].ToString();
}
catch
{
lbl_Result.Text = "Unknown";
}DataRow[] drArrRow = dataTable.Select("Hide = False");
DataTable filteredDataTable = new DataTable();filteredDataTable.Columns.Add("Variable", typeof(string));
filteredDataTable.Columns.Add("Value", typeof(string));foreach (DataRow dr in drArrRow)
{
filteredDataTable.Rows.Add(dr["Variable"], dr["Value"]);
}string res = String.Join(Environment.NewLine, filteredDataTable.Rows.OfType().Select(x => String.Join(" ; ", x.ItemArray)));
MessageBox.Show(res);
Is the MessageBox the "final" way of displaying the values? Or is it just some temporary measure and should be replaced by something "nicer" eventually?
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
-
Is the MessageBox the "final" way of displaying the values? Or is it just some temporary measure and should be replaced by something "nicer" eventually?
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
-
What I am trying to accomplish is that I am making the datatable to interpret large data. I only wanted to print out the header, then the data. Also only to show "Variable" and "Value" columns. I'm not sure how to weed out the "Hex Value" column. Also to list the rows that are (Hidden = false). I'm not sure how to accomplish this. On the bottom code (with variable "res" does show all of the rows with all of the columns, but no header... :sigh:
DataTable dataTable = new DataTable();
dataTable.Columns.Add("Variable", typeof(string));
dataTable.Columns.Add("Hex Value", typeof(Int64));
dataTable.Columns.Add("Value", typeof(string));
dataTable.Columns.Add("Hidden", typeof(bool));dataTable.Rows.Add("Ronald", 0x0, "Value = 0", false);
dataTable.Rows.Add("Ronald", 0x1, "Value = 1", false);
dataTable.Rows.Add("Ronald", 0x2, "Value = 2", true);
dataTable.Rows.Add("Ronald", 0x4, "Value = 4", true);
dataTable.Rows.Add("Ronald", 0x8, "Value = 8", false);
dataTable.Rows.Add("Ronald", 0x16, "Value = 16", true);
dataTable.Rows.Add("Ronald", 0x32, "Value = 32", false);
dataTable.Rows.Add("Ronald", 0x64, "Value = 64", false);
dataTable.Rows.Add("Ronald", 0x128, "Value = 128", false);
dataTable.Rows.Add("Ronald", 0xFF, "Value = 255", true);try
{
DataRow selectedRow = dataTable.Select("").FirstOrDefault(x => (Int64)x["Hex Value"] == Convert.ToInt64(tb_Input.Text, 16));
lbl_Result.Text = selectedRow["Value"].ToString();
}
catch
{
lbl_Result.Text = "Unknown";
}string res = String.Join(Environment.NewLine, dataTable.Rows.OfType().Select(x => String.Join(" ; ", x.ItemArray)));
MessageBox.Show(res);
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(..).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 be
-
I plan to write the "res" value into text file (also Excel file.) I used this example as to see the quick result of the res value output through MessageBox before I start writing to the file.
I updated my answer, please take another look if you've seen an earlier version already.
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
-
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(..).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 be
-
Thanks! I'm learning more depth on the use of DataTable... I do appreciate some help with this. You've answered my questions.
You're welcome, glad I could help :)
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson