locked
when clicked on gridview edit button data is filled to source textboxes of same page? RRS feed

  • Question

  • User-1026236167 posted

    hello in my code when clicked on command field (select) button data of gridview is filled to source textboxes

    my problem is same operation is done on command field (edit) button

    when clicked on edit button data of gridview is filled to source textboxes and select does not perform

     

    css

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Data.SqlClient;

    namespace WebApplication14
    {
    public partial class WebForm61 : System.Web.UI.Page
    {
    SqlCommand cmd = new SqlCommand();
    SqlConnection con = new SqlConnection();
    string connection = System.Configuration.ConfigurationManager.AppSettings["con"].ToString();


    public void EstablishConnection(string storeprocedure)
    {
    con.ConnectionString = connection;
    cmd.Connection = con;
    cmd.Connection.Open();
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = storeprocedure;
    }


    public void CloseConnection()
    {
    cmd.Connection.Close();
    cmd.Connection.Dispose();
    con.Close();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
    if (!Page.IsPostBack)
    {
    FillGridview();

    }
    }

    public void FillGridview()
    {
    SqlDataAdapter adp = new SqlDataAdapter(" select * from tbl_package", connection);
    DataTable DT = new DataTable();
    adp.Fill(DT);
    GridView1.DataSource = DT;
    GridView1.DataBind();
    }

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
    TextBox1.Text = GridView1.SelectedRow.Cells[0].Text;
    TextBox2.Text = GridView1.SelectedRow.Cells[1].Text;
    TextBox3.Text = GridView1.SelectedRow.Cells[2].Text;
    TextBox4.Text = GridView1.SelectedRow.Cells[3].Text;

    }

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
    string s = GridView1.DataKeys[e.RowIndex].Value.ToString();
    string name = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
    string address = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
    string sal = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
    cmd = new SqlCommand("Update tbl_package set package_name='" + name + "',reward='" + address + "',remarks='" + sal + "' where tbl_id='" + s + "'", con);

    GridView1.EditIndex = -1;
    FillGridview();

    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
    GridView1.EditIndex = e.NewEditIndex;
    FillGridview();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
    GridView1.EditIndex = -1;
    FillGridview();
    }
    //protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    //{

    // cmd.CommandText = "DELETE FROM employee WHERE id='" + GridView1.DataKeys[e.RowIndex].Values[0].ToString() + "'";
    // con.Open();
    // cmd.ExecuteNonQuery();
    // con.Close();
    // FillGridview();
    //}
    }


    }

    aspx

    <%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="WebForm61.aspx.cs" Inherits="WebApplication14.WebForm61" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="title" runat="server">
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="head" runat="server">
    </asp:Content>
    <asp:Content ID="Content3" ContentPlaceHolderID="contentbody" runat="server">

    <div style="background-color: #FF9900; width: 356px;">

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
    DataKeyNames="tbl_id" Height="264px" onrowediting="GridView1_RowEditing"
    onrowupdating="GridView1_RowUpdating"
    onselectedindexchanged="GridView1_SelectedIndexChanged" Width="379px"
    BackColor="#66CCFF" BorderColor="#FF0066"
    onrowcancelingedit="GridView1_RowCancelingEdit"
    >
    <Columns>
    <asp:BoundField DataField="tbl_id" HeaderText="Id" />
    <asp:BoundField DataField="package_name" HeaderText="Name" />
    <asp:BoundField DataField="reward" HeaderText="Address" />
    <asp:BoundField DataField="remarks" HeaderText="Salary" />
    <asp:CommandField ShowEditButton="True" />
    <asp:CommandField ShowSelectButton="True" />
    <%-- <asp:CommandField ShowDeleteButton="True" />--%>

    </Columns>
    <SelectedRowStyle BackColor="#FF66FF" />
    </asp:GridView>

    &nbsp;&nbsp;&nbsp;&nbsp;
    <table class="style1" style="background-color: #CCFF99; height: 268px;">
    <tr>
    <td class="style2">
    ID</td>
    <td>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td class="style2">
    Name</td>
    <td>
    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td class="style2">
    Address</td>
    <td>
    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td class="style2">
    Salary</td>
    <td>
    <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
    </td>
    </tr>

    </table>

    <br />

    <br />

    </div>


    </asp:Content>

    Wednesday, June 3, 2020 12:39 PM

