locked
Edit button outside GridView in VB.NET RRS feed

  • Question

  • User-1578974752 posted

    How can make below possible:

    Edit button is placed below the grid view.Once I click editbuttontwo columns in the gridview must show as Editable.user will type the all the 15 values in two columns,then press save button which outside the grid view.Then all values will be saved to the Oracle table.Is this possible in grid view?
    Appreciate the Help


                         </Columns>

         <asp:GridView ID="GridView1" runat="server"  Width="400px" DataSourceID="SqlDataSource1" AutoGenerateColumns="False" CellPadding="4" AllowPaging="True" BackColor="White" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" >

                         <Columns>
                             <asp:BoundField DataField="NUMBER" HeaderText="PO_NUMBER" SortExpression="UMBER" />
                             <asp:BoundField DataField="DESCRIPTION" HeaderText="ITEM_DESCRIPTION" SortExpression="DESCRIPTION" />
                             <asp:BoundField DataField="CODE" HeaderText="CODE" SortExpression="CODE" />
                            
                         </Columns>

                         <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
                         <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
                         <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
                         <RowStyle BackColor="White" ForeColor="#003399" />
                         <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
                         <SortedAscendingCellStyle BackColor="#EDF6F6" />
                         <SortedAscendingHeaderStyle BackColor="#0D4AC4" />
                         <SortedDescendingCellStyle BackColor="#D6DFDF" />
                         <SortedDescendingHeaderStyle BackColor="#002876" />

            </asp:GridView>
                    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString3 %>" ProviderName="<%$ ConnectionStrings:ConnectionString3.ProviderName %>" SelectCommand="SELECT &quot;NUMBER&quot;, &quot;DESCRIPTION&quot;, &quot;CODE&quot)">
                        <SelectParameters>
                            <asp:ControlParameter ControlID="ponumtxt" Name="PO_NUMBER" PropertyName="Text" Type="String" />
                        </SelectParameters>
                    </asp:SqlDataSource>
                    <br />
            <br />

    Thursday, October 25, 2018 8:55 AM

Answers

  • User-1716253493 posted

    In Gridview only one row can be edited, if you want all rows editable place a textbox in itemtemplate, then loop it to save the data.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 26, 2018 7:05 AM
  • User839733648 posted

    Hi shsu,

    Accorrding to your description, I've made a small sample and maybe could give you some ideas.

    Below is my code.

    .Aspx

            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
                <Columns>
                    <asp:BoundField DataField="name" HeaderText="name" />
                    <asp:BoundField DataField="tel" HeaderText="tel" />
                </Columns>
            </asp:GridView>
            <br />
            <asp:Button ID="Button_Edit" runat="server" OnClick="Btn_Edit_Click" Text="Edit" />

    Code-behind.

           protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    dt = new System.Data.DataTable();
                    dt.Columns.Add("name");
                    dt.Columns.Add("tel");
                    dt.Rows.Add("aaaaa", "1111");
                    dt.Rows.Add("bbbbb", "2222");
                    dt.Rows.Add("ccccc", "3333");
                    BindData();
                    isEditMode = false;
                }
    
                if (isEditMode)
                    AddTextBox();
            }
            System.Data.DataTable dt
            {
                get
                {
                    return (System.Data.DataTable)ViewState["dt"];
                }
                set
                {
                    ViewState["dt"] = value;
                }
            }
    
            bool isEditMode
            {
                get
                {
                    if (ViewState["isEditMode"] == null)
                        return false;
                    return (bool)ViewState["isEditMode"];
                }
                set
                {
                    ViewState["isEditMode"] = value;
                }
            }
    
            void BindData()
            {
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
    
            void AddTextBox()
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        GridView1.Rows[i].Cells[j].Controls.Add(new TextBox());
                    }
                }
            }
    
            void LoadDataIntoTextbox()
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        ((TextBox)GridView1.Rows[i].Cells[j].Controls[0]).Text = dt.Rows[i][j] + "";
                    }
                }
            }
    
            void SaveData()
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        dt.Rows[i][j] = ((TextBox)GridView1.Rows[i].Cells[j].Controls[0]).Text;
                    }
                }
    
                BindData();
            }
    
            protected void Btn_Edit_Click(object sender, EventArgs e)
            {
                isEditMode = !isEditMode;
                if (isEditMode)
                {
                    Button_Edit.Text = "Save";
                    AddTextBox();
                    LoadDataIntoTextbox();
                }
                else
                {
                    Button_Edit.Text = "Edit";
                    SaveData();
                }
            }

    result:

    Best Regards,

    Jenifer

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 30, 2018 9:48 AM

