如何在GridView中显示搜索数据?

时间:2013-03-29 08:34:50

标签: asp.net linq gridview

我的GridViews连接到各自的LinqDataSource(LinqDataSourceMale& LinqDataSourceFemale)

<asp:GridView ID="GridViewMale" runat="server" AllowPaging="True" AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4" DataKeyNames="ID" ForeColor="Black" GridLines="Horizontal" PageSize="5" Width="692px" DataSourceID="LinqDataSourceMale">
            <Columns>
                <asp:ImageField DataImageUrlField="Image" NullImageUrl="images/bullet.png" ReadOnly="True">
                </asp:ImageField>
                <asp:BoundField DataField="Age" HeaderText="Age" SortExpression="Age" />
                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                <asp:BoundField DataField="Education" HeaderText="Education" SortExpression="Education" />
                <asp:BoundField DataField="CurrentStatus" HeaderText="CurrentStatus" SortExpression="CurrentStatus" />
                <asp:BoundField DataField="Height" HeaderText="Height" SortExpression="Height" />
                <asp:BoundField DataField="Complexion" HeaderText="Complexion" SortExpression="Complexion" />
                <asp:BoundField DataField="Caste" HeaderText="Caste" SortExpression="Caste" />
                <asp:BoundField DataField="Group" HeaderText="Group" SortExpression="Group" />
            </Columns>
            <FooterStyle BackColor="#CCCC99" ForeColor="Black" />
            <HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
            <SortedAscendingCellStyle BackColor="#F7F7F7" />
            <SortedAscendingHeaderStyle BackColor="#4B4B4B" />
            <SortedDescendingCellStyle BackColor="#E5E5E5" />
            <SortedDescendingHeaderStyle BackColor="#242121" />
        </asp:GridView>
        <asp:LinqDataSource ID="LinqDataSourceMale" runat="server" ContextTypeName="MB.BerouDataContext" EntityTypeName="" TableName="Males">
        </asp:LinqDataSource>
        <asp:GridView ID="GridViewFemale" runat="server" AllowPaging="True" AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4" DataKeyNames="ID" ForeColor="Black" GridLines="Horizontal" PageSize="5" Width="693px" DataSourceID="LinqDataSourceFemale">
            <Columns>
                <asp:ImageField DataImageUrlField="Image" >
                </asp:ImageField>
                <asp:BoundField DataField="Age" HeaderText="Age" SortExpression="Age" />
                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                <asp:BoundField DataField="Education" HeaderText="Education" SortExpression="Education" />
                <asp:BoundField DataField="CurrentStatus" HeaderText="CurrentStatus" SortExpression="CurrentStatus" />
                <asp:BoundField DataField="Height" HeaderText="Height" SortExpression="Height" />
                <asp:BoundField DataField="Complexion" HeaderText="Complexion" SortExpression="Complexion" />
                <asp:BoundField DataField="Caste" HeaderText="Caste" SortExpression="Caste" />
                <asp:BoundField DataField="Group" HeaderText="Group" SortExpression="Group" />
            </Columns>
            <FooterStyle BackColor="#CCCC99" ForeColor="Black" />
            <HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Center" Wrap="True" />
            <SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
            <SortedAscendingCellStyle BackColor="#F7F7F7" />
            <SortedAscendingHeaderStyle BackColor="#4B4B4B" />
            <SortedDescendingCellStyle BackColor="#E5E5E5" />
            <SortedDescendingHeaderStyle BackColor="#242121" />
        </asp:GridView>
        <asp:LinqDataSource ID="LinqDataSourceFemale" runat="server" ContextTypeName="MB.BerouDataContext" EntityTypeName="" TableName="Females">
        </asp:LinqDataSource>

我的SearchClick事件代码为:

 protected void ButtonSearch_Click(object sender, EventArgs e)
    {
        using(BerouDataContext Data = new BerouDataContext())
        {
              if (DropDownListGender.SelectedItem.Text == "Male")
              {

                  int age = Convert.ToInt32(DropDownListAge.Text);
                  string education = DropDownListEducation.Text.ToString();
                  string maritalstatus = DropDownListMaritalStatus.Text.ToString();
                  string caste = DropDownListCaste.Text.ToString();
                  string city = DropDownListCity.ToString();

                  var SearchResultBoys = Data.Males.Where(tan =>
                      (tan.Age == age)
                      && (tan.Education.Contains(education))
                      && (tan.Group.Contains(maritalstatus))
                      && (tan.Caste.Contains(caste)));

                  GridViewMale.DataSourceID = "";
                  GridViewMale.DataSource = SearchResultBoys;
                  GridViewMale.DataBind();
              }
              else if (DropDownListGender.SelectedItem.Text == "Female")
              {
                  int age = Convert.ToInt32(DropDownListAge.Text);
                  string education = DropDownListEducation.Text.ToString();
                  string maritalstatus = DropDownListMaritalStatus.Text.ToString();
                  //var religion = DropDownListReligion.Text.ToString();
                  string caste = DropDownListCaste.Text.ToString();
                  string city = DropDownListCity.ToString();

                  var SearchResultGirls = Data.Females.Where(tan =>
                      (tan.Age == age)
                      && (tan.Education.Contains(education))
                      && (tan.Group.Contains(maritalstatus))
                      && (tan.Caste.Contains(caste)));

                  GridViewFemale.DataSourceID = "";
                  GridViewFemale.DataSource = SearchResultGirls;
                  GridViewFemale.DataBind();



              }
        }
    }

我无法在gridview中显示数据,gridview在searchClickEvent之后没有显示,请帮助。

1 个答案:

答案 0 :(得分:0)

我认为您可能需要重新初始化LinqDataSource数据源而不是gridview

查看this教程和this主题