Loading Gridview in code

G

GaryDean

I'm trying to load a GridView up with data manually, in code. I'm not using
any datasource. Using this code....

ArrayList myRowArrayList;
GridViewRow myGVR = new GridViewRow(0,0,DataControlRowType.DataRow,
DataControlRowState.Normal);
TableCell myCell = new TableCell();
TextBox myTextbox = new TextBox();
myTextbox.Text = "hello world";
myCell.Controls.Add(myTextbox);
myGVR.Cells.Add(myCell);
myRowArrayList.Add(myGVR);
GridViewRowCollection myCollection = new
GridViewRowCollection(myRowArrayList);
//How do I attach this collection to my GridView?

I can't see how to add the new row I created to the GridView. There is no
GridView.rows.add method. How is a row added to a gridview or how is the
GridViewRowcollectin attached to the Gridview?
 
K

Ken Cox [Microsoft MVP]

Hi Gary,

Perhaps you could explain what your ultimate goal is?

It looks like you're trying to use a gridview to create a table of some sort
without binding to data. Its the data the makes the GridView add rows.

Ken
Microsoft MVP [ASP.NET]
 
W

Walter Wang [MSFT]

Hi Gary,

Thank you for your post.

I believe this post is a further step during your adventure of learning
GridView, :)

Based on my understanding, both DataGrid and GridView are designed to work
with DataBinding scenario.

Can I ask why you want to manually load data into the GridView?

Also, could you please telling me how did you load data into DataGrid as
you described in your another post about DataGrid vs GridView?



Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

GaryDean

Walter, thanks for responding.

I'll try to answer you both... (by posting to each response)

I expected to get a "Why do you want to do that" answer on this issue
because maybe it's impossible to do what I want to do.

I have had repeated needs on several recent jobs to provide the user with a
run time variable matrix of dataentry fields. I know that older
conventional web developers would render out an html table from their code
so the user could enter the data but this has lots of weaknesses.

I thought that a DataGrid or a GridView would be a good tool to use as I
could put it in a DIV where it could be scrolled, I could vary the matrix at
run time (4x6, 1000 x 1000, etc), and add textboxes, regularexpression
validators, etc as desired. I could even create a component using this
technique to make it easier for the developer to use. I figured it must be
possible to manually load data into a GridView since databinding somehow
does it. I see however that the Rows property is read-only.

So, that's WHY I want to do it.

BTW, I never really did this with a DataGrid either. I wrote a General
Purpose Table Maintenance Utility where I got the data in by loading up a
dataset, bound it, and then walked through the grid after the data was
changed. Maybe I'll have to take that approach here too?

Is it impossible to do what I am trying to do? Is there some beter way?
 
G

GaryDean

Ken, thanks for responding.

I'll try to answer you both... (by posting to each response)

I expected to get a "Why do you want to do that" answer on this issue
because maybe it's impossible to do what I want to do.

I have had repeated needs on several recent jobs to provide the user with a
run time variable matrix of dataentry fields. I know that older
conventional web developers would render out an html table from their code
so the user could enter the data but this has lots of weaknesses.

I thought that a DataGrid or a GridView would be a good tool to use as I
could put it in a DIV where it could be scrolled, I could vary the matrix at
run time (4x6, 1000 x 1000, etc), and add textboxes, regularexpression
validators, etc as desired. I could even create a component using this
technique to make it easier for the developer to use. I figured it must be
possible to manually load data into a GridView since databinding somehow
does it. I see however that the Rows property is read-only.

So, that's WHY I want to do it.

BTW, I never really did this with a DataGrid either. I wrote a General
Purpose Table Maintenance Utility where I got the data in by loading up a
dataset, bound it, and then walked through the grid after the data was
changed. Maybe I'll have to take that approach here too?

Is it impossible to do what I am trying to do? Is there some beter way?

--
Regards,
Gary Blakely


--
Regards,
Gary Blakely
Dean Blakely & Associates
www.deanblakely.com
Ken Cox said:
Hi Gary,

Perhaps you could explain what your ultimate goal is?

It looks like you're trying to use a gridview to create a table of some
sort without binding to data. Its the data the makes the GridView add
rows.

Ken
Microsoft MVP [ASP.NET]

GaryDean said:
I'm trying to load a GridView up with data manually, in code. I'm not
using any datasource. Using this code....

ArrayList myRowArrayList;
GridViewRow myGVR = new
GridViewRow(0,0,DataControlRowType.DataRow, DataControlRowState.Normal);
TableCell myCell = new TableCell();
TextBox myTextbox = new TextBox();
myTextbox.Text = "hello world";
myCell.Controls.Add(myTextbox);
myGVR.Cells.Add(myCell);
myRowArrayList.Add(myGVR);
GridViewRowCollection myCollection = new
GridViewRowCollection(myRowArrayList);
//How do I attach this collection to my GridView?

I can't see how to add the new row I created to the GridView. There is
no GridView.rows.add method. How is a row added to a gridview or how is
the GridViewRowcollectin attached to the Gridview?
 
K

Ken Cox [Microsoft MVP]

Hi Gary,

I'm not sure I've really grasped what you're after yet but my impression is
that breaking into the GridView for this task is going to be difficult.

What about using a repeater and adding your dynamic controls to its controls
collection? Or, create one user control that packages all of the individual
controls (textbox and validator) and add as many user controls as you need?


Ken
Microsoft MVP [ASP.NET]

GaryDean said:
Ken, thanks for responding.

I'll try to answer you both... (by posting to each response)

I expected to get a "Why do you want to do that" answer on this issue
because maybe it's impossible to do what I want to do.

I have had repeated needs on several recent jobs to provide the user with
a run time variable matrix of dataentry fields. I know that older
conventional web developers would render out an html table from their code
so the user could enter the data but this has lots of weaknesses.

I thought that a DataGrid or a GridView would be a good tool to use as I
could put it in a DIV where it could be scrolled, I could vary the matrix
at run time (4x6, 1000 x 1000, etc), and add textboxes, regularexpression
validators, etc as desired. I could even create a component using this
technique to make it easier for the developer to use. I figured it must
be possible to manually load data into a GridView since databinding
somehow does it. I see however that the Rows property is read-only.

So, that's WHY I want to do it.

BTW, I never really did this with a DataGrid either. I wrote a General
Purpose Table Maintenance Utility where I got the data in by loading up a
dataset, bound it, and then walked through the grid after the data was
changed. Maybe I'll have to take that approach here too?

Is it impossible to do what I am trying to do? Is there some beter way?

--
Regards,
Gary Blakely


--
Regards,
Gary Blakely
Dean Blakely & Associates
www.deanblakely.com
Ken Cox said:
Hi Gary,

Perhaps you could explain what your ultimate goal is?

It looks like you're trying to use a gridview to create a table of some
sort without binding to data. Its the data the makes the GridView add
rows.

Ken
Microsoft MVP [ASP.NET]

GaryDean said:
I'm trying to load a GridView up with data manually, in code. I'm not
using any datasource. Using this code....

ArrayList myRowArrayList;
GridViewRow myGVR = new
GridViewRow(0,0,DataControlRowType.DataRow, DataControlRowState.Normal);
TableCell myCell = new TableCell();
TextBox myTextbox = new TextBox();
myTextbox.Text = "hello world";
myCell.Controls.Add(myTextbox);
myGVR.Cells.Add(myCell);
myRowArrayList.Add(myGVR);
GridViewRowCollection myCollection = new
GridViewRowCollection(myRowArrayList);
//How do I attach this collection to my GridView?

I can't see how to add the new row I created to the GridView. There is
no GridView.rows.add method. How is a row added to a gridview or how is
the GridViewRowcollectin attached to the Gridview?
 
G

GaryDean

Ken,
I guess this is a dead end. Looks like the DataGrid and the GridView just
don't have the methods to load data by code. It would be cool if they did.
I can load the GridView with Template textboxes, load a dataset in code,
bind to the dataset, then walk through the grid after the user has changed
fields.

Thanks for your help.

--
Regards,
Gary Blakely
Ken Cox said:
Hi Gary,

I'm not sure I've really grasped what you're after yet but my impression
is that breaking into the GridView for this task is going to be difficult.

What about using a repeater and adding your dynamic controls to its
controls collection? Or, create one user control that packages all of the
individual controls (textbox and validator) and add as many user controls
as you need?


Ken
Microsoft MVP [ASP.NET]

GaryDean said:
Ken, thanks for responding.

I'll try to answer you both... (by posting to each response)

I expected to get a "Why do you want to do that" answer on this issue
because maybe it's impossible to do what I want to do.

I have had repeated needs on several recent jobs to provide the user with
a run time variable matrix of dataentry fields. I know that older
conventional web developers would render out an html table from their
code so the user could enter the data but this has lots of weaknesses.

I thought that a DataGrid or a GridView would be a good tool to use as I
could put it in a DIV where it could be scrolled, I could vary the matrix
at run time (4x6, 1000 x 1000, etc), and add textboxes, regularexpression
validators, etc as desired. I could even create a component using this
technique to make it easier for the developer to use. I figured it must
be possible to manually load data into a GridView since databinding
somehow does it. I see however that the Rows property is read-only.

So, that's WHY I want to do it.

BTW, I never really did this with a DataGrid either. I wrote a General
Purpose Table Maintenance Utility where I got the data in by loading up a
dataset, bound it, and then walked through the grid after the data was
changed. Maybe I'll have to take that approach here too?

Is it impossible to do what I am trying to do? Is there some beter way?

--
Regards,
Gary Blakely


--
Regards,
Gary Blakely
Dean Blakely & Associates
www.deanblakely.com
Ken Cox said:
Hi Gary,

Perhaps you could explain what your ultimate goal is?

It looks like you're trying to use a gridview to create a table of some
sort without binding to data. Its the data the makes the GridView add
rows.

Ken
Microsoft MVP [ASP.NET]

I'm trying to load a GridView up with data manually, in code. I'm not
using any datasource. Using this code....

ArrayList myRowArrayList;
GridViewRow myGVR = new
GridViewRow(0,0,DataControlRowType.DataRow,
DataControlRowState.Normal);
TableCell myCell = new TableCell();
TextBox myTextbox = new TextBox();
myTextbox.Text = "hello world";
myCell.Controls.Add(myTextbox);
myGVR.Cells.Add(myCell);
myRowArrayList.Add(myGVR);
GridViewRowCollection myCollection = new
GridViewRowCollection(myRowArrayList);
//How do I attach this collection to my GridView?

I can't see how to add the new row I created to the GridView. There is
no GridView.rows.add method. How is a row added to a gridview or how
is the GridViewRowcollectin attached to the Gridview?
 
W

Walter Wang [MSFT]

Hi Gary,

Thank you for your detailed explanation.

I guess your original intention to use GridView is that it can help you
generate dynamic matrix.

Since the DataGrid and GridView are all designed to be used with
DataBinding, for such scenario, I recommend you to create a custom control
to do that as Ken also suggested.

If you need to discuss on how to create such control, you're welcome to
open a new thread in group
microsoft.public.dotnet.framework.aspnet.buildingcontrols.



Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top