DevPinoy.org
A Filipino Developers Community
   

How to create dynamic row in gridview

rated by 0 users
This post has 6 Replies | 2 Followers

Top 100 Contributor
Posts 13
Points 260
d_konqueror Posted: 09-30-2007 7:34 PM

Hi guys,

I have a problem here. How can i create a dynamic row in gridview the row will defend from the user input.

for example the user enter a 4 then the gridview will generate 4 rows.

Please site me an example.

Thanks,

-------------------- Lightning Revenent...
  • | Post Points: 35
Top 100 Contributor
Posts 15
Points 255

 i havent tried that but gridview is supposed to have a datasource... so you have to insert first the record in the datasource then you rebind the gridview to show the new rows. That means this depends on what dataadapter/datasource you will be using.Hmmm..

For example,

if you were using sqldatasource control you should call sqldatasource1.Insert

if you want it to add X rows then create a loop

for i=1 to X 

sqldatasource1.Insert() 

next i 

 After that update the gridview ..

GridView1.Databind

So it really depends on your database and your dataAdapter.. basically  this should work. 

www.numonix.net
  • | Post Points: 35
Top 100 Contributor
Posts 13
Points 95

I don't think you can unless there's a valid data source to populate rows of the grid view. Maybe you could set the values as String.Empty

  • | Post Points: 5
Top 10 Contributor
Posts 365
Points 4,965

sino pa ba may ibang sagot dito? this is exactly what I need. I've seen lots of web/form apps that have this "dynamic row". Is looping the right way to do this?

devpinoy sig

  • | Post Points: 5
Top 10 Contributor
Posts 545
Points 8,915

I'm assuming this is an ASP.NET GridView:

First, set your gridview's AutoGenerateColumns to false (not really necessary)

Next, create your field item and populate it:

BoundField boundField = new BoundField();
boundField.HeaderText = "This is the column header";
boundField.DataField = "BoundName"; // This is the name to look at how to bind it to data (using INamingContainer)
boundField.SortExpression = columnInfo.Name;
// boundField.DataFormatString = "Format"; // If your data needs some formating, put it here
//boundField.ItemStyle.Width = Unit.Percentage(10); // How wide your column is...

// Add the column to your GridView
gridView1.Columns.Add(boundField);

There are many field items you can choose from:

AutoGeneratedField
CheckBoxField
ButtonField
CommandField
HyperLinkField
ImageField
TemplateField (using ITemplate)


Cheers,

-chris

Chris Vega This posting is provided "AS IS" with no warranties, and confers no rights My Weblog|Visit MSDN Community
  • | Post Points: 5
Top 10 Contributor
Posts 545
Points 8,915

Opps sorry, dynamic rows pala....

Just use an extendable collection, like List<YourType> -- populate it on the fly, then bind that data to GridView. Rebind it if necessary.

List<string> names = new List<string>();

names.Add("Puppy");
names.Add("Mickey");
names.Add("Goofey");
names.Add("Foo");

gridView1.DataSource = names;
gridView1.DataBind();

// If you changed the content of names, just rebind it:

gridView1.DataSource = names;
gridView1.DataBind();



Cheers,

-chris

Chris Vega This posting is provided "AS IS" with no warranties, and confers no rights My Weblog|Visit MSDN Community
  • | Post Points: 20
Top 10 Contributor
Posts 2,038
Points 42,030

 Hi,

I created a sample application to show how to solve this problem.

http://devpinoy.org/blogs/keithrull/archive/2008/07/28/how-to-create-dynamic-input-rows-in-a-gridview-with-asp-net.aspx

Thanks,

Keith

devpinoy sig

  • | Post Points: 5
Page 1 of 1 (7 items) | RSS
Copyright DevPinoy 2005-2008