c# - GridView 行编辑

标签 c# asp.net .net gridview

我有一个带有五个 BoundFields 的 gridview。当向网格插入值时,我有文本框字段到单元格并且它工作正常。我的问题是当编辑一行时我只需要 3 个单元格是可编辑的,其他两个单元格应该只读。我怎样才能做到这一点?

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        CellPadding="4" ForeColor="#333333" GridLines="None" BackColor="WhiteSmoke" CssClass="grid-view" 
        AlternatingRowStyle-CssClass="alt" PageSize="8"
            onrowcancelingedit="GridView1_RowCancelingEdit" 
            onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing" 
            onrowupdating="GridView1_RowUpdating" 
            onrowdatabound="GridView1_RowDataBound" 
            onrowcommand="GridView1_RowCommand" onrowcreated="GridView1_RowCreated">
        <RowStyle BackColor="#EFF3FB" />
        <PagerSettings PageButtonCount="5" />
        <Columns>
        <asp:TemplateField>
        <HeaderTemplate>
            <asp:LinkButton ID="btnInsert" runat="server" 
            Text="Insert" CommandName="Insert" ForeColor="White" />
        </HeaderTemplate>
        </asp:TemplateField>
            <asp:CommandField HeaderText="Edit-Update" ShowEditButton="True" />
            <asp:BoundField DataField="DeptID" HeaderText="Department ID" 
                ReadOnly="True" />
                           <asp:BoundField DataField="DeptCode" HeaderText="Department Code"/>
            <asp:BoundField DataField="DeptDescription" HeaderText="Description" />          

            <asp:CommandField HeaderText="Delete" ShowDeleteButton="True" />               
        </Columns>
        <PagerStyle CssClass="pgr"></PagerStyle>
        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <EditRowStyle BackColor="#2461BF" />
        <AlternatingRowStyle BackColor="White" />
    </asp:GridView>

向网格插入值的代码:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Insert")
    {
        DataTable DT = new DataTable();
        DT = PinCDAO.GetDepartments();
        DataRow DR = DT.NewRow();
        DT.Rows.InsertAt(DR, 0);

        GridView1.EditIndex = 0;
        GridView1.DataSource = DT;

        GridView1.DataBind();

        ((LinkButton)GridView1.Rows[0].Cells[1].Controls[0]).Text = "Insert";
    }
}

行编辑代码:

GridView1.EditIndex = e.NewEditIndex;

插入和更新:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    if (GetDeptCode())
    {
        lblMessage.Text = "Already Contains a Department Code with the Same Name";
    }
    else if (CheckCodeLength())
    {
        lblMessage.Text = "Department Code Character Length is 3";
    }
    else
    {
        if (((LinkButton)GridView1.Rows[0].Cells[1].Controls[0]).Text == "Insert")
        {
            DepertmentProperties DP = new DepertmentProperties();
            DP.DeptCode = ((TextBox)GridView1.Rows[0].Cells[3].Controls[0]).Text.ToString();
            DP.DeptDescription = ((TextBox)GridView1.Rows[0].Cells[4].Controls[0]).Text.ToString();

            PinCDAO.InsertDepartment(DP);
        }
        else
        {
            DepertmentProperties DP = new DepertmentProperties();
            DP.DeptID = Convert.ToInt32(GridView1.Rows[e.RowIndex].Cells[2].Text.ToString());
            DP.DeptCode = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text.ToString();
            DP.DeptDescription = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text.ToString();

            PinCDAO.UpdateDepartment(DP);
        }
        GridView1.EditIndex = -1;
        BindData();
        lblMessage.Text = "";
    }
}

最佳答案

设置BoundField's ReadOnly property为“真”

关于c# - GridView 行编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5450831/

相关文章:

c# - Moq 与 Autofac Func<Owned<IClass>>

c# - C#中的轻量级表类型

jquery - 在jquery中找到最接近的值

asp.net - 在客户端访问 ASP.NET 身份验证票证(通过 javascript)

c# - 如何在 .net 2.0 项目中使用 .net 3.0 类?

c# - C# 中的哈希密码和盐密码

c# - 任何方法类型的委托(delegate) - C#

javascript - ASP .NET MVC 无法使用 jQuery/JavaScript 提交表单

.net - 将频繁的文件 I/O 操作放在 SyncLock block 中是一个坏主意吗?

.net - 什么可能导致 CurrentCulture 和 CurrentUICulture 默认为明显系统区域性以外的值?