不想在网格中显示数据的Id

时间:2014-04-19 12:08:11

标签: asp.net sql-server

我正在尝试在网格上显示表格数据,但我想要显示表格ID。

我还保留 ApplyFormatInEditMode =" True"

如果我执行datafield visible = false 。它不适用于编辑命令。

如何隐藏网格中的ID。

这是我的代码。

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="EditExpenses.aspx.cs" Inherits="EditExpenses" %>

    

编辑费用

                               

        <asp:BoundField DataField="Expense_Category" HeaderText="Expense Category" 
            SortExpression="Expense_Category" ApplyFormatInEditMode="True" 
             />
        <asp:BoundField DataField="Expense_Description" 
            HeaderText="Expense Description" SortExpression="Expense_Description" 
            ApplyFormatInEditMode="True" />
        <asp:BoundField DataField="Expense_Date" HeaderText="Expense Date" 
            SortExpression="Expense_Date" ApplyFormatInEditMode="True" 
            DataFormatString="{0:d}" HtmlEncode="False" />
        <asp:BoundField DataField="Amount" HeaderText="Amount" 
            SortExpression="Amount" ApplyFormatInEditMode="True" 
             />
    </Columns>
</asp:GridView>
</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:testAzharConnectionString %>" 
    SelectCommand="SELECT Expense_Category, Expense_Description, Amount, Expense_Date FROM [CompanyExpenses3]"
    UpdateCommand="Update [CompanyExpenses3] SET Expense_Category=@Expense_Category, Expense_Description=@Expense_Description,Expense_Date=@Expense_Date, Amount=@Amount WHERE ExpenseId=@ExpenseId"
          OnUpdated="OnDSUpdatedHandler"></asp:SqlDataSource>
</asp:Content>

1 个答案:

答案 0 :(得分:0)

你可以写一个css类......

<style type="text/css">
.hiddencol
    {
        display:none;
    }
    </style>

在您的Rowcreated gridview事件中,您可以使用CSS类..

protected void gridview1_RowCreated(object sender, GridViewRowEventArgs e)
    {

        try
        {

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].CssClass = "hiddencol";

            }
            if (e.Row.RowType == DataControlRowType.Header)
            {
                e.Row.Cells[0].CssClass = "hiddencol";
            }
        }
        catch (Exception ex)
        { }
    }

此处0是您必须隐藏的列的索引。