-4

Tying to print out a datatable in C#. Used a directive of "using System.Data;". No errors occurring in the program but it does not print out the datatable. LgeoEPH[k] anf BgeoEPH[k] exist.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.IO;
    using System.Text;

in a subroutine:

        DataTable dtz = new DataTable();

        dtz.Columns.Add("", typeof(string));
        dtz.Columns.Add("Sun", typeof(double));
        dtz.Columns.Add("Moon", typeof(double));
        dtz.Rows.Add("LgeoEPH", LgeoEPH[0], LgeoEPH[1], LgeoEPH[2], LgeoEPH[3], LgeoEPH[4], LgeoEPH[5], LgeoEPH[6], LgeoEPH[7], LgeoEPH[8], LgeoEPH[9], LgeoEPH[10]);
        dtz.Rows.Add("BgeoEPH", BgeoEPH[0], BgeoEPH[1], BgeoEPH[2], BgeoEPH[3], BgeoEPH[4], BgeoEPH[5], BgeoEPH[6], BgeoEPH[7], BgeoEPH[8], BgeoEPH[9], BgeoEPH[10]);
        DataView dvz = new DataView(dtz);
er_jack
  • 114
  • 9
  • 1
    What exactly are you expecting to happen? Your code makes little sense as it is anyway, because you're creating a `DataTable` with 3 columns and then trying to add rows 12 values? How are you expecting 12 to fit into 3? After that, the only thing you do is create a `DataView`. The4re's no code to "print" anything, whatever that actually means in this scenario. What exactly do you expect to happen and how exactly do you expect that code to make that happen? – jmcilhinney Jan 03 '23 at 00:49
  • THere is ony 3 columns:dtz.Columns.Add("", typeof(string)); dtz.Columns.Add("Sun", typeof(double)); dtz.Columns.Add("Moon", typeof(double)); dtz.Rows.Add("LgeoEPH", LgeoEPH[0], LgeoEPH[1]); dtz.Rows.Add("BgeoEPH", BgeoEPH[0], BgeoEPH[1]); DataView dvz = new DataView(dtz); c#datatable – er_jack Jan 03 '23 at 00:52
  • 1
    Why are you posting code in a comment that is different from the code in the question? Don't post long code in comments to begin with and don't post unformatted code in comments at all. Don't post things in comments that should be in the question. Edit your question and post the actual code. That still doesn't help with "printing" though. Again, what does "printing" mean in this context and how exactly do you expect the code you have to accomplish it? Printing is generally sending data to a printer to be transferred to paper but some people just mean "display" when they say "print". – jmcilhinney Jan 03 '23 at 01:02
  • Not displaying the DataTable. It only has 3 columns of data as in the comments. Where am I going wrong? – er_jack Jan 03 '23 at 01:32
  • Displaying where?! If you can't actually explain your problem then no one is going to want to help or be able to. Edit your question and provide the FULL and CLEAR explanation that you should have in the first place. Based on your namespace imports, you appear to be using Web Forms and yet there's no tag to indicate that on the question. If you're trying to display this data in a web page then how about you say that? – jmcilhinney Jan 03 '23 at 01:38

1 Answers1

0

Based on the following using directives:

  • using System.Web;
  • using System.Web.UI;
  • using System.Web.UI.WebControls;

it seems that you may be attempting to display a DataTable on a WebForm.

Prerequisite

If developing on Win 10, ensure that IIS is enabled/installed (Control Panel => View by: Small icons => Programs and Features => Turn Windows features on or off => check "Internet Information Services" => expand "Internet Information Services" => expand "World Wide Web Services" => expand "Application Development Features" => check "ASP.NET 4.8" => click "OK")

Try the following:

VS 2022:

Create a new ASP.NET Web Application (.NET Framework)

  • Open Visual Studio 2022

  • Click Create a new project

    enter image description here

  • For filter, select

    enter image description here

  • Select ASP.NET Web Application (.NET Framework)

    enter image description here

  • Click Next

  • Enter desired project name (ex: WebAppGridViewTest)

  • Select desired framework (ex: .NET Framework 4.8)

  • Click Create

  • On the "Create a new ASP.NET Web Application" form, select Empty.

    enter image description here

    If you're using "http" instead of "https", under "Advance", uncheck "Configure for HTTPS".

    enter image description here

  • Click Create


Add Web Form to project

  • In VS menu, click Project
  • Select Add New Item...
  • On left side, expand "Web". Select Web Form (name: default.aspx)
  • Click Add

Open Solution Explorer

  • In VS menu, click View
  • Select Solution Explorer

default.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="WebAppGridViewTest._default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:Label ID="LabelMsg" runat="server" Text="" style="position:absolute;left:50px; top:60px"></asp:Label>
            </div>
            <div style="position:absolute;left:50px; top:100px">
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"  DataKeyNames="ID" GridLines="Both">
                    <Columns>
                        <asp:BoundField DataField="ID" HeaderText="ID" ItemStyle-HorizontalAlign="Center" Visible="True" />
                        <asp:BoundField DataField="FirstName" HeaderText="First Name" ItemStyle-HorizontalAlign="Center" />
                        <asp:BoundField DataField="LastName" HeaderText="Last Name" ItemStyle-HorizontalAlign="Center" />
                    </Columns>
                </asp:GridView>
            </div>
        </form>
    </body>
</html>

In Solution Explorer, expand "default.aspx" and select "default.aspx.cs".

default.aspx.cs

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace WebAppGridViewTest
{
    public partial class _default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("ID", typeof(int)));
            dt.Columns.Add(new DataColumn("FirstName", typeof(string)));
            dt.Columns.Add(new DataColumn("LastName", typeof(string)));

            //add test data
            AddRow(dt, 1, "John", "Doe");
            AddRow(dt, 2, "Tom", "Smith");
            AddRow(dt, 3, "Susan", "Archer");

            GridView1.DataSource= dt;
            GridView1.DataBind();
        }

        private void AddRow(DataTable dt, int id, string firstName, string lastName)
        {
            DataRow row = dt.NewRow();
            row["ID"] = id;
            row["FirstName"] = firstName;
            row["LastName"] = lastName;

            //add
            dt.Rows.Add(row);
        }
    }
}

Result:

enter image description here


Resources

Tu deschizi eu inchid
  • 4,117
  • 3
  • 13
  • 24