2011-03-27 161 views
2

我有一个gridview有五个BoundFields.when插入值到网格我有文本框字段的单元格,它的工作fine.my问题是编辑一行时,我只需要3个单元格是可编辑和其他两个单元应该只读只读,我可以实现这个目标吗?GridView行编辑

<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"; 
    } 
} 

行уditing代码:

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

回答

2

设置BoundField's ReadOnly property为 “true”

+0

我需要文本框出现在所有单元格中,当我向grid.i插入值时,只需要在编辑一行时不出现在3个单元格中的文本框,所以这不起作用。 – chamara 2011-03-28 03:41:03

+0

@chamara:GridView默认不带插入功能。那么你是使用其他控件还是一些定制?而ReadOnly属性只影响Edit。请澄清更多,分享一些代码会更有帮助。 – gbs 2011-03-28 15:47:18