Как назначить источник данных для Gridview программно

У меня есть два источника данных, и в зависимости от условий я хочу назначить их Gridview.

Я пытался применять сеансы по-разному и также сохранял флаг обновления, но не сработал, в конце концов я нашел подход ниже, но не повезло. как-то не обновляет данные gridview. Может кто-то, пожалуйста, посмотрите в код и дать решение.

ASPX код.

<dx:ASPxGridView AutoGenerateColumns="false" ClientIDMode="Static" ClientSideEvents-Init="OnInit" DataSourceID="dsSKUListMaint" ID="gvSKUListMaint" KeyFieldName="ID"
                                                             OnBatchUpdate="gv_BatchUpdate" OnCustomButtonCallback="gvSKUListMaint_CustomButtonCallback" OnHtmlDataCellPrepared="gvSKUListMaint_HtmlDataCellPrepared" OnRowDeleted="gvSKUListMaint_RowDeleted"
                                                             OnRowUpdated="gvSKUListMaint_RowUpdated" runat="server" SettingsCommandButton-ClearFilterButton-Image-IconID="filter_clearfilter_16x16" SettingsCommandButton-ClearFilterButton-Image-ToolTip="Clear all filters"
                                                             SettingsEditing-BatchEditSettings-EditMode="Cell" SettingsEditing-BatchEditSettings-StartEditAction="Click" SettingsEditing-Mode="Batch" Settings-HorizontalScrollBarMode="Hidden"
                                                             SettingsPager-Mode="ShowAllRecords" Settings-ShowFilterRow="true" Settings-ShowFooter="true" Settings-ShowStatusBar="Hidden" Settings-ShowTitlePanel="true" Settings-VerticalScrollableHeight="0"
                                                             Settings-VerticalScrollBarMode="Auto" Styles-AlternatingRow-BackColor="LightCyan" Width="100%">
                                                <Templates>
                                                    <TitlePanel>
                                                        <table border="0" cellpadding="0" cellspacing="0">
                                                            <tr>
                                                                <td style="height: 38px; padding-right: 5px;">
                                                                    <dx:ASPxImage ClientSideEvents-Click="function() { gvSKUListMaint.UpdateEdit(); }" Cursor="pointer" EmptyImage-IconID="save_save_32x32" ID="imgSaveChanges" runat="server" />
                                                                </td>
                                                                <td style="padding-right: 20px">
                                                                    <dx:ASPxLabel ClientSideEvents-Click="function() { gvSKUListMaint.UpdateEdit(); }" Cursor="pointer" ID="lblSaveChanges" runat="server" Text="Save Changes" />
                                                                </td>
                                                                <td style="padding-right: 5px">
                                                                    <dx:ASPxImage ClientSideEvents-Click="function() { gvSKUListMaint.CancelEdit(); }" Cursor="pointer" EmptyImage-IconID="history_undo_32x32" ID="imgCancelChanges"
                                                                                  runat="server" />
                                                                </td>
                                                                <td>
                                                                    <dx:ASPxLabel ClientSideEvents-Click="function() { gvSKUListMaint.CancelEdit(); }" Cursor="pointer" ID="lblCancelChanges" runat="server" Text="Cancel Changes" />
                                                                </td>
                                                            </tr>
                                                        </table>
                                                    </TitlePanel>
                                                </Templates>
                                                <Columns>
                                                    <dx:GridViewCommandColumn ButtonType="Image" Caption=" " ShowClearFilterButton="true" Width="32">
                                                        <CustomButtons>
                                                            <dx:GridViewCommandColumnCustomButton Image-IconID="edit_delete_16x16" Image-ToolTip="Delete item from SKU List" />
                                                        </CustomButtons>
                                                    </dx:GridViewCommandColumn>
                                                    <dx:GridViewDataTextColumn FieldName="ID" Visible="false" />
                                                    <dx:GridViewDataTextColumn EditFormSettings-Visible="False" FieldName="SKU" Settings-AutoFilterCondition="Contains" Width="60" />
                                                    <dx:GridViewDataTextColumn FieldName="SKU_DESC" Settings-AutoFilterCondition="Contains" SortIndex="0" SortOrder="Ascending" />
                                                    <dx:GridViewDataTextColumn FieldName="MAX_QTY" Settings-AutoFilterCondition="Equals" Width="80" />
                                                </Columns>
                                                <TotalSummary>
                                                    <dx:ASPxSummaryItem DisplayFormat="#,##0 Items" FieldName="SKU" ShowInColumn="SKU" SummaryType="Count" />
                                                </TotalSummary>
                                            </dx:ASPxGridView>
                                            <asp:SqlDataSource ConnectionString="<%$ ConnectionStrings:FOET_ConnectionString %>" DeleteCommand="usp_gvSKUListMaint_DELETE" DeleteCommandType="StoredProcedure"
                                                               ID="dsSKUListMaint" runat="server" SelectCommand="usp_gvSKUListMaint_SELECT" SelectCommandType="StoredProcedure" UpdateCommand="usp_gvSKUListMaint_UPDATE" UpdateCommandType="StoredProcedure">
                                                <SelectParameters>
                                                    <asp:SessionParameter Name="DC" SessionField="storeType" Type="Int32" />
                                                    <asp:SessionParameter Name="storeType" SessionField="storeType" Type="Int32" />
                                                </SelectParameters>
                                                <UpdateParameters>
                                                    <asp:Parameter Name="ID" Type="Int16" />
                                                    <asp:Parameter Name="SKU_DESC" Type="String" />
                                                    <asp:Parameter Name="MAX_QTY" Type="Int32" />
                                                </UpdateParameters>
                                            </asp:SqlDataSource>
                                            <asp:SqlDataSource ConnectionString="<%$ ConnectionStrings:FOET_ConnectionString %>" DeleteCommand="usp_gvSKUListMaint_DELETE" DeleteCommandType="StoredProcedure"
                                                               ID="SqlDataSource1" runat="server" SelectCommand="usp_gvSKUListMaint_SELECT" SelectCommandType="StoredProcedure" UpdateCommand="usp_gvSKUListMaint_UPDATE" UpdateCommandType="StoredProcedure">
                                                <SelectParameters>
                                                    <asp:Parameter Name="DC" Type="Int32" DefaultValue ="0" />
                                                    <asp:Parameter Name="storeType" Type="Int64" DefaultValue ="0" />
                                                </SelectParameters>
                                                <UpdateParameters>
                                                    <asp:Parameter Name="ID" Type="Int16" />
                                                    <asp:Parameter Name="SKU_DESC" Type="String" />
                                                    <asp:Parameter Name="MAX_QTY" Type="Int32" />
                                                </UpdateParameters>
                                            </asp:SqlDataSource>

Код.CS

protected void gvItemFile_DataBound(object sender, EventArgs e)
        {
            gvItemFile.ExpandAll();
            string Dept = gvItemFile.GetRowValues(0, "DEPT").ToString();
            if (Dept.Contains("956"))
            {
                gvSKUListMaint.DataSourceID = "dsSKUListMaint";
                gvSKUListMaint.DataBind();
            }
            if(Dept.Contains("952"))
            {
                gvSKUListMaint.DataSourceID = "SqlDataSource1";
                gvSKUListMaint.DataBind();
            }
        }

0 ответов

Другие вопросы по тегам