All replies

  • User632428103 posted

    Hello shsu,

    yes is possible try this sample;

            <asp:gridview ID="gv" runat="server" AutoGenerateColumns="false">
                <Columns>
                <asp:TemplateField HeaderText="BookId">  
                        <ItemTemplate>  
                            <asp:Label ID="lblBookId" runat="server" Text='<%#Eval("BookId") %>'></asp:Label>  
                        </ItemTemplate>  
                        <EditItemTemplate>  
                            <asp:TextBox ID="txtBookId" runat="server" Text='<%#Eval("BookId") %>'></asp:TextBox>  
                        </EditItemTemplate>  
                    </asp:TemplateField>  
                   </Columns>
                </asp:gridview>
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />

    code behind

     protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    bindGrid();
                }
            }
    
            void bindGrid()
            {
                gv.DataSource = SampleAppTest.BusinessObject.Book.GetBooksOnDataset();
                gv.DataBind();
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                gv.EditIndex = gv.Rows.Count - 1;
                bindGrid();
            }

    on click on button OUTSIDE the grid i edit the LAST ROW ...of course you need to KNOW WHICH ROW you want to edit 

    Thursday, October 25, 2018 9:55 AM
  • User-1578974752 posted

    If there is 20 rows in the grid view,Then on click of edit button ,I need all the 20 rows-(DESCRIPTION and code field) to be  editable at once and click save button all edited 20 rows(DESCRIPTION and code field) save at a time

    Thursday, October 25, 2018 10:20 AM
  • User632428103 posted

    yes it's possible make a foreach about the gridview.rows.count but for me it's not the best way ...

    because on save method, you will do the same ...

    I think the grid view is not the best control to use for what you try to make ...

    Perhpas a dataList will be better ...looks at this sample

    https://www.c-sharpcorner.com/UploadFile/009464/how-to-bind-and-perform-update-edit-delete-operation-on-da/

    Thursday, October 25, 2018 10:32 AM
  • User-1578974752 posted

    converted ur code to vb.net.Edit is working, But I request as below

    if there is 10 rows in grid view ,then upon clicking the Edit button,column2 and 3 of all rows 10 rows must be editable at once.

    For Each dRow As GridViewRow In kgrid.Rows

    kgrid.EditIndex = kgrid.Rows.Count - 1

    kgrid.EditIndex = kgrid.Rows.Count - 2

    kgrid.EditIndex = kgrid.Rows.Count - 3

    bindGrid()

    Next

    The above code is making only 1 row editable.

    Update statement to update all the values at once .Once save is click the values must be updated and the columns of all rows must become non editable.

    Class SurroundingClass

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If Not IsPostBack Then

    bindGrid()

    End If

    End Sub

    Private Sub bindGrid()

    gv.DataSource = SampleAppTest.BusinessObject.Book.GetBooksOnDataset()

    gv.DataBind()

    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)

    gv.EditIndex = gv.Rows.Count - 1

    bindGrid()

    End Sub

    end Class

    Friday, October 26, 2018 2:54 AM
  • User632428103 posted

    Hello all,

    @shsu => all right you have right i thought if you make a for and place editIndex  for each row it will be done BUT every time one row is editing !!!!!

    I've search on internet and i've found some article :

    http://highoncoding.com/Articles/219_GridView_All_Rows_in_Edit_Mode.aspx

    https://stackoverflow.com/questions/4314845/put-multiple-rows-of-a-gridview-into-edit-mode

    Perhpas these sample will help you ...

    but why you use a gridview why don't you use a dataList or a repeater ?

    Friday, October 26, 2018 7:03 AM
  • User-1716253493 posted

    In Gridview only one row can be edited, if you want all rows editable place a textbox in itemtemplate, then loop it to save the data.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 26, 2018 7:05 AM
  • User-1578974752 posted

    Item template is ok but upon clicking the edit button outside ,I want to make it as editable else the columns must look like normal columns. And upon clicking the save button these columns must become non editable. I could see some examples in net but all are c#.net codes.Upon conversion using telerik also some errors are coming..Hence how can I do this.. appreciate the help.

    Tried as below,  http://highoncoding.com/Articles/219_GridView_All_Rows_in_Edit_Mode.aspx

    bool is not declared,it may be inaccessible due to protection level
    leading'.'or'!' can only appear inside a with statement  such error are showing..

    <asp:TemplateField>

    <ItemTemplate>

    <asp:TextBox ID="TextBox1" Visible='<%# IsInEditMode %>' runat="server" Text='<%# Eval("QUANTITY_ORDERED") %>'></asp:TextBox>

    <asp:Label ID="Label94" Visible='<%# !(bool) IsInEditMode %>' runat="server" Text='<%# Eval("QUANTITY_ORDERED") %>'></asp:Label>

    </ItemTemplate>

    </asp:TemplateField>

    <asp:TemplateField></asp:TemplateField>

    </Columns>

    Friday, October 26, 2018 7:12 AM
  • User839733648 posted

    Hi shsu,

    Accorrding to your description, I've made a small sample and maybe could give you some ideas.

    Below is my code.

    .Aspx

            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
                <Columns>
                    <asp:BoundField DataField="name" HeaderText="name" />
                    <asp:BoundField DataField="tel" HeaderText="tel" />
                </Columns>
            </asp:GridView>
            <br />
            <asp:Button ID="Button_Edit" runat="server" OnClick="Btn_Edit_Click" Text="Edit" />

    Code-behind.

           protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    dt = new System.Data.DataTable();
                    dt.Columns.Add("name");
                    dt.Columns.Add("tel");
                    dt.Rows.Add("aaaaa", "1111");
                    dt.Rows.Add("bbbbb", "2222");
                    dt.Rows.Add("ccccc", "3333");
                    BindData();
                    isEditMode = false;
                }
    
                if (isEditMode)
                    AddTextBox();
            }
            System.Data.DataTable dt
            {
                get
                {
                    return (System.Data.DataTable)ViewState["dt"];
                }
                set
                {
                    ViewState["dt"] = value;
                }
            }
    
            bool isEditMode
            {
                get
                {
                    if (ViewState["isEditMode"] == null)
                        return false;
                    return (bool)ViewState["isEditMode"];
                }
                set
                {
                    ViewState["isEditMode"] = value;
                }
            }
    
            void BindData()
            {
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
    
            void AddTextBox()
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        GridView1.Rows[i].Cells[j].Controls.Add(new TextBox());
                    }
                }
            }
    
            void LoadDataIntoTextbox()
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        ((TextBox)GridView1.Rows[i].Cells[j].Controls[0]).Text = dt.Rows[i][j] + "";
                    }
                }
            }
    
            void SaveData()
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        dt.Rows[i][j] = ((TextBox)GridView1.Rows[i].Cells[j].Controls[0]).Text;
                    }
                }
    
                BindData();
            }
    
            protected void Btn_Edit_Click(object sender, EventArgs e)
            {
                isEditMode = !isEditMode;
                if (isEditMode)
                {
                    Button_Edit.Text = "Save";
                    AddTextBox();
                    LoadDataIntoTextbox();
                }
                else
                {
                    Button_Edit.Text = "Edit";
                    SaveData();
                }
            }

    result:

    Best Regards,

    Jenifer

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 30, 2018 9:48 AM