I have an ASP.NET 2.0 web form that includes a GridView control. I then have a method in the code-behind that loads the results of an SQL stored procedure into an SQLDataReader and binds it to the GridView control. In the GridView control, I've set the columns using mostly bound fields. The GridView is displaying the columns correctly, but then it goes ahead and adds extra columns, one for each column returned by the stored procedure. What's going on, and how can I get it to return only those columns that I have specifically designated inside the <Columns> tag?
Here's the ASP.NET code for the GridView:
<asp:GridView ID="gvPlanList" ShowHeader="true" ShowFooter="false" RowStyle-BorderStyle="none
"
HeaderStyle-BackColor="Lig
htBlue"
RowStyle-Font-Size="10px"
HeaderStyle-Font-Size="10p
x"
HeaderStyle-Font-Bold="tru
e"
AllowPaging="false"
runat="server"
>
<Columns>
<asp:ButtonField Text="Add" CommandName="AddPlanToMyQu
ote" HeaderText="My Quote" HeaderStyle-BackColor="Dar
kBlue" HeaderStyle-ForeColor="Whi
te" />
<asp:BoundField DataField="Rate1" DataFormatString="{0:c}" HeaderStyle-BackColor="Lig
htBlue" HeaderText="Employee Only" />
<asp:BoundField DataField="Rate2" DataFormatString="{0:c}" HeaderStyle-BackColor="Lig
htBlue" HeaderText="Employee + Spouse" />
<asp:BoundField DataField="Rate3" DataFormatString="{0:c}" HeaderStyle-BackColor="Lig
htBlue" HeaderText="Employee + Family" />
<asp:BoundField DataField="Rate4" DataFormatString="{0:c}" HeaderStyle-BackColor="Lig
htBlue" HeaderText="Employee +Children" />
<asp:BoundField DataField="Total" DataFormatString="{0:c}" HeaderStyle-BackColor="Lig
htBlue" HeaderText="Total Premium" />
<asp:BoundField DataField="Total" DataFormatString="{0:c}" HeaderStyle-BackColor="Lig
htBlue" HeaderStyle-ForeColor="#99
0000" HeaderText="Employer Contribution" />
<asp:TemplateField ItemStyle-Width="40" HeaderStyle-BackColor="Whi
te" HeaderStyle-ForeColor="Whi
te" HeaderText="" />
<asp:HyperlinkField Text="PlanProfile" DataNavigateUrlFields="pla
ndetails.h
tm?plan={1
}" HeaderStyle-BackColor="Lig
htBlue" HeaderText="Quoted Plan<br />(Click plan to view details)" />
</Columns>
</asp:GridView>
And here's the code-behind method being called:
public void getData()
{
SqlConnection conn = new SqlConnection(_dbConnectio
n);
conn.Open();
try
{
SqlCommand command = new SqlCommand("ITDB.dbo.spSOM
CAgentWebQ
uote_sel",
conn);
command.CommandType = CommandType.StoredProcedur
e;
command.Parameters.Add(new
SqlParameter("@ProposalID"
, SqlDbType.Int));
command.Parameters.Add(new
SqlParameter("@RatepageCat
egoryID", SqlDbType.Int));
command.Parameters.Add(new
SqlParameter("@EECount", SqlDbType.Int));
command.Parameters.Add(new
SqlParameter("@ESCount", SqlDbType.Int));
command.Parameters.Add(new
SqlParameter("@EFCount", SqlDbType.Int));
command.Parameters.Add(new
SqlParameter("@ECCount", SqlDbType.Int));
command.Parameters.Add(new
SqlParameter("@EmployerCon
tributionI
D", SqlDbType.Int));
command.Parameters[0].Valu
e = _proposalID;
command.Parameters[1].Valu
e = _ratepageCategoryID;
command.Parameters[2].Valu
e = _eeCount;
command.Parameters[3].Valu
e = _esCount;
command.Parameters[4].Valu
e = _efCount;
command.Parameters[5].Valu
e = _ecCount;
command.Parameters[6].Valu
e = _employerContributionID;
SqlDataReader dataReader = command.ExecuteReader();
dataReader.Read();
this.gvPlanList.DataSource
= dataReader;
this.gvPlanList.DataBind()
;
}
catch {
}
finally
{
conn.Close();
}
}