从 Gridview 获取 SQLDataSource 更新参数

发布于 2024-10-27 19:15:46 字数 2438 浏览 2 评论 0原文

我试图从 gridview 获取更新参数,但它是一个我不想显示的 ID 列。如果我在边界字段中显示数据,它工作正常,但如果我将可见性设置为 false,则参数将不再发送到更新存储过程。似乎没有可以放入网格视图中的隐藏字段列。

我尝试通过后面的代码设置参数,但我不确定如何访问我想要的数据,以下代码不起作用(它将参数设置为空

Protected Sub grvFacilityDisciplineBillingRate_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs) Handles grvFacilityDisciplineBillingRate.RowUpdating
    Dim row As GridViewRow = grvFacilityDisciplineBillingRate.Rows(e.RowIndex)
    sqlDisciplineBillingRate.UpdateParameters("Facility_ID").DefaultValue = CInt(row.DataItem("Facility_ID"))
    sqlDisciplineBillingRate.UpdateParameters("Discipline_ID").DefaultValue = CInt(row.DataItem("Discipline_ID"))
End Sub

,这是显示两个 ID 列的前端,这不是我想要的

    <asp:SqlDataSource ID="sqlDisciplineBillingRate" runat="server" DataSourceMode="DataSet" SelectCommandType="StoredProcedure" SelectCommand="SP_Facility_DisciplineBillingRates" UpdateCommandType="StoredProcedure" UpdateCommand="SP_DiscplineBillingRate_Update" ConnectionString="<%$ ConnectionStrings:Trustaff_ESig2 %>">
        <SelectParameters>
            <asp:Parameter Name="Facility_ID" DbType="Int32" />
        </SelectParameters>
        <UpdateParameters>
            <asp:Parameter Name="Facility_ID" DbType="Int32" />
            <asp:Parameter Name="Discipline_ID" DbType="Int32" />
        </UpdateParameters>
    </asp:SqlDataSource>
    <asp:GridView ID="grvFacilityDisciplineBillingRate" runat="server" DataSourceID="sqlDisciplineBillingRate" DataKeyNames="DisciplineBillingRate_ID"  AutoGenerateColumns="false" AllowSorting="true" AllowPaging="false" AutoGenerateEditButton="true"   CssClass="gridview">
        <EmptyDataTemplate>
            There were no discipline billing rates for this facility.
        </EmptyDataTemplate>
        <Columns>
            <asp:BoundField DataField="Name" HeaderText="Discipline" SortExpression="Name" ReadOnly="true" />
            <asp:BoundField DataField="BillingRate" HeaderText="Billing Rate" SortExpression="BillingRate" />
            <asp:BoundField DataField="Facility_ID" HeaderText="Facility_ID" SortExpression="Facility_ID" InsertVisible="false" />
            <asp:BoundField DataField="Discipline_ID" HeaderText="Discipline_ID" SortExpression="Discipline_ID" />
        </Columns>
    </asp:GridView>

I am trying to get a parameter for my update from the gridview but it is an ID column that I do not want displayed. If I display the data in a boundfield it works fine but if I set the visibility to false the parameter is no longer sent to the update stored procedure. There does not appear to be a hiddenfield column that I can put into the gridview.

I have tried to set the parameters through the code behind but I am not certain on how to access the data I want the following code does not work (It sets the parameter to nothing

Protected Sub grvFacilityDisciplineBillingRate_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs) Handles grvFacilityDisciplineBillingRate.RowUpdating
    Dim row As GridViewRow = grvFacilityDisciplineBillingRate.Rows(e.RowIndex)
    sqlDisciplineBillingRate.UpdateParameters("Facility_ID").DefaultValue = CInt(row.DataItem("Facility_ID"))
    sqlDisciplineBillingRate.UpdateParameters("Discipline_ID").DefaultValue = CInt(row.DataItem("Discipline_ID"))
End Sub

And this is the front end with the two ID columns displayed, which is not what I want

    <asp:SqlDataSource ID="sqlDisciplineBillingRate" runat="server" DataSourceMode="DataSet" SelectCommandType="StoredProcedure" SelectCommand="SP_Facility_DisciplineBillingRates" UpdateCommandType="StoredProcedure" UpdateCommand="SP_DiscplineBillingRate_Update" ConnectionString="<%$ ConnectionStrings:Trustaff_ESig2 %>">
        <SelectParameters>
            <asp:Parameter Name="Facility_ID" DbType="Int32" />
        </SelectParameters>
        <UpdateParameters>
            <asp:Parameter Name="Facility_ID" DbType="Int32" />
            <asp:Parameter Name="Discipline_ID" DbType="Int32" />
        </UpdateParameters>
    </asp:SqlDataSource>
    <asp:GridView ID="grvFacilityDisciplineBillingRate" runat="server" DataSourceID="sqlDisciplineBillingRate" DataKeyNames="DisciplineBillingRate_ID"  AutoGenerateColumns="false" AllowSorting="true" AllowPaging="false" AutoGenerateEditButton="true"   CssClass="gridview">
        <EmptyDataTemplate>
            There were no discipline billing rates for this facility.
        </EmptyDataTemplate>
        <Columns>
            <asp:BoundField DataField="Name" HeaderText="Discipline" SortExpression="Name" ReadOnly="true" />
            <asp:BoundField DataField="BillingRate" HeaderText="Billing Rate" SortExpression="BillingRate" />
            <asp:BoundField DataField="Facility_ID" HeaderText="Facility_ID" SortExpression="Facility_ID" InsertVisible="false" />
            <asp:BoundField DataField="Discipline_ID" HeaderText="Discipline_ID" SortExpression="Discipline_ID" />
        </Columns>
    </asp:GridView>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

灼疼热情 2024-11-03 19:15:46

You simply have to set the DataKeyNames e.g. DataKeyNames="Facility_ID,Discipline_ID" and the SqlDataSource will figure it out for you when using GV's Update and Delete feature.

冷月断魂刀 2024-11-03 19:15:46

尝试使用模板字段并使用标签控件并通过设置visible =“false”来隐藏它们。

    <asp:TemplateField HeaderText="College">
       <ItemTemplate>
         <asp:Label ID="lbl_Title" runat="server" text='<%# Bind("Title") %>' visible="false"></asp:Label>
       </ItemTemplate>
   </asp:TemplateField>

并使用下面的代码从代码隐藏中访问值。

If gvrow.RowType = DataControlRowType.DataRow Then
     Dim label1 As Label = DirectCast(gvrow.FindControl("lbl_Title"), Label)
'Access text of label using label1.text and cast to int or string
End If

如果您不希望使用 ID 列,则可以取消使用模板化字段。仍然使用 datakeynames 属性访问它

Try using Template fields and use a label control and hide them by setting visible="false".

    <asp:TemplateField HeaderText="College">
       <ItemTemplate>
         <asp:Label ID="lbl_Title" runat="server" text='<%# Bind("Title") %>' visible="false"></asp:Label>
       </ItemTemplate>
   </asp:TemplateField>

And use below code to access values from codebehind.

If gvrow.RowType = DataControlRowType.DataRow Then
     Dim label1 As Label = DirectCast(gvrow.FindControl("lbl_Title"), Label)
'Access text of label using label1.text and cast to int or string
End If

You can eliminate using templated fields if you don't wish for ID columns. And still access it using datakeynames property

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文