Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. DataTable output to string with filter

DataTable output to string with filter

Scheduled Pinned Locked Moved C#
tutorialworkspace
10 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Blubbo
    wrote on last edited by
    #1

    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);

    P S 2 Replies Last reply
    0
    • B Blubbo

      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);

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      If dataTable is a System.Data.DataTable, then look into the DefaultView property.

      B 2 Replies Last reply
      0
      • P PIEBALDconsult

        If dataTable is a System.Data.DataTable, then look into the DefaultView property.

        B Offline
        B Offline
        Blubbo
        wrote on last edited by
        #3

        I've got it... Now one question hasn't been answered. How do I generate output showing only the "Variable" and "Value" columns?

        1 Reply Last reply
        0
        • P PIEBALDconsult

          If dataTable is a System.Data.DataTable, then look into the DefaultView property.

          B Offline
          B Offline
          Blubbo
          wrote on last edited by
          #4

          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);

          S 1 Reply Last reply
          0
          • B Blubbo

            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);

            S Offline
            S Offline
            Sascha Lefevre
            wrote on last edited by
            #5

            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

            B 1 Reply Last reply
            0
            • S Sascha Lefevre

              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

              B Offline
              B Offline
              Blubbo
              wrote on last edited by
              #6

              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.

              S 1 Reply Last reply
              0
              • B Blubbo

                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);

                S Offline
                S Offline
                Sascha Lefevre
                wrote on last edited by
                #7

                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

                B 1 Reply Last reply
                0
                • B Blubbo

                  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.

                  S Offline
                  S Offline
                  Sascha Lefevre
                  wrote on last edited by
                  #8

                  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

                  1 Reply Last reply
                  0
                  • S Sascha Lefevre

                    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

                    B Offline
                    B Offline
                    Blubbo
                    wrote on last edited by
                    #9

                    Thanks! I'm learning more depth on the use of DataTable... I do appreciate some help with this. You've answered my questions.

                    S 1 Reply Last reply
                    0
                    • B Blubbo

                      Thanks! I'm learning more depth on the use of DataTable... I do appreciate some help with this. You've answered my questions.

                      S Offline
                      S Offline
                      Sascha Lefevre
                      wrote on last edited by
                      #10

                      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

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups