2017-09-14 1 views
0

로컬 데이터베이스에서 저장된 데이터를 표시하는 웹 페이지를 만듭니다. GridView에서 어떤 열과 순서를 변경하려면 DropDownList Item을 사용하고 싶습니다. DropdownList 또는 Changing Query 문자열에서 선택하면 DataGrid 데이터 소스를 쉽게 변경 할 수 있습니까?C# Asp.Net DropDownList를 사용하여 GridView의 DataSource를 변경합니다.

모든 데이터는 동일한 데이터 소스에서 가져 오지만 다른 열과 다른 순서로 표시됩니다. 예를 들어

:

DropDownList로 Selctions은 (일반 개요, 포트폴리오, 시스템 로그)

일반 개요 선택시 : 의 GridView는 열 ([이름], [위치], [이익]을 표시합니다, [현재 순위]를 선택 포트폴리오에 Tabel [홈페이지])

에서 asending 이름 순서 :

의 GridView는

를 표시합니다

표 [주]에서 이익 차순으로 주문한 [이름], [현재 가격], [이익 가격], [매입 수량], [매매]

시스템 로그 선택시 :

있는 gridview가

열이 표시됩니다 ([시간], [시스템 메시지], [오류 코드가], [다시 시작] 시간이 표 [시스템 로그]에서 asending 발주)

도움이 될 것이다 대단히 감사합니다!! 나는 결과없이 이틀 동안 답을 찾았다. 아이디어 환영합니다 !! 고맙습니다!

답변

0

간단한 예로서 다음은 dataTbale를 선택 변경합니다. 내 목록 항목을 "일반", "이익", "시스템 로그"로 설정합니다. 나는 지금 전화를 시도하지 않고 그리드 데이터의 다른 세트를 표시합니다 목록에서 항목을 선택시 C#을 측면

private void drop1_SelectedIndexChanged(object sender, EventArgs e) 
     { 
      String selI = drop1.SelectedItem.ToString(); 
      String strConnect = ("Data Source = (LocalDB)\\MSSQLLocalDB; AttachDbFilename = C:\\Users\\smith\\Documents\\SqlStockDB.mdf; Integrated Security = True; Connect Timeout = 30"); 
      SqlConnection Connect = new SqlConnection(strConnect); 
      SqlCommand sqlcmd = new SqlCommand(); 
      sqlcmd.Connection = Connect; 
      sqlcmd.CommandType = CommandType.Text; 

      if (selI == "General") 
      { 
       sqlcmd.CommandText = "SELECT [Call Sign] AS Call_Sign, [Current Price] AS Current_Price FROM [Main] ORDER BY [Call Sign]"; 

       SqlDataAdapter adp = new SqlDataAdapter(sqlcmd); 

       DataTable dtRecord = new DataTable(); 
       adp.Fill(dtRecord); 
       dataGridView1.DataSource = dtRecord; 
       dataGridView1.Refresh(); 
      } 
      dataGridView1.ClearSelection(); 

      if (selI == "Profit") 
      { 
       sqlcmd.CommandText = "SELECT [Call Sign] AS Call_Sign, [Current Price] AS Current_Price, [Profit], [Buy], [Sell], [Available Money] AS Available_Money, [Quantity Bought] AS Quantity_Bought FROM [Main] ORDER BY [Profit] DESC, [Quantity Bought] DESC, [Call Sign]"; 

       SqlDataAdapter adp = new SqlDataAdapter(sqlcmd); 

       DataTable dtRecord = new DataTable(); 
       adp.Fill(dtRecord); 
       dataGridView1.DataSource = dtRecord; 
       dataGridView1.Refresh(); 
      } 

      if (selI == "System Logs") 
      { 
       sqlcmd.CommandText = "SELECT [Time], [Module], [Error Code] AS Error_Code, [Restart] FROM [Error] ORDER BY [Time], [Module], [Restart]"; 

       SqlDataAdapter adp = new SqlDataAdapter(sqlcmd); 

       DataTable dtRecord = new DataTable(); 
       adp.Fill(dtRecord); 
       dataGridView1.DataSource = dtRecord; 

       dataGridView1.Refresh(); 
      } 

에이 코드를 작성하고 데이터 소스를 설정하지 않고 형태로 데이터 그리드를 삽입 다른 데이터 소스.

+0

gridview에 표시된 순서를 변경하려면 sqlCmd.CommandText의 열을 표시하려는 위치로 옮기십시오. –

0

GridView에서 AutoGenerateColumns="true" (기본값) 속성을 유지하고 <Columns>을 설정하지 않는 한. GridView는 자동으로 데이터 소스에 맞는 열을 생성합니다. 콤보 상자를 사용하려면

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true"> 
    <asp:ListItem>NameOnly</asp:ListItem> 
    <asp:ListItem>WithPosition</asp:ListItem> 
</asp:DropDownList> 
<br /> 
<asp:GridView runat="server" ID="GridView1"> 
</asp:GridView> 

코드 숨김

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (DropDownList1.SelectedValue == "WithPosition") 
    { 
     GridView1.DataSource = new List<dynamic>() { 
      new { Name = "Andy", LastName="Wayne", Position = "PG"}, 
      new { Name = "Bill", LastName="Johnson", Position = "SD" }, 
      new { Name = "Caroline", LastName="Barry", Position = "Manager"} 
     }; 
     GridView1.DataBind(); 
    } 
    else if (DropDownList1.SelectedValue == "NameOnly") 
    { 
     GridView1.DataSource = new List<dynamic>() { 
      new { Name = "Andy", LastName = "Wayne"}, 
      new { Name = "Bill", LastName = "Johnson"}, 
      new { Name = "Caroline", LastName = "Barry"} 
     }; 
     GridView1.DataBind(); 
    } 
} 
+0

감사합니다. 내가 귀하의 코드에 대해 이해하고있는 것 : 귀하는 표시 할 목록을 만들었습니다. 내 데이터 소스에서 가져온 데이터 항목을 표시하고 있습니다. –

+0

나는 datasouce 자동 설정을 시도하는 대신에 선택된 명령 문자열을 사용하여 데이터 소스에 쿼리를 호출 할 수 있다고 믿는다. 현재 스크립트를 작성 중입니다. –

관련 문제