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?
 
sybeCommented:
> 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]
0
 
NavneetSoftware EngineerCommented:
Hi!

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.

Thanks!
0
 
nishant joshiTechnology Development ConsultantCommented:
check code...


regards,,




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

0
 
Sudhakar PulivarthiProject Lead - EngineeringCommented:
Hi,

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

            dataGridView1.Columns.Add(column);
        }
0
 
Minesh ShahSharePoint & all about itAuthor Commented:
ok
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.