469,926 Members | 2,489 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,926 developers. It's quick & easy.

c# Gridview Column Count

Hello all.

I am trying to interate thorugh by GridView data to pump each cell of
each row into a stored proc to load to a DB. I seem to be having a
heck of a time getting the Columns.Count to work for my interation and
I am starting to wonder if I am missing something in what that Count
method should return.

The data is loading perfectly into my GridView control.

I started with this:

foreach (GridViewRow row in GridView1.Rows)
{
for (int c = 0; c <= GridView1.Columns.Count; c++)
{
Response.Write(row.Cells[c].Text + "<br />");
}
}

and I get 0 for the column count, so obviously it doesn't work.

So I changed to

foreach (GridViewRow row in GridView1.Rows)
{
for (int c = 0; c < 13; c++)
{
Response.Write(row.Cells[c].Text + "<br />");
}
}

and it worked, but I don't want to asume less than 13 columns.

I put a couple Response.Writes right after my GridView load to make
sure I wasn't doing some postback in the process and I get

7 - Which is correct
0 - Which is not

I have this as the code for the above:

GridView1.DataSource = dsDatasetName;
GridView1.DataBind();

Response.Write("<br/ >" + GridView1.Columns.Count);
Response.Write("<br/ >" + GridView1.Rows.Count);

I have not done anything special to the HTML side of things:

<asp:GridView ID="GridView1" runat="server"></asp:GridView>

Thoughts?

Aug 16 '06 #1
1 13255
Hello all,

Found the answer for myself and thought I would share.

Basically I figured out that the .Columns property is always empty when
autogenerated. I need them to be auto generated since I didn't want to
explicitly name the columns in my upload of the Excel Spread sheet. So
I decided to just get a count before the post back, store it Viewstate
and retrieve it later.

Thanks,
Greg

hartbypass wrote:
Hello all.

I am trying to interate thorugh by GridView data to pump each cell of
each row into a stored proc to load to a DB. I seem to be having a
heck of a time getting the Columns.Count to work for my interation and
I am starting to wonder if I am missing something in what that Count
method should return.

The data is loading perfectly into my GridView control.

I started with this:

foreach (GridViewRow row in GridView1.Rows)
{
for (int c = 0; c <= GridView1.Columns.Count; c++)
{
Response.Write(row.Cells[c].Text + "<br />");
}
}

and I get 0 for the column count, so obviously it doesn't work.

So I changed to

foreach (GridViewRow row in GridView1.Rows)
{
for (int c = 0; c < 13; c++)
{
Response.Write(row.Cells[c].Text + "<br />");
}
}

and it worked, but I don't want to asume less than 13 columns.

I put a couple Response.Writes right after my GridView load to make
sure I wasn't doing some postback in the process and I get

7 - Which is correct
0 - Which is not

I have this as the code for the above:

GridView1.DataSource = dsDatasetName;
GridView1.DataBind();

Response.Write("<br/ >" + GridView1.Columns.Count);
Response.Write("<br/ >" + GridView1.Rows.Count);

I have not done anything special to the HTML side of things:

<asp:GridView ID="GridView1" runat="server"></asp:GridView>

Thoughts?
Aug 18 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by rufpirat | last post: by
5 posts views Thread by Steve Bugden | last post: by
10 posts views Thread by =?Utf-8?B?bWljaGFlbCBzb3JlbnM=?= | last post: by
nitindel
1 post views Thread by nitindel | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.