Go Back   Velocity Reviews > Newsgroups > ASP Net
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

ASP Net - Loading Gridview in code

 
Thread Tools Search this Thread
Old 07-25-2006, 06:32 PM   #1
Default Loading Gridview in code


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?
--
Regards,
Gary Blakely




GaryDean
  Reply With Quote
Old 07-26-2006, 01:34 AM   #2
Ken Cox [Microsoft MVP]
 
Posts: n/a
Default Re: Loading Gridview in code

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" <> wrote in message
news:...
> 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?
> --
> Regards,
> Gary Blakely
>
>



  Reply With Quote
Old 07-26-2006, 11:16 AM   #3
Walter Wang [MSFT]
 
Posts: n/a
Default Re: Loading Gridview in code

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 (, 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.

  Reply With Quote
Old 07-26-2006, 04:24 PM   #4
GaryDean
 
Posts: n/a
Default Re: Loading Gridview in code

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?

--
Regards,
Gary Blakely
"Walter Wang [MSFT]" <> wrote in message
news:...
> 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 (, 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.
>



  Reply With Quote
Old 07-26-2006, 04:24 PM   #5
GaryDean
 
Posts: n/a
Default Re: Loading Gridview in code

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 [Microsoft MVP]" <> wrote in message
news:%...
> 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" <> wrote in message
> news:...
>> 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?
>> --
>> Regards,
>> Gary Blakely
>>
>>

>
>



  Reply With Quote
Old 07-26-2006, 08:50 PM   #6
Ken Cox [Microsoft MVP]
 
Posts: n/a
Default Re: Loading Gridview in code

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" <> wrote in message
news:OWnY1$...
> 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 [Microsoft MVP]" <> wrote in
> message news:%...
>> 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" <> wrote in message
>> news:...
>>> 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?
>>> --
>>> Regards,
>>> Gary Blakely
>>>
>>>

>>
>>

>
>



  Reply With Quote
Old 07-26-2006, 10:41 PM   #7
GaryDean
 
Posts: n/a
Default Re: Loading Gridview in code

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 [Microsoft MVP]" <> wrote in message
news:...
> 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" <> wrote in message
> news:OWnY1$...
>> 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 [Microsoft MVP]" <> wrote in
>> message news:%...
>>> 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" <> wrote in message
>>> news:...
>>>> 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?
>>>> --
>>>> Regards,
>>>> Gary Blakely
>>>>
>>>>
>>>
>>>

>>
>>

>
>



  Reply With Quote
Old 07-27-2006, 01:37 AM   #8
Walter Wang [MSFT]
 
Posts: n/a
Default Re: Loading Gridview in code

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.buildingc ontrols.



Regards,
Walter Wang (, 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.

  Reply With Quote
Old 09-08-2008, 04:05 PM   #9
rhkulju
Junior Member
 
Join Date: Sep 2008
Posts: 1
Default

"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."

What!!!?!?!?!? Uhhhh... Microsoft didn't hire Anders Hejlsberg to have this same old historical nonsense that was considered Microsoft Development to continue.... and thankfully he put a stop to it.

Of course you can manually populate the GridView..... why wouldn't you???... I've done it.

You need to Create your Cells manually do an call the AddRange method on the Rows you manually create to add the Cells.

You then need to do an AddRange on the DataGridView to add the Rows.

Manually adding things is a basic necessity in all Computer Science applications... thats why you can do it.
rhkulju is offline   Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump