C# 我必须在下拉选择索引更改时更改GridView数据
我必须在下拉选择值更改时更改Gridview详细信息。这是我的代码,但它不起作用。它没有在GridView中显示数据。当我更改DropDownList的索引时,GridView为空 index.aspx.cs代码:C# 我必须在下拉选择索引更改时更改GridView数据,c#,asp.net,gridview,C#,Asp.net,Gridview,我必须在下拉选择值更改时更改Gridview详细信息。这是我的代码,但它不起作用。它没有在GridView中显示数据。当我更改DropDownList的索引时,GridView为空 index.aspx.cs代码: protected void DropDownListDoctor_SelectedIndexChanged(object sender, EventArgs e) { string query = "SELECT * FROM Doctor WHER
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>
选择医生姓名
医生细节
您需要添加
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为false。在更改DropDownIndex时显示此错误:-{DataSource和DataSourceID都是在“GridViewDoctorDetail”上定义的。删除一个定义}您是否手动添加列,只是没有发布代码?