Steven

Steven

  • NA
  • 1
  • 0

Gridview edit fields

May 20 2009 3:52 PM
I current am working on an application that depending on a specific project type, it will return a different datatable with the fields I want to show.  So if the user loads into project type 1, the gridview will pull the datatable for that project type and bind it utilizing

Gridview.Datasource = dt;
Gridview.Databind();

Same goes for if the user loads a project with a project type of 2. (It pulls datatable 2 that has different fields than the datatable from project type 1)

Now being that the gridview control is dynamic (can be loaded based on project type), I'm having an issue when editing comes around.  I'm controlling the editing in the code behind by the following code:

 GridView.RowEditing += new GridViewEditEventHandler(GridView_RowEditing);

 protected void GridView_RowEditing(object sender, GridViewEditEventArgs e)
        {         
            GridView.EditIndex = e.NewEditIndex;          
            GridView1.DataBind();
        }


But right now my issue is that when I set the edit index, all fields show up as being editable whereas I only want specific fields to show up as editable.  So how can I go about specifying which field is editable in the code behind for instance column 4?

Which I know you can set the enabled to false on the Databound event as such:
 void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
             e.Row.Cells[2].Enabled = false;          
        }

but when you go into edit mode, the text box still surrounds that field (granted you can't edit it but there has to be a better way).

Essentially I'm looking to have the same effect as:

<Columns>
                           <asp:BoundField DataField="DATE_ORDERED" DataFormatString="{0:d}"
                                              HeaderText="Date Ordered" ReadOnly="true">
                                              <ControlStyle Height="20px" />
                                          </asp:BoundField>
                                          <asp:BoundField DataField="AMT" DataFormatString="{0:c}"
                                              HeaderText="Amount" ReadOnly="true">
                                              <ControlStyle Height="20px" />
                                          </asp:BoundField>
                                          <asp:BoundField DataField="DATE_SIGNED" DataFormatString="{0:d}"
                                              HeaderText="Date Signed" ReadOnly="true">
                                              <ControlStyle Height="20px" />
                                          </asp:BoundField>
                                          <asp:TemplateField HeaderText="Approved">
                                              <EditItemTemplate>
                                                  <asp:TextBox ID="txtApproval" runat="server" Width ="50px" 
                                                      Text='<%# Bind("APPROVED") %>'></asp:TextBox>
                                              </EditItemTemplate>
                                              <ItemTemplate>
                                                  <asp:Label ID="lblApproval" runat="server"
                                                      Text='<%# Bind("APPROVED") %>'></asp:Label>
                                              </ItemTemplate>
                                              <ControlStyle Height="20px" />
                                          </asp:TemplateField>                                         
                                      </Columns>

The first three fields are bound fields so therefore when going into edit mode, they are not surrounded by a textbox for edit and the Approval field is.

Thanks in advance for any help and or suggestions.