GridView行编辑

时间:2011-03-27 17:13:41

标签: 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 = "";
    }
}

1 个答案:

答案 0 :(得分:2)

BoundField's ReadOnly property设为“true”