2016-06-28 27 views
1

where節を作成して、データベースからViewStateに格納された特定のIDを持つgridviewに値を渡すことができます。句はGridViewコントロールでWHERE句を使用するにはどうすればよいですか?

<asp:GridView ID="gvView" runat="server" AutoGenerateColumns="false" DataKeyNames="ID" 
       DataSourceID="SqlDataSource" AllowPaging="true" PageSize="50" Width="100%" 
       EmptyDataText="--- No records yet. ---" PagerStyle-HorizontalAlign="Center" PagerSettings-PageButtonCount="5" 
       EmptyDataRowStyle-ForeColor="#888581" EmptyDataRowStyle-Font-Size="14px" EmptyDataRowStyle-Height="30px" 
       EmptyDataRowStyle-Font-Italic="true" AlternatingRowStyle-BackColor="#E2E2E2" 
       PagerStyle-CssClass="pager"> 
       <Columns> 
        <asp:TemplateField HeaderText="Select"> 
         <ItemTemplate> 
          <asp:CheckBox ID="RowSelector" runat="server" /> 
         </ItemTemplate> 
        </asp:TemplateField> 
        <asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" Visible="false" /> 
        <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" /> 


       </Columns> 
      </asp:GridView> 
      <asp:SqlDataSource ID="SqlDataSource" runat="server" 
       ProviderName="System.Data.SqlClient" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
       SelectCommand="SELECT ID, Title FROM Table **WHERE [email protected]** ORDER BY ID"> 
      </asp:SqlDataSource> 

答えて

0

コードで星に書かれている場合、あなたがSqlDataSourceコントロール

SqlDataSource SqlDataSource = new SqlDataSource(); 
    SqlDataSource.ID = "SqlDataSource"; 
    this.Page.Controls.Add(SqlDataSource1); 
    SqlDataSource.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["conString"].ConnectionString; 
    SqlDataSource.SelectCommand = "SELECT ID, Title FROM Table WHERE AnotherID='"+anotherid+"' ORDER BY ID"; 
    gvView.DataSource = SqlDataSource; 
    gvView.DataBind(); 
+0

はあなたの助けをありがとう...私の問題を整理するために、この方法を使用します。.. –

0
<asp:SqlDataSource ID="SqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT ID, Title FROM Table WHERE WHERE [AnotherID] = '" + @anotherid + "' ORDER BY ID "> 
     <SelectParameters> 
      <asp:Parameter DefaultValue='<%# ViewState("ViewStateID") %>' Name="anotherid" Type="Int32" /> 
     </SelectParameters> 
</asp:SqlDataSource> 
    と結合する場合は、あなたのGridView

    protected void BindGridview(int anotherid) 
    { 
        DataSet ds = new DataSet(); 
        using (SqlConnection con = new SqlConnection("Data Source=source;Integrated Security=true;Initial Catalog=MySampleDB")) 
        { 
         con.Open(); 
         SqlCommand cmd = new SqlCommand("SELECT ID, Title FROM Table WHERE AnotherID='"+anotherid+"' ORDER BY ID", con); 
         SqlDataAdapter da*emphasized text* = new SqlDataAdapter(cmd); 
         da.Fill(ds); 
         con.Close(); 
         gvView.DataSource = ds; 
         gvView.DataBind(); 
    } 
    

    をバインドするために、このmethordを呼び出すことができます

  1. ViewStateからパラメータを作成
  2. selectコマンドに "@" + paramterNameのパラメータ名を含めます。
+0

はあなたの助けをありがとう...これはあなたのソリューションであれば:-) Midhun –

+0

により投稿された手順を試みることによって私の解決策を手に入れましたそれを受け入れられた答えとしてマークすることができます。ありがとうございました。 – Tuks

関連する問題