从代码隐藏在gridview中显示数据的问题
当我从codebehind绑定gridview时,它显示相同的列2次
ex:列empname,empcity,empid显示2次。
那我该怎么办呢?
以下是我的代码:
protected void Page_Load(object sender,EventArgs e)
{
if(!IsPostBack)
{
绑定();
}
}
protected void绑定()
{
string con = ConfigurationManager.ConnectionStrings [empConnectionString]。ConnectionString;
SqlConnection conn = new SqlConnection(con);
conn.Open();
SqlCommand chk = new SqlCommand(Select * from emp,conn);
SqlDataAdapter da = new SqlDataAdapter(chk);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
------------------------- -------------------------------------------------- ----
这是我的设计代码:
Hi,
When i am binding gridview from codebehind it is displaying same columns 2 times
for ex: column empname,empcity,empid is shown 2 times.
So how should i go at it?
Below is my code:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind();
}
}
protected void Bind()
{
string con = ConfigurationManager.ConnectionStrings["empConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(con);
conn.Open();
SqlCommand chk = new SqlCommand("Select * from emp", conn);
SqlDataAdapter da = new SqlDataAdapter(chk);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
-------------------------------------------------------------------------------
here is my design code:
<asp:GridView ID="GridView1" runat="server" BackColor="#33CC33"
AlternatingRowStyle-BackColor="#FFFF66" Height="317px" Width="364px"
style="margin-left: 391px; margin-top: 82px">
<AlternatingRowStyle BackColor="#FFFF66"></AlternatingRowStyle>
<Columns>
<%--This is first column--%>
<asp:TemplateField>
<HeaderTemplate>
EMP_ID
</HeaderTemplate>
<ItemTemplate>
<asp:Label runat="server" Text='<%#Eval("empid") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Bind("empid") %>'></asp:Label>
</EditItemTemplate>
<FooterTemplate>
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
</FooterTemplate>
</asp:TemplateField>
<%--This is second column--%>
<asp:TemplateField>
<HeaderTemplate>
EMP_NAME
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%#Eval("empname") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%#Bind("empname") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<%--This is third column--%>
<asp:TemplateField>
<HeaderTemplate>
EMP_CITY
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%#Eval("empcity") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%#Bind("empcity") %>'>
<asp:ListItem>Mumbai</asp:ListItem>
<asp:ListItem>Delhi</asp:ListItem>
<asp:ListItem>Bangalore</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="DropDownList2" runat="server">
<asp:ListItem>Mumbai</asp:ListItem>
<asp:ListItem>Delhi</asp:ListItem>
<asp:ListItem>Bangalore</asp:ListItem>
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
将AutoGenerateColumns设置为false
set AutoGenerateColumns as false
<asp:GridView ID="GridView1" AutoGenerateColumns="False" ...