【问题标题】:I have to change GridView Data on Dropdown Selected Index Change我必须在下拉选择的索引更改上更改 GridView 数据
【发布时间】:2014-05-01 19:59:24
【问题描述】:

我必须在 DropDown Selected Value Change 上更改 Gridview 详细信息。这是我的代码,但它不起作用。它没有在 GridView 中显示数据。当我更改 DropDownList 的索引时,GridView 为空。

index.aspx.cs 代码:

    protected void DropDownListDoctor_SelectedIndexChanged(object sender, EventArgs e)
    {
        string query = "SELECT * FROM Doctor WHERE Doc_Id=" + DropDownListDoctor.SelectedValue + "";
        DataTable dt = new DataTable();
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        conn.Open();
        SqlDataAdapter da = new SqlDataAdapter(query, conn);
        da.Fill(dt);
        GridViewDoctorDetail.DataSource = dt;
    }

index.aspx 代码:

<form id="form1" runat="server">
<div>
<label>Select Doctor Name</label>
<br />
   <asp:DropDownList ID="DropDownListDoctor" runat="server" AutoPostBack="True" 
        DataSourceID="Doctor_DataSource" DataTextField="DocName" 
        DataValueField="Doc_Id" 
        onselectedindexchanged="DropDownListDoctor_SelectedIndexChanged">
    </asp:DropDownList>
    <asp:SqlDataSource ID="Doctor_DataSource" runat="server" 
        ConnectionString="Data Source=IM-82B70624D72D;Initial Catalog=AppointmentScheduler;Persist Security Info=True;User ID=sa;Password=za3452432760za" 
        ProviderName="System.Data.SqlClient" 
        SelectCommand="SELECT [Doc_Id], [DocName] FROM [Doctor]">
    </asp:SqlDataSource>
    <br />
    <label>Doctor Detail</label>
    <br />
    <asp:GridView ID="GridViewDoctorDetail" runat="server" BackColor="White" 
        BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" 
        ForeColor="Black" GridLines="Vertical" Width="339px" 
        AutoGenerateColumns="False">
        <AlternatingRowStyle BackColor="White" />
        <FooterStyle BackColor="#CCCC99" />
        <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
        <RowStyle BackColor="#F7F7DE" />
        <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
        <SortedAscendingCellStyle BackColor="#FBFBF2" />
        <SortedAscendingHeaderStyle BackColor="#848384" />
        <SortedDescendingCellStyle BackColor="#EAEAD3" />
        <SortedDescendingHeaderStyle BackColor="#575357" />
    </asp:GridView>
    <asp:SqlDataSource ID="DoctorDetail_SqlDataSource" runat="server" 
        ConnectionString="Data Source=IM-82B70624D72D;Initial Catalog=AppointmentScheduler;Persist Security Info=True;User ID=sa;Password=za3452432760za" 
        ProviderName="System.Data.SqlClient" 
        SelectCommand="SELECT * FROM [Doctor] WHERE ([Doc_Id] = @Doc_Id)">
        <SelectParameters>
            <asp:ControlParameter ControlID="DropDownListDoctor" DefaultValue="1" 
                Name="Doc_Id" PropertyName="SelectedValue" Type="Int32" />
        </SelectParameters>
    </asp:SqlDataSource>
</div>
</form>

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    你需要添加

    GridViewDoctorDetail.DataBind();
    

    到您的后台代码。当回发发生时,gridview 会丢失其先前的内容,并且没有任何数据绑定到它。

    protected void DropDownListDoctor_SelectedIndexChanged(object sender, EventArgs e)
    {
        string query = "SELECT * FROM Doctor WHERE Doc_Id=" + DropDownListDoctor.SelectedValue + "";
        DataTable dt = new DataTable();
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        conn.Open();
        SqlDataAdapter da = new SqlDataAdapter(query, conn);
        da.Fill(dt);
        GridViewDoctorDetail.DataSource = dt;
        GridViewDoctorDetail.DataBind();
    }
    

    【讨论】:

    • 您确定数据是从 SQL 查询返回的吗?
    • 另外,您有 AutoGenerateColumns="False"。您还需要添加列以显示任何内容。
    • 是的。 AutoGeneratedColumn 为假。
    • 更改 DropDownIndex 时显示此错误:- {DataSource 和 DataSourceID 均在 'GridViewDoctorDetail' 上定义。删除一个定义}
    • 您是否手动添加列而只是没有发布代码?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-12
    • 1970-01-01
    • 2012-05-04
    • 2021-02-10
    • 2016-05-24
    • 2014-06-10
    相关资源
    最近更新 更多