2017-08-16 57 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的更新命令總是靜態的,以產生一個動態更新的,你應該通過C#代碼處理GridView控件的RowUpdating事件做到這一點。

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

我得到了這個工作!非常感謝! –

相關問題