locked
Gridview update RRS feed

  • Question

  • User-2094959909 posted

    Hello everyone, 

    i have a gridview who have some operations as a test, the operations are made of  (name operation, number operation, line operation, model operation, ... and video) so when i want to add a new operation it's all good but i have another web form with a gridview in it that shows me all operations but when i have to update an operation all the fields are textboxes, so like example the line operation and model operation are listbox so in the update i need to update them with the models and lines i have in the lists and also for the video i need to fileupload a new video so how can i do that if anyone can help please ?

    Monday, July 30, 2018 5:17 PM

All replies

  • User283571144 posted

    Hi Omar27,

    so like example the line operation and model operation are listbox so in the update i need to update them with the models and lines i have in the lists and also for the video i need to fileupload a new video so how can i do that if anyone can help please ?

    According to your description, I couldn't understand your requirement clearly.

    Do you mean you want to upload a file and show the listbox in the girdview edit template?

    If this is your requirement, I suggest you could refer to below codes:

    ASPX:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GrdiviewWithListBoxandFileUplaod.aspx.cs" Inherits="AspNetNormalIssue.Webform.GrdiviewWithListBoxandFileUplaod" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:GridView ID="gvData" runat="server" AutoGenerateColumns="False" AutoGenerateEditButton="True" OnRowCancelingEdit="gvData_RowCancelingEdit" OnRowEditing="gvData_RowEditing" OnRowUpdating="gvData_RowUpdating" OnRowCommand="gvData_RowCommand" DataKeyNames="ID">
                    <Columns>
                        <asp:BoundField DataField="ID" HeaderText="ID" />
                        <asp:TemplateField HeaderText="Code">
                            <ItemTemplate>
                                <%# Eval("Code") %>
                            </ItemTemplate>
                            <EditItemTemplate>
                                 <asp:CheckBoxList ID="CheckBoxList1" runat="server" >
                                    <asp:ListItem Value="A" Text="A"></asp:ListItem>
                                    <asp:ListItem Value="B" Text="B"></asp:ListItem>
                                    <asp:ListItem Value="C" Text="C"></asp:ListItem>
                                    <asp:ListItem Value="D" Text="D"></asp:ListItem>
                                    <asp:ListItem Value="E" Text="E"></asp:ListItem>
                                </asp:CheckBoxList>
                            </EditItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Video">
                            <ItemTemplate>
                                <%# Eval("Video") %>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:Label ID="lblVideo" runat="server" Text='<%# Bind("Video") %>'></asp:Label>
                                <asp:FileUpload ID="fuVideo" runat="server" /><asp:Button ID="btnVideo" runat="server" Text="Upload" CommandName="Upload" CommandArgument='<%# Bind("ID") %>' />
                            </EditItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Description">
                            <ItemTemplate>
                                <%# Eval("Description") %>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="txbDescription" runat="server" Text='<%# Bind("Description") %>'></asp:TextBox>
                            </EditItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AutoGenerateEditButton="True" OnRowCancelingEdit="gvData_RowCancelingEdit" OnRowEditing="gvData_RowEditing" OnRowUpdating="gvData_RowUpdating" OnRowCommand="gvData_RowCommand" DataKeyNames="ID">
                    <Columns>
                        <asp:BoundField DataField="ID" HeaderText="ID" />
                        <asp:TemplateField HeaderText="Code">
                            <ItemTemplate>
                                <%# Eval("Code") %>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="txbCode" runat="server" Text='<%# Bind("Code") %>'></asp:TextBox>
                            </EditItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Video">
                            <ItemTemplate>
                                <%# Eval("Video") %>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:Label ID="lblVideo" runat="server" Text='<%# Bind("Video") %>'></asp:Label>
                                <asp:FileUpload ID="fuVideo" runat="server" /><asp:Button ID="btnVideo" runat="server" Text="Upload" CommandName="Upload" CommandArgument='<%# Bind("ID") %>' />
                            </EditItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Description">
                            <ItemTemplate>
                                <%# Eval("Description") %>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="txbDescription" runat="server" Text='<%# Bind("Description") %>'></asp:TextBox>
                            </EditItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
    
            </div>
        </form>
    </body>
    </html>
    

    Code-behind:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace AspNetNormalIssue.Webform
    {
        public partial class GrdiviewWithListBoxandFileUplaod : System.Web.UI.Page
        {
            List<GridViewEntity> source = new List<GridViewEntity>() {
        new GridViewEntity{ ID=1, Code="A", Video="", Description="It's A" },
        new GridViewEntity{ ID=2, Code="B", Video="", Description="It's B" },
        new GridViewEntity{ ID=3, Code="C", Video="", Description="It's C" },
        new GridViewEntity{ ID=4, Code="D", Video="", Description="It's D" },
        new GridViewEntity{ ID=5, Code="E", Video="", Description="It's E" }
    };
    
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    this.Bind();
                }
            }
    
            protected void gvData_RowEditing(object sender, GridViewEditEventArgs e)
            {
                GridView gv = (GridView)sender;
                gv.EditIndex = e.NewEditIndex;
                this.Bind();
            }
    
            // Cancel
            protected void gvData_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
            {
                GridView gv = (GridView)sender;
                gv.EditIndex = -1;
                this.Bind();
            }
    
            protected void gvData_RowUpdating(object sender, GridViewUpdateEventArgs e)
            {
                GridView gv = (GridView)sender;
                TextBox txbCode = gv.Rows[e.RowIndex].FindControl("txbCode") as TextBox;
                TextBox txbDescription = gv.Rows[e.RowIndex].FindControl("txbDescription") as TextBox;
                Label lblVideo = gv.Rows[e.RowIndex].FindControl("lblVideo") as Label;
    
                int id = Convert.ToInt32(gv.DataKeys[e.RowIndex].Value);
    
                GridViewEntity entity = source.Where(x => x.ID == id).Single();
                entity.Code = txbCode.Text;
                entity.Description = txbDescription.Text;
                entity.Video = lblVideo.Text;
    
                gv.EditIndex = -1;
                this.Bind();
            }
    
            private void Bind()
            {
                this.gvData.DataSource = source;
                this.gvData.DataBind();
            }
    
            protected void gvData_RowCommand(object sender, GridViewCommandEventArgs e)
            {
                GridView gv = (GridView)sender;
    
                int index = Convert.ToInt32(e.CommandArgument) - 1;
    
                if (Equals(e.CommandName, "Upload"))
                {
                    FileUpload file = gv.Rows[index].FindControl("fuVideo") as FileUpload;
                    Label lblVideo = gv.Rows[index].FindControl("lblVideo") as Label;
    
                    // Omit upload code...
    
                    Random random = new Random();
                    lblVideo.Text = random.Next(1000, 1000000).ToString();
                }
            }
    
            private class GridViewEntity
            {
    
                public int ID { get; set; }
                public string Code { get; set; }
    
                public string Video { get; set; }
                public string Description { get; set; }
            }
        }
    }

    Result:

    Best Regards,

    Brando

    Wednesday, August 1, 2018 5:44 AM