Hello,

My question is: Can I add in new column(S) in the gridview if the column is not found in the database?
- E.g. An indicator if a certain lecturer is a ML/Lec (roles).

I have a gridview that is "extracted" out from the database.
But I want to add in a new row which is not in the database but links to the data in the database.
For example, in the database, I know that a certain lecturer is a module leader, staff.
But I want to have an extra column which can indicate in itself (probably a tick/mark) that the following name (prev column) is a module leader or staff; and i am not allowed to add new column in the database.

Any help appreciated.
Need me to further explain my queries, let me know. :)

kumar.Challa commented: add new record using button +0

DataTable class.

DataTable dt=new DataTable();
dt.Columns.Add("Col1",typeof(int));

Hello adatapost,
Ohs. OKies. Thank you. I shall try.

without touching the datasource, you can add your column to your gridview directly.

the project is attached to this post.

Form1.cs :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            dataGridView1.Columns.Add("columnName", "columnHeader");
            dataGridView1.Rows.Add(new object[]{"cell content"});
        }
    }
}

Form1.Designer.cs :

namespace WindowsApplication2
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.dataGridView1 = new System.Windows.Forms.DataGridView();
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
            this.SuspendLayout();
            // 
            // dataGridView1
            // 
            this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridView1.Location = new System.Drawing.Point(0, 0);
            this.dataGridView1.Name = "dataGridView1";
            this.dataGridView1.Size = new System.Drawing.Size(240, 150);
            this.dataGridView1.TabIndex = 0;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Controls.Add(this.dataGridView1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.DataGridView dataGridView1;
    }
}
commented: ++Rep. Cool code. +14

Hello Serkan Sendur,

Thank you for your help.
Although i have download the application, i am not able to run it cuz i am using visual studio 2005 and they are unable to convert it.

Hmmm. I have read the codes, i can understand that you just have to .add it. but for the rest, i cant really understand.

i created it in VS 2005, i dont understand why it didnt open. the rest of the code is from the designer, it is generated code, dont worry about it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.