Answers

  • User1535942433 posted

    Hi prabhjot1313,

    As far I think,It's impossible.CommandField showeditbutton is defined in the gridview.When you click the edit button,it have update and cancel button.Of courses,you could hide cancel button using showcancelbutton=false.

    However,you also don't want to have update button.So,it's impossible.You must customize a edit button to implement your result.

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 5, 2020 7:32 AM

All replies

  • User1535942433 posted

    Hi prabhjot1313,

    Accroding to your description and codes,as far as I know,Using the ShowSelectButton property to specify whether a Select button is displayed in a CommandField field for each record in the data-source control. The Select button allows the user to select a row in the data-source control.Using the ShowEditButton property to specify whether an Edit button is displayed in the CommandField field for each record in the data-source control. The Edit button allows you to edit the values of a record.

    If you want to fill the textbox when click the edit button,I suggest you could bind the value to textbox when you click the edit button.

    More details,you could refer to below codes:

    <asp:Content ID="Content3" ContentPlaceHolderID="contentbody" runat="server">
        <div style="background-color: #FF9900; width: 356px;">
    
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
                DataKeyNames="tbl_id" Height="264px" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" Width="379px"
                BackColor="#66CCFF" BorderColor="#FF0066" OnRowCancelingEdit="GridView1_RowCancelingEdit">
                <Columns>
                    <asp:BoundField DataField="tbl_id" HeaderText="Id" />
                    <asp:BoundField DataField="package_name" HeaderText="Name" />
                    <asp:BoundField DataField="reward" HeaderText="Address" />
                    <asp:BoundField DataField="remarks" HeaderText="Salary" />
                    <asp:CommandField ShowEditButton="True" />
                    <asp:CommandField ShowSelectButton="True" />
                    <%-- <asp:CommandField ShowDeleteButton="True" />--%>
                </Columns>
                <SelectedRowStyle BackColor="#FF66FF" />
            </asp:GridView>
    
            &nbsp;&nbsp;&nbsp;&nbsp;
            <table class="style1" style="background-color: #CCFF99; height: 268px;">
                <tr>
                    <td class="style2">ID</td>
                    <td>
                        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td class="style2">Name</td>
                    <td>
                        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td class="style2">Address</td>
                    <td>
                        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td class="style2">Salary</td>
                    <td>
                        <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
                    </td>
                </tr>
    
            </table>
    
            <br />
    
            <br />
    
        </div>
    </asp:Content>

    Code-behind:

    protected void Page_Load(object sender, EventArgs e)
            {
                bind();
            }
    
            protected void bind()
            {
                string str, strSql;
                str = System.Configuration.ConfigurationManager.ConnectionStrings["aspnet-TestApplicationWithDatabase-20190820030542"].ConnectionString;
                SqlConnection conn = new SqlConnection(str);
                strSql = "select * from tbl_package";
                SqlDataAdapter da = new SqlDataAdapter(strSql, str);
                DataSet ds = new DataSet();
                da.Fill(ds, "tbl_package");
                this.GridView1.DataSource = ds.Tables[0].DefaultView;
                this.GridView1.DataBind();
                conn.Close();
            }
            protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
            {
                GridView1.EditIndex = e.NewEditIndex;
                bind();
                TextBox1.Text = GridView1.DataKeys[e.NewEditIndex].Value.ToString();
                TextBox2.Text = ((TextBox)GridView1.Rows[e.NewEditIndex].Cells[1].Controls[0]).Text;
                TextBox3.Text = ((TextBox)GridView1.Rows[e.NewEditIndex].Cells[2].Controls[0]).Text;
                TextBox4.Text = ((TextBox)GridView1.Rows[e.NewEditIndex].Cells[3].Controls[0]).Text;
            }
    
            protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
            {
                string s = GridView1.DataKeys[e.RowIndex].Value.ToString();
                string name = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
                string address = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
                string sal = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
    
                string constring = ConfigurationManager.ConnectionStrings["aspnet-TestApplicationWithDatabase-20190820030542"].ConnectionString;
                SqlConnection con = new SqlConnection(constring);
    
                SqlCommand cmd = new SqlCommand("Update tbl_package set package_name='" + name + "',reward='" + address + "',remarks='" + sal + "' where tbl_id='" + s + "'", con);
    
                GridView1.EditIndex = -1;
                bind();
               
            }
    
            protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
            {
                TextBox1.Text = GridView1.SelectedRow.Cells[0].Text;
                TextBox2.Text = GridView1.SelectedRow.Cells[1].Text;
                TextBox3.Text = GridView1.SelectedRow.Cells[2].Text;
                TextBox4.Text = GridView1.SelectedRow.Cells[3].Text;
            }
    
            protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
            {
                GridView1.EditIndex = -1;
                bind();
            }

    Result:

    Best regards,

    Yijing Sun

    Thursday, June 4, 2020 3:23 AM
  • User-1026236167 posted

    sir when clicked on edit button gridview does not play funcitionality of edit operation in gridview row only i clicked the edit button and data fill to source textboxes and also when clicked on command field edit button update and cancel button also not shown

    Thursday, June 4, 2020 6:00 AM
  • User1535942433 posted

    Hi prabhjot1313,

    when clicked on edit button gridview does not play funcitionality of edit operation in gridview row only i clicked the edit button and data fill to source textboxes and also when clicked on command field edit button update and cancel button also not shown

    Accroding to your description,I don't understand your requirment clearly.Do you cann't show update and cancel button when you click the commandfield edit button?

    I create a test and it works fine.

    Could you add punctuation marks for each sentences.It's help us to understand your requirment.Besides,could you psot full codes or sample result to us.It will help us to solve your problems.

    Best regards,

    Yijing Sun

    Thursday, June 4, 2020 8:55 AM
  • User-1026236167 posted

    yes sir i dont want update and cancel command field button only clicked on edit command field button and data filled to source textboxes?

    Thursday, June 4, 2020 10:29 AM
  • User1535942433 posted

    Hi prabhjot1313,

    Accroding to your description,I don't understand your requirment clearly.I have some guesses.

    1.You have a edit button and don't want to have a select command field.

    2.You want to click edit button to update the current row data.

    3.However,when you click the edit button,you don't want to have update and cancel button.Also,you don't want to show textbox when you have clicked the edit button.

    4.After clicking the edit button,the data will be filled into the textbox:ID,Name,Address,Salary.

    5.When you click the save button,the date will be updated into the database.

    If my guesses are right,I suggest you could use custom edit button without using showeditbutton and showselectbutton.

    If my guesses are wrong,you could point out.

    More details,you could refer to below codes:

    <%@ Page Title="" Language="C#" MasterPageFile="~/Site15.Master" AutoEventWireup="true" EnableEventValidation="false"  CodeBehind="WebForm97.aspx.cs" Inherits="Demo.WebForm97" %>
    
    <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    </asp:Content>
    <asp:Content ID="Content3" ContentPlaceHolderID="contentbody" runat="server">
        <div style="background-color: #FF9900; width: 356px;">
    <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
                DataKeyNames="tbl_id" Height="264px" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" Width="379px"
                BackColor="#66CCFF" BorderColor="#FF0066" OnRowCancelingEdit="GridView1_RowCancelingEdit">
                <Columns>
                    <asp:BoundField DataField="tbl_id" HeaderText="Id" />
                    <asp:BoundField DataField="package_name" HeaderText="Name" />
                    <asp:BoundField DataField="reward" HeaderText="Address" />
                    <asp:BoundField DataField="remarks" HeaderText="Salary" />
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:Button ID="Button1" runat="server" Text="Edit" OnClick="Button1_Click"/>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <SelectedRowStyle BackColor="#FF66FF" />
            </asp:GridView>
                 &nbsp;&nbsp;&nbsp;&nbsp;
            <table class="style1" style="background-color: #CCFF99; height: 268px;">
                <tr>
                    <td class="style2">ID</td>
                    <td>
                        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td class="style2">Name</td>
                    <td>
                        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td class="style2">Address</td>
                    <td>
                        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td class="style2">Salary</td>
                    <td>
                        <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
                    </td>
                </tr>
    
            </table>
    
            <br />
    
            <br />
                <asp:Button ID="Button2" runat="server" Text="Save" OnClick="Button2_Click" />
    
            </ContentTemplate>
        </asp:UpdatePanel>
           
        </div>
    </asp:Content>
           protected void Page_Load(object sender, EventArgs e)
            {
                bind();
            }
    
            protected void bind()
            {
                string str, strSql;
                str = System.Configuration.ConfigurationManager.ConnectionStrings["aspnet-TestApplicationWithDatabase-20190820030542"].ConnectionString;
                SqlConnection conn = new SqlConnection(str);
                strSql = "select * from tbl_package";
                SqlDataAdapter da = new SqlDataAdapter(strSql, str);
                DataSet ds = new DataSet();
                da.Fill(ds, "tbl_package");
                this.GridView1.DataSource = ds.Tables[0].DefaultView;
                this.GridView1.DataBind();
                conn.Close();
            }
            protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
            {
                GridView1.EditIndex = e.NewEditIndex;
                bind();
                TextBox1.Text = GridView1.DataKeys[e.NewEditIndex].Value.ToString();
                TextBox2.Text = ((TextBox)GridView1.Rows[e.NewEditIndex].Cells[1].Controls[0]).Text;
                TextBox3.Text = ((TextBox)GridView1.Rows[e.NewEditIndex].Cells[2].Controls[0]).Text;
                TextBox4.Text = ((TextBox)GridView1.Rows[e.NewEditIndex].Cells[3].Controls[0]).Text;
            }
    
            protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
            {
                string s = GridView1.DataKeys[e.RowIndex].Value.ToString();
                string name = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
                string address = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
                string sal = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
    
                string constring = ConfigurationManager.ConnectionStrings["aspnet-TestApplicationWithDatabase-20190820030542"].ConnectionString;
                SqlConnection con = new SqlConnection(constring);
    
                SqlCommand cmd = new SqlCommand("Update tbl_package set package_name='" + name + "',reward='" + address + "',remarks='" + sal + "' where tbl_id='" + s + "'", con);
    
                GridView1.EditIndex = -1;
                bind();
               
            }
    
            protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
            {
                TextBox1.Text = GridView1.SelectedRow.Cells[0].Text;
                TextBox2.Text = GridView1.SelectedRow.Cells[1].Text;
                TextBox3.Text = GridView1.SelectedRow.Cells[2].Text;
                TextBox4.Text = GridView1.SelectedRow.Cells[3].Text;
            }
    
            protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
            {
                GridView1.EditIndex = -1;
                bind();
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                Button btn = (Button)sender;
                GridViewRow gvr = (GridViewRow)btn.NamingContainer;
                TextBox1.Text = gvr.Cells[0].Text;
                TextBox2.Text = gvr.Cells[1].Text;
                TextBox3.Text = gvr.Cells[2].Text;
                TextBox4.Text = gvr.Cells[3].Text;
                
            }
    
            protected void Button2_Click(object sender, EventArgs e)
            {
                var cnnString = ConfigurationManager.ConnectionStrings["aspnet-TestApplicationWithDatabase-20190820030542"].ConnectionString;
                var querystring = "update tbl_package set package_name=@package_name,reward=@reward,remarks=@remarks where tbl_id=@tbl_id";
                using (SqlConnection cnn = new SqlConnection(cnnString))
                {
                    using (SqlCommand cmd = new SqlCommand(querystring, cnn))
                    {
                        cmd.Parameters.AddWithValue("@tbl_id", TextBox1.Text);
                        cmd.Parameters.AddWithValue("@package_name", TextBox2.Text);
                        cmd.Parameters.AddWithValue("@reward", TextBox3.Text);
                        cmd.Parameters.AddWithValue("@remarks", TextBox4.Text);
                        cnn.Open();
                        cmd.ExecuteNonQuery();
                    }
                }
                bind();
            }

    Result:

    Best regards,

    Yijing Sun

    Friday, June 5, 2020 3:30 AM
  • User-1026236167 posted

    sir your whole functionality is right but is this operation can done with command field edit button?

    Friday, June 5, 2020 6:20 AM
  • User1535942433 posted

    Hi prabhjot1313,

    As far I think,It's impossible.CommandField showeditbutton is defined in the gridview.When you click the edit button,it have update and cancel button.Of courses,you could hide cancel button using showcancelbutton=false.

    However,you also don't want to have update button.So,it's impossible.You must customize a edit button to implement your result.

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 5, 2020 7:32 AM