DataGridView Datasource Datatable

來源:互聯網
上載者:User

很簡單的一個問題,花了3到4個鐘頭,還是不能確定什麼原因。

 

從文本裡讀出一個列表,每一行作為表的一行,填充到datagridview中去

 

先定義table,然後定義columns,然後定義row,然後一個一個row加進table中去,

最後將datagridview的datasource屬性設定為table,

卻總是在最後一句報錯,提示Null 參考實體類似的資訊。

搞不清什麼原因。

將datagridview1重新new一個,問題還是依舊。

 

最後不知怎麼弄得,反正可以了,代碼如下,以後參考

        public Form1(string _taskpath)
        {
            taskpath = _taskpath;

            InitializeComponent();
            ArrayList taskArray = loadTask();

            DataTable dt = new DataTable();
            dt.Columns.Add("查詢", typeof(String));
            dt.Columns.Add("資料庫", typeof(String));
            dt.Columns.Add("時間", typeof(String));

            foreach (string task in taskArray)
            {

                DataRow dr = dt.NewRow();
                dr[0] = task;
                dr[1] = "";
                dr[2] = "";
                dt.Rows.Add(dr);
              }

            this.dataGridView1.DataSource = dt;
            this.dataGridView1.ReadOnly = true;

        }

 

 

下面部分是Form1.Designer.cs中對form 和datagridview的屬性進行設定的代碼:

            //
            // dataGridView1
            //
            this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridView1.Location = new System.Drawing.Point(34, 50);
            this.dataGridView1.Name = "dataGridView1";
            this.dataGridView1.RowTemplate.Height = 23;
            this.dataGridView1.Size = new System.Drawing.Size(703, 816);
            this.dataGridView1.TabIndex = 2;
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(1107, 920);
            this.Controls.Add(this.dataGridView1);
            this.Controls.Add(this.label1);
            this.Name = "Form1";
            this.ShowInTaskbar = false;
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.