2017-09-13 4 views
0

SQL 데이터베이스의 특정 데이터를 표시하는 ASP.NET 웹 응용 프로그램 만들기. DropDownList에서 선택한 데이터에 대해 각각 특정 3 개의 데이터 소스를 만들었습니다.GridView에 바인딩 된 C# ASP.NET 응용 프로그램 콤보 상자 표시 문제

C#을 측면 코딩 내 DropDownList로는 다음과 같습니다은 ASP.NET 사이드에

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 
     { 
      string SourceName = DropDownList1.SelectedValue; 
      GridView1.DataSourceID = SourceName; 
      GridView1.DataBind(); 
     } 

DropDownList로 코딩 :

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"> 
      <asp:ListItem Value="DataSource_Gerneral">General Overview</asp:ListItem> 
      <asp:ListItem Value="DataSource_Portfolio">Portfolio</asp:ListItem> 
      <asp:ListItem Value="DataSource_ErrorLog">Error Log</asp:ListItem> 
     </asp:DropDownList> 
그리드보기는 ASP.NET 사이드에 코딩

이 다음에 같이

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BorderStyle="Inset" CellPadding="4" DataKeyNames="Call_Sign" DataSourceID= "DataSource_Gerneral" Font-Size="Smaller" ForeColor="#333333" GridLines="None" Height="16px" Width="155px" AllowPaging="True" AllowSorting="True" ClientIDMode="AutoID" HorizontalAlign="Justify" PageSize="100" OnSelectedIndexChanged="GridView1_SelectedIndexChanged1"> 
      <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> 



      <Columns> 
       <asp:BoundField DataField="Call_Sign" HeaderText="Call_Sign" ReadOnly="True" SortExpression="Call_Sign" /> 
       <asp:BoundField DataField="Current_Price" HeaderText="Current_Price" SortExpression="Current_Price" /> 
       <asp:BoundField DataField="Stock_Market" HeaderText="Stock_Market" SortExpression="Stock_Market" /> 
      </Columns> 



      <EditRowStyle BackColor="#999999" /> 
      <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 
      <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 
      <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /> 
      <RowStyle BackColor="#F7F6F3" ForeColor="#333333" /> 
      <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /> 
      <SortedAscendingCellStyle BackColor="#E9E7E2" /> 
      <SortedAscendingHeaderStyle BackColor="#506C8C" /> 
      <SortedDescendingCellStyle BackColor="#FFFDF8" /> 
      <SortedDescendingHeaderStyle BackColor="#6F8DAE" /> 
     </asp:GridView> 

나는 실제로 데이터 소스 ID를 변경했는지 확인하기 위해 드롭 다운 목록을 테스트했습니다. 제대로 작동했습니다. 그러나 선택한보기가 드롭 다운 목록으로 선택되어 있으면 gridview는 그대로 유지됩니다. 나는 새로운 데이터를 표시하는 방법을 찾고있다. 나는 뭔가를 놓치고있다! 런타임에서 gridview를 변경하는 것은 단순한 코드 라인 일뿐입니다.

답변

0

나는 거대한 게임 체인저를 간과했다. 나는 바른 장소를 바꾸지 않고 있었다. 모든 정보가 동일한 데이터베이스에서 왔기 때문입니다. command.CommandText 문을 변경하려면 if 문을 3 개 작성해야했습니다. 예.

If (DataName == "DataSource_General) 
{ 
    command.CommandText = " ... Select..."; 
} 

If (DataName == "DataSource_Portfolio") 
{ 
    command.CommandText = " ... Select ..."; 
} 

이렇게하면 데이터가 올바르게 표시됩니다. 다음과 같이 명령 텍스트에 DropDownList ItemValue를 구성한 다음 호출 할 수도 있습니다.

String selectStatement = DropDownList.SelectedValue.ToString(); 
command.CommandText = selectStatement; 

이렇게하면 두 번째 방법으로 깨끗한 코드를 얻을 수 있습니다.