【问题标题】:GridView row editingGridView 行编辑
【发布时间】:2011-03-27 17:13:41
【问题描述】:

我有一个带有五个 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 = "";
    }
}

【问题讨论】:

    标签: c# asp.net .net gridview


    【解决方案1】:

    BoundField's ReadOnly property 设置为“真”

    【讨论】:

    • 当我向网格中插入值时,我需要文本框出现在所有单元格中。编辑一行时,我只需要文本框不要出现在 3 个单元格中。所以这不会不行。
    • @chamara:GridView 默认不附带插入功能。那么您是在使用其他控件还是进行一些自定义?并且 ReadOnly 属性只影响编辑。请澄清更多,分享一些代码会更有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    相关资源
    最近更新 更多