<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false" 
              CellPadding="4" DataKeyNames="sno" 
              ForeColor="#333333" GridLines="None" Width="546px" >
            <AlternatingRowStyle BackColor="White" />
            <Columns>
                <asp:BoundField DataField="sno" HeaderText="学号" ReadOnly="True" 
                    SortExpression="sno" />
                <asp:BoundField DataField="sname" HeaderText="姓名" SortExpression="sname" />
                <asp:BoundField DataField="sex" HeaderText="性别" SortExpression="sex" />
                <asp:BoundField DataField="zzmianmao" HeaderText="政治面貌" 
                    SortExpression="zzmianmao" />
                <asp:BoundField DataField="nation" HeaderText="民族" SortExpression="nation" />
                <asp:BoundField DataField="nplace" HeaderText="籍贯" SortExpression="nplace" />
                <asp:BoundField DataField="cno" HeaderText="班级" SortExpression="cno" />
                <asp:BoundField DataField="hostel" HeaderText="宿舍号" SortExpression="hostel" />
            </Columns>
            <EditRowStyle BackColor="#2461BF" />
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#EFF3FB" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#F5F7FB" />
            <SortedAscendingHeaderStyle BackColor="#6D95E1" />
            <SortedDescendingCellStyle BackColor="#E9EBEF" />
            <SortedDescendingHeaderStyle BackColor="#4870BE" />
        </asp:GridView>
下面是button代码:
     protected void chabanji_Click(object sender, EventArgs e)
    {
        SqlConnection myConnection = new SqlConnection("server=localhost;database=student;uid=sa;pwd=111");
        myConnection.Open();
        string banji2=banji.Text;
        string safeSql = "select sno,sname,sex,zzmianmao,nation,nplace,cno,hostel from Sinformation where cno='" + banji2 + "'";
        SqlDataAdapter myda = new SqlDataAdapter(safeSql,myConnection);
        DataSet ds = new DataSet();
        myda.Fill(ds);
        this.GridView2.DataSource = ds;
        this.GridView2.DataBind();
    }
显示不出来。大神帮帮我啊。新手不太懂。

解决方案 »

  1.   

    没有报错,说明代码没什么问题,数据显示不出来的话,很大可能是SQL语句取出的数据行数是0,所以没有显示。
    protected void chabanji_Click(object sender, EventArgs e)
    {
            SqlConnection myConnection = new SqlConnection("server=localhost;database=student;uid=sa;pwd=111");
            myConnection.Open();
            string banji2=banji.Text.Trim()//取消空格试试
            string safeSql = "select sno,sname,sex,zzmianmao,nation,nplace,cno,hostel from Sinformation where cno='" + banji2 + "'"; //注意这里的班级编号的字段类型,你的这种写法,说明cno在数据库中是字符型的,如果是数字类型的话,请将变量两端的单引号去掉
            SqlDataAdapter myda = new SqlDataAdapter(safeSql,myConnection);
            DataSet ds = new DataSet();
            myda.Fill(ds);
            //这里输出ds中的记录个数
            Response.Write("<script>alert('共有:"+ds.Tables[0].Rows.Count+"条数据!');</script>");

            this.GridView2.DataSource = ds;
            this.GridView2.DataBind();
        }