c# Gridview Column Count

H

hartbypass

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?
 
H

hartbypass

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
 

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