Home > Net >  DataGridView is not showing all columns from table
DataGridView is not showing all columns from table

Time:04-06

I have following table in my PostgreSQL database:

 Table "public.ads"
        Column        |            Type             | Collation | Nullable |              Default
---------------------- ----------------------------- ----------- ---------- -----------------------------------
 idad                 | integer                     |           | not null | nextval('ads_idad_seq'::regclass)
 uidowner             | integer                     |           |          |
 month                | integer                     |           |          |
 year                 | integer                     |           |          |
 mileage              | integer                     |           |          |
 idmake               | integer                     |           |          |
 idmodel              | integer                     |           |          |
 idmotor              | integer                     |           |          |
 idbodytype           | integer                     |           |          |
 description          | text                        |           |          |
 createdat            | timestamp without time zone |           | not null |
 optionalequipmentids | character varying[]         |           |          |
 photos               | character varying[]         |           |          |
 price                | integer                     |           |          |
 generaldata          | jsonb                       |           |          |
 vehicledata          | jsonb                       |           |          |
 engineenvironment    | jsonb                       |           |          |
 conditionmaintenance | jsonb                       |           |          |
 idfueltype           | integer                     |           |          |
Indexes:
    "ads_pk" PRIMARY KEY, btree (idad)
Foreign-key constraints:
    "ads_idbodytype_fkey" FOREIGN KEY (idbodytype) REFERENCES bodytype(idbodytype) ON UPDATE CASCADE
    "ads_idfueltype_fkey" FOREIGN KEY (idfueltype) REFERENCES fueltype(idfueltype) ON UPDATE CASCADE
    "ads_idmake_fkey" FOREIGN KEY (idmake) REFERENCES make(idmake) ON UPDATE CASCADE
    "ads_idmodel_fkey" FOREIGN KEY (idmodel) REFERENCES model(idmodel) ON UPDATE CASCADE
    "ads_idmotor_fkey" FOREIGN KEY (idmotor) REFERENCES motor(idmotor) ON UPDATE CASCADE

I created method for filling DataGridView with data from table above, and here is the code part:

private void FillDataGridAds()
        {
            if (!connected) return;

            string cmdString = string.Empty;

            try
            {
                cmdString = "SELECT * FROM ads";
                NpgsqlCommand command = new NpgsqlCommand(cmdString, conn);
                NpgsqlDataAdapter da = new NpgsqlDataAdapter(command);
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGridAds.DataSource = dt.DefaultView;
            }
            catch (NpgsqlException ex)
            {
                textBox1.AppendText("PostgreSQL exception: "   ex.Message   Environment.NewLine);
            }
            catch (Exception ex)
            {
                textBox1.AppendText("Exception ex: "   ex.Message   Environment.NewLine);
            }
        }

Designer part of code of my dataGridAds

// 
            // dataGridAds
            // 
            this.dataGridAds.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.dataGridAds.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridAds.ContextMenuStrip = this.contextMenuStrip1;
            this.dataGridAds.Location = new System.Drawing.Point(9, 49);
            this.dataGridAds.Name = "dataGridAds";
            this.dataGridAds.RowHeadersWidth = 51;
            this.dataGridAds.RowTemplate.Height = 29;
            this.dataGridAds.Size = new System.Drawing.Size(1326, 549);
            this.dataGridAds.TabIndex = 0;
            this.dataGridAds.CellEndEdit  = new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridAds_CellEndEdit);
            // 

Problem:

For some reason when method for filling dataGridAds is triggered, dataGridAds is displaying all columns except optionalequipmentids and photos, so does anybody know where can be a problem, I was trying to figured out where problem might be, but unsuccessfully, have same issue with another tables in my appplication and want to fix as soon as possible, thank you all in advance.

CodePudding user response:

The columns are both data-type character varying[] I assume that the columns in other tables with the same problem are the same data-type.
You could try using CAST. Does the following script with a nomnative SELECT with CASE for the problem columns work?
If CAST( ... AS VARCHAR(1000)) doesn't work you could also try
CAST( ... AS VARCHAR)

private void FillDataGridAds()
        {
            if (!connected) return;

            string cmdString = string.Empty;

            try
            {
                cmdString = 
"SELECT 
 idad                 ,
 uidowner             ,
 month                ,
 year                 ,
 mileage              ,
 idmake               ,
 idmodel              ,
 idmotor              ,
 idbodytype           ,
 description          ,
 createdat            ,
CAST( optionalequipmentids AS VARCHAR(1000)),
CAST( photos             AS VARCHAR(1000)),
 price                ,
 generaldata          ,
 vehicledata          ,
 engineenvironment    ,
 conditionmaintenance ,
 idfueltype FROM ads";

                NpgsqlCommand command = new NpgsqlCommand(cmdString, conn);
                NpgsqlDataAdapter da = new NpgsqlDataAdapter(command);
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGridAds.DataSource = dt.DefaultView;
            }
            catch (NpgsqlException ex)
            {
                textBox1.AppendText("PostgreSQL exception: "   ex.Message   Environment.NewLine);
            }
            catch (Exception ex)
            {
                textBox1.AppendText("Exception ex: "   ex.Message   Environment.NewLine);
            }
        }

  • Related