Gridview大小(外观)在不同的搜索中发生变化?

时间:2013-06-03 08:20:37

标签: asp.net database gridview

我有一个显示搜索结果的网格视图。但是在不同的搜索结果中,我看到了这个网格视图的不同大小,并且它看起来并不好看。这是:

enter image description here

enter image description here

如您所见,听觉和行的大小会发生变化。这是gridview的代码:

<asp:GridView ID="searchResults" runat="server" AutoGenerateColumns="False" OnSelectedIndexChanged="searchResults_SelectedIndexChanged"
    BackColor="White" BorderColor="White" BorderStyle="None" BorderWidth="1px" CellPadding="4"
    ForeColor="Black" GridLines="Vertical" style="margin-right: 0px" Width="1018px" 
                Height="192px">
    <RowStyle BackColor="#F7F7DE" />
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:CheckBox ID="checkboxRegister" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="CRN" HeaderText="CRN" />
        <asp:BoundField DataField="CourseCode" HeaderText="Course Code" />
        <asp:BoundField DataField="CourseNumber" HeaderText="Course Number" />
        <asp:BoundField DataField="Credit" HeaderText="Credit" />
        <asp:BoundField DataField="CourseName" HeaderText="Course Name" />
        <asp:BoundField DataField="Capacity" HeaderText="Capacity" />
        <asp:BoundField DataField="InstructorName" HeaderText="Instructor" />
        <asp:BoundField DataField="Level" HeaderText="Level" />
    </Columns>
    <FooterStyle BackColor="#CCCC99" />
    <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
    <SelectedRowStyle BackColor="#9E51CF" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#564D94" Font-Bold="True" ForeColor="White" />
    <AlternatingRowStyle BackColor="White" />
</asp:GridView>

以下是我如何绑定数据:

protected void BClassSearch_Click(object sender, EventArgs e)
{
        SqlConnection con = new SqlConnection();
        con.ConnectionString = Userfunctions.GetConnectionString();
        con.Open();
        string selected = lbCourseListBox.SelectedValue;

       if(selected!="" && Tcoursenumber.Text!="")
       {
           string query="";
           if(MyGlobals.student.getLevel()=="Undergraduate")
              query = "select [CRN],[CourseCode],[CourseNumber],[Credit],[CourseName],[Capacity],[InstructorName],[Level] from CourseTable where CourseCode='" + lbCourseListBox.SelectedValue+"' and CourseNumber = '" + Tcoursenumber.Text+"'" + "' and Level='Undergraduate";
           else
               query="select [CRN],[CourseCode],[CourseNumber],[Credit],[CourseName],[Capacity],[InstructorName],[Level] from CourseTable where CourseCode='" + lbCourseListBox.SelectedValue+"' and CourseNumber = '" + Tcoursenumber.Text+"'";

            SqlDataAdapter adap = new SqlDataAdapter(query, con);

            DataTable tab = new DataTable();

            adap.Fill(tab);

            searchResults.DataSource = tab;
            searchResults.DataBind();
       }
       else if (selected != "" && Tcoursenumber.Text == "")
       {
           string query = "";
           if (MyGlobals.student.getLevel() == "Undergraduate") 
              query = "select [CRN],[CourseCode],[CourseNumber],[Credit],[CourseName],[Capacity],[InstructorName],[Level] from CourseTable where CourseCode='" + lbCourseListBox.SelectedValue+"' and Level='Undergraduate'";
           else
               query = "select [CRN],[CourseCode],[CourseNumber],[Credit],[CourseName],[Capacity],[InstructorName],[Level] from CourseTable where CourseCode='" + lbCourseListBox.SelectedValue + "'";

           SqlDataAdapter adap = new SqlDataAdapter(query, con);

           DataTable tab = new DataTable();

           adap.Fill(tab);

           searchResults.DataSource = tab;
           searchResults.DataBind();
       }
       else if (selected == "" && Tcoursenumber.Text != "")
       {
           string query="";

           if(MyGlobals.student.getLevel()=="Undergraduate")
                query = "select [CRN],[CourseCode],[CourseNumber],[Credit],[CourseName],[Capacity],[InstructorName],[Level] from CourseTable where CourseNumber='" + Tcoursenumber.Text+"' and Level='Undergraduate'";
           else
               query = "select [CRN],[CourseCode],[CourseNumber],[Credit],[CourseName],[Capacity],[InstructorName],[Level] from CourseTable where CourseNumber='" + Tcoursenumber.Text + "'";
           SqlDataAdapter adap = new SqlDataAdapter(query, con);

           DataTable tab = new DataTable();

           adap.Fill(tab);

           searchResults.DataSource = tab;
           searchResults.DataBind();
       }
       else if (selected == "" && Tcoursenumber.Text == "")
       {
           string query = "";

           if(MyGlobals.student.getLevel()=="Undergraduate")
                query = "select [CRN],[CourseCode],[CourseNumber],[Credit],[CourseName],[Capacity],[InstructorName],[Level] from CourseTable where Level='Undergraduate' order by CourseCode";
           else
                query = "select [CRN],[CourseCode],[CourseNumber],[Credit],[CourseName],[Capacity],[InstructorName],[Level] from CourseTable order by CourseCode";

           SqlDataAdapter adap = new SqlDataAdapter(query, con);

           DataTable tab = new DataTable();

           adap.Fill(tab);

           searchResults.DataSource = tab;
           searchResults.DataBind();
       }
}

谁能告诉我如何解决这个问题?我感谢任何帮助。感谢。

1 个答案:

答案 0 :(得分:0)

设置每个项目的宽度和高度,如下所示:

<asp:BoundField DataField="Credit" HeaderText="Credit"  HeaderStyle-Width="100" HeaderStyle-Height="20" ItemStyle-Height="20" ItemStyle-Width="100" />

或者添加1(或2)个css样式并为每个项添加它们:

<asp:BoundField DataField="CourseName" HeaderText="Course Name"  ItemStyle-CssClass="style1" HeaderStyle-CssClass="style" />

如果只需要指定高度(显然),则只能设置高度。