2017-08-16 13 views
1

私はGridViewをSQLテーブルにリンクしています。表には未知数または可変数の列があります。列の数と列の名前の両方が可変です。すべての列を更新できるようにsqlDataSourceに動的なUpdateCommandを設定することは可能ですか?そうであれば;どうやって?可変数の列でGridViewを更新することはできますか?

コードは、私が試した:

<asp:GridView ID="GridView1" AutoGenerateColumns="True" ShowHeaderWhenEmpty ="True" DataSourceID="UpdateSqlDataSource" 
    CssClass = "table" runat="server" AllowSorting="True" BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" 
    CellPadding="3" DataKeyNames="UpdateID" ShowFooter="True" 
    AutoGenerateDeleteButton="true" AutoGenerateSelectButton ="true" AutoGenerateEditButton="true"> 
    <AlternatingRowStyle BackColor="#F7F7F7" /> 
    <Columns> 
    </Columns> 
    <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" /> 
    <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" /> 
    <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" /> 
    <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" /> 
    <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" /> 
    <SortedAscendingCellStyle BackColor="#F4F4FD" /> 
    <SortedAscendingHeaderStyle BackColor="#5A4C9D" /> 
    <SortedDescendingCellStyle BackColor="#D8D8F0" /> 
    <SortedDescendingHeaderStyle BackColor="#3E3277" /> 
</asp:GridView> 
<asp:SqlDataSource ID ="UpdateSqlDataSource" runat ="server" ConnectionString="<%$ ConnectionStrings:MachineUpdateDataBaseConnectionString %>" 
    DeleteCommand="DELETE FROM [MachineUpdate] WHERE [UpdateID] = @UpdateID" 
    SelectCommand="SELECT * FROM [MachineUpdate]" UpdateCommand="UPDATE SET [MachineUpdate] = @MachineUpdate WHERE [UpdateID] = @UpdateID[*] = @* WHERE [UpdateID] = @UpdateID"> 
    <DeleteParameters> 
      <asp:Parameter Name="UpdateID" Type="Int32" /> 
    </DeleteParameters> 
</asp:SqlDataSource> 
+1

あなたが試したことを私たちに教えてください。私たちはそこから助けてくれるでしょう。 –

+0

何を更新しようとしていますか?行の内容は? – Ewerton

+0

ヒント:columns.addをループ内に追加します。 –

答えて

0

あなたをSqlDataSourceのUpdateCommandのは常に静的であり、動的更新を生成するために、あなたは、GridViewののRowUpdatingイベントを処理するC#コードでそれを行う必要があります。

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) 
{ 
    string sqlCommand = "UPDATE YourTable SET "; 
    foreach (DictionaryEntry item in e.NewValues) 
    { 
     // You will need to take care to not update PK, FK, etc 
     // You will need to handle DB restrictions 
     // You will need to handle DataTypes (eg: Not set a boolean in a Date column) 
     sqlCommand = sqlCommand + item.Key + " = " + item.Value + ","; 
    } 

    sqlCommand = sqlCommand.TrimEnd(','); // removes the last comma 

    foreach (DictionaryEntry de in e.Keys) 
    { 
     //Assuming that you will have just only one key 
     // You can get the Key in the GridView.DataKeyNames property as well 
     sqlCommand = sqlCommand + " WHERE " + de.Key.ToString() + " = " + de.Value.ToString(); 
    } 


    GridView grd = (GridView)sender; 
    SqlDataSource ds = (SqlDataSource)grd.DataSourceObject; 
    ds.UpdateCommand = sqlCommand; // Modify the UpdateCommand of you SqlDataSource 
} 

注:動的に良いアイデアではありません更新列は、あなたはメリットよりも多くの頭痛を持っています。しかし、シンプルなシナリオでは、上記のコードが動作する可能性があります。

+1

私はこれを動作させました!どうもありがとうございます! –

関連する問題