Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to create a grid dynamically as per the user requirements.

EG. I have one textbox and if the user enters 4 it should be a 4 column in grid.


I need help urgently,

Thanks in Advance......
Posted
Updated 16-Jan-12 21:30pm
v4
Comments
Dalek Dave 16-Jan-12 4:21am    
Edited for Grammar, Syntax and Readability.
Anuja Pawar Indore 17-Jan-12 3:30am    
Urgent word is not required

This[^] Archive item may help.
 
Share this answer
 
for gridview with dynamic column you need to set some property to your grid like this...

AutoGenerateColumns="true"

so it will genrate the number of column as per your data, means if your datatable contains 4 column data then the grid will show 4 column, or if it will contains 2 column data then it will display 2 columns....

like if your data table like this...

Table1
STUDENTNAME,ROLNO,CITY,DEPARTMENT

then the grid will contains this column..

or if the data will be like this..

STUDENTNAME,ROLNO

then it will show only 2 column in grid...


so for this you need to fire the select query like this. or provide datasource to grid with this criteria....

Grid Design like this...

ASP.NET
<asp:gridview id="GridView1" runat="server" autogeneratecolumns="true" xmlns:asp="#unknown">
</asp:gridview>
 
Share this answer
 
So you have to create a ADO.NET Table means DATA TABLE with the number of column user enter...
and add one or needed blank row to that table and give that to gridviews data source...

C#
DataTable dt = new DataTable();
for (int i = 0; i < 4; i++)
{
    string columnName = "COLUMN" + i.ToString();
    dt.Columns.Add(columnName);
}
DataRow dr = dt.NewRow();
dt.Rows.Add(dr);
dt.AcceptChanges();
GridView1.DataSource = dt;
GridView1.DataBind();


here i give 4 statically but you can give it user defined with use of variables....
 
Share this answer
 
Comments
Tejas Vaishnav 17-Jan-12 1:24am    
if my answer solved your question then mark it as your solution so other will use it for solving their problem..
thx for ur reply but actuallt i want to create blank column dynamically as per the user enter number of value in textbox
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900