Can we create programmatically new rows and Columns in gridview without using datatable at run time?

Hi, I have one requirement in which number of Columns and Column names are changing at run time, this can be achieved by using datatable and also can add new row by using datatable.

But I just want to know that, Is it possible to create new rows and columns in GridView at run time without using Datatable?

Initially, I have just drag and drop gridview in aspx page and now what should I do?

Kindly help me.
LVL 10
Minesh ShahSharePoint & all about itAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Navneet.Net Full Stack DeveloperCommented:

Probably not since gridview needs to be aware of all column at first hand.
So once you deifne n column, you can't make it +1 at runtime. you have to make it n+1 altogther
like binding it again with all the n+1 column.

nishant joshiTechnology Development ConsultantCommented:
check code...


System.Data.DataTable dt = (System.Data.DataTable)GridView1.DataSource;
        dt.Columns.Add("new column");
        dt.Rows.Add("new row");
        GridView1.DataSource = dt;

Open in new window

> number of Columns and Column names are changing at run time

What do you mean? The first rows have a different number of columns as the last rows? Or do you mean that the columns in the grid do not match the columns in the database table?

Because you can easily write a query which creates "ghost" columns

SELECT *, 'ghost' AS ghostcolumn FROM [yourtable]

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Sudhakar PulivarthiProject Lead - EngineeringCommented:

You  can add a new column for the datagridview when it is not binded. As below a new column can be added to the existing 2 columns grid.

 private void button1_Click(object sender, EventArgs e)
            DataGridViewColumn column = new DataGridViewColumn();
            column.Name = "MyColumn"; // New column name
            column.CellTemplate = Column1.CellTemplate; // Set the previous column cell template for the new column, else u can set the cell template object

Minesh ShahSharePoint & all about itAuthor Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.