Paul Kirkason
Paul Kirkason

Reputation: 247

Making a column editable in an ASP.net GridView

I have a GridView where one of the columns is for a display order for the fields where they will be shown on the front end of my website. Instead of going into each record in the edit page and having to change the order this way, it would be handier to be able to click a button and have the whole DisplayOrder (int) editable, therefore making life alot easier. How can this be done?

Upvotes: 2

Views: 56384

Answers (5)

RonaldPaguay
RonaldPaguay

Reputation: 335

This link had the answer I was looking for. If you have a custom data source, must handle Edit each edit event raised by GridView. In my case:

Protected Sub gMaterias_RowCancelingEdit(sender As Object, e As GridViewCancelEditEventArgs) Handles gMaterias.RowCancelingEdit
    gMaterias.EditIndex = -1
    BindData()
End Sub

Protected Sub gMaterias_RowEditing(sender As Object, e As GridViewEditEventArgs) Handles gMaterias.RowEditing
    gMaterias.EditIndex = e.NewEditIndex
    BindData()
End Sub

Protected Sub gMaterias_RowUpdating(sender As Object, e As GridViewUpdateEventArgs) Handles gMaterias.RowUpdating
    lError.Visible = False
    lError.Text = ""
    Dim idMateria As Integer = e.Keys(0)
    Dim row As GridViewRow = gMaterias.Rows(e.RowIndex)
    Dim tbl As DataTable = Session("Materias")

    tbl.Rows(row.DataItemIndex)("universidad") = CType(gMaterias.Rows(e.RowIndex).Cells(5).Controls(0), TextBox).Text

    Dim calf = CType(gMaterias.Rows(e.RowIndex).Cells(6).Controls(0), TextBox).Text
    If IsNumeric(calf) Then
        tbl.Rows(row.DataItemIndex)("calificacion") = calf
    Else
        lError.Visible = True
        lError.Text = "La calificación no es válida"
    End If

    gMaterias.EditIndex = -1
    BindData()
End Sub

Upvotes: 0

RSB
RSB

Reputation: 359

You can try this, it will give text boxes in column

    <asp:Label ID="DescriptionLabel" runat="server"
        Text='<%# Eval("Description") %>'></asp:Label>

    <asp:TextBox ID="Description" runat="server"
        Text='<%# Eval("Description") %>' Width="175px"
        visible="false"></asp:TextBox>

</ItemTemplate>

Upvotes: 0

Kapil Pandit
Kapil Pandit

Reputation: 1

You can try below:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AutoGenerateEditButton = "true">
       <Columns>
            <asp:BoundField DataField="prodId" HeaderText="Product Id" SortExpression="prodId" ReadOnly = "true" />
            <asp:BoundField DataField="prodQuantity" HeaderText="Quantity" 
                                SortExpression="prodQuantity" ReadOnly = "true" />
        </Columns>
</asp:GridView>

At Gridview level set AutoGenerateEditButton = "true". This will enable user to edit row. At Data Field level use ReadOnly = "true" to prevent specific field(in row) from being edited.

Hope this helps.

Upvotes: 0

Subhash Jakhar
Subhash Jakhar

Reputation: 172

You can try this using ShowDeleteButton="true" in asp commandfield you can make gridview edidable :-

<Columns>
     <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowSelectButton="True" />
      <asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" SortExpression="ID" />
      <asp:BoundField DataField="Name" HeaderText="Name"    SortExpression="Name" />
   </Columns>

may be help you

Upvotes: 0

Bhupendra Shukla
Bhupendra Shukla

Reputation: 3914

Try this code :

 <asp:ListBox ID="ListBox1" runat="server">
                <asp:ListItem>Manager1</asp:ListItem>
                <asp:ListItem>Manager2</asp:ListItem>
                <asp:ListItem>Manager3</asp:ListItem>
                <asp:ListItem>Manager4</asp:ListItem>
            </asp:ListBox>
            <asp:GridView ID="UserAllocationGrid" runat="server"
                AutoGenerateColumns="False">
            <Columns>

                <asp:BoundField DataField="Manager" HeaderText="Manager"
                    SortExpression="managers" />
                    <asp:TemplateField HeaderText="Allocation Percentage">
                    <ItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server"
                            Text= '<%# Bind("AllocationPercentage") %>' BorderStyle="None"></asp:TextBox>
                    </ItemTemplate>
                    </asp:TemplateField>

            </Columns>
            </asp:GridView>

Code Behind

     void fillGV()
        {
            DataTable UserAllocationTable = new DataTable();

            UserAllocationTable.Columns.Add("Manager");
            UserAllocationTable.Columns.Add("AllocationPercentage");
            // go through listbox1 to find selected managers = selectedManagersList 
            List<string> selectedManagersListDates = new List<string>();
            int counterR = 0;
            foreach (ListItem strItem in ListBox1.Items)
            {                     
                    //selectedManagersListDates.Add(strItem.Value); 
                    DataRow drManagerName = UserAllocationTable.NewRow();
                    UserAllocationTable.Rows.Add(drManagerName);
                    UserAllocationTable.Rows[counterR]["Manager"] = strItem.Value;
                    counterR = counterR + 1;            
            }
           // ViewState["UserAllocationTable"] = UserAllocationTable;
            UserAllocationGrid.DataSource = UserAllocationTable;
            UserAllocationGrid.DataBind(); 
        }

Use this void in any event I did it in abutton click

      protected void Button1_Click(object sender, EventArgs e)
        {
            fillGV();

        }

Upvotes: 2

Related Questions