ASP.net gridview评估每条记录后的数据字段

时间:2012-08-15 13:35:12

标签: asp.net gridview

嘿,我想知道每次将新记录放入一行时是否可以评估数据字段?

我的gridview代码是这样的:

<asp:Panel ID="pnlGrid" runat="server">
    <div class="left_main_container" style="margin-bottom:20px;">
        <asp:GridView ID="grdView" runat="server" CssClass="GridViewStyle"
            AutoGenerateColumns="False" GridLines="None" Width="100%">
            <Columns>
                <asp:BoundField DataField="id" Visible="False" />
                <asp:BoundField DataField="theName" HeaderText="Name" />
                <asp:BoundField DataField="status" HeaderText="Status" />
            </Columns>
            <RowStyle CssClass="RowStyle" />
            <EmptyDataRowStyle CssClass="EmptyRowStyle" />
            <PagerStyle CssClass="PagerStyle" />
            <SelectedRowStyle CssClass="SelectedRowStyle" />
            <HeaderStyle CssClass="HeaderStyle" />
            <EditRowStyle CssClass="EditRowStyle" />
            <AlternatingRowStyle CssClass="AltRowStyle" />
        </asp:GridView>
    </div>
</asp:Panel>

我需要评估的领域是状态。我需要查看状态是还是。如果,那么我需要为用户创建一个链接,以便能够将其更改为

gridview由以下代码填充:

    Dim objConn As MySqlConnection
    Dim objCmd As MySqlCommand

    objConn = New MySqlConnection(strConnString)
    objConn.Open()

    Dim strSQL As String
    strSQL = "SELECT id, status, " & _
                "CONCAT(' ', first_name, last_name) AS theName " & _
             "FROM(builder_requests) " & _
             "ORDER BY status, id DESC;"

    Dim dtReader As MySqlDataReader
    objCmd = New MySqlCommand(strSQL, objConn)
    dtReader = objCmd.ExecuteReader()

    grdView.DataSource = dtReader
    grdView.DataBind()

    dtReader.Close()
    dtReader = Nothing

    objConn.Close()
    objConn = Nothing

任何帮助都会很棒!

1 个答案:

答案 0 :(得分:2)

我喜欢在代码隐藏中使用TemplateField和函数来执行此类操作。

我假设你的状态字段是一个字符串字段而不是布尔值,但如果没有让我知道,我可以调整这个例子。

将您的BoundField替换为......

标记:

        <asp:TemplateField>
            <ItemTemplate>
                <%# Eval("status")%>&nbsp;
                <asp:LinkButton ID="cmdChangeStatus" runat="server" CommandName="ChangeStatusToYes" CommandArgument='<%# Eval("id") %>'
                    Visible='<%# SetChangeStatusVisibility(Eval("status")) %>'>Change to Yes</asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>

并在函数后面添加代码......

代码:

Protected Function SetChangeStatusVisibility(status As Object) As String
    Dim strStatus As String = status.ToString()
    If strStatus = "no" Then
        Return "True"
    Else
        Return "False"
    End If
End Function

然后您可以处理GridView.RowCommand事件以根据CommandArgument中的id值更改状态值:)