1

I want to display my gridview details in respective textbox by clicking Edit buttonfield appear in gridview. I am able get only first row(row[0]) data, either by default or by clicking all over the another rows. But I need each row by clicking each row edit button. Here is my design code. I am using VS 2013 and SQL Server 2012.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Admin.aspx.cs"     Inherits="RigoTest.Admin" %>
<!DOCTYPE html>
   <html xmlns="http://www.w3.org/1999/xhtml">
   <head runat="server">
   <title></title>
   <style type="text/css">
    .auto-style1 {
        width: 50%;
    }
  </style>
</head>
<body>
<form id="form1" runat="server">
<div>
<center>
    <asp:GridView ID="gvdetails" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" 
        GridLines="None" Width="800px" HorizontalAlign="Center" OnRowEditing="gvdetails_RowEditing" OnRowCommand="gvdetails_RowCommand">
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        <Columns>
            <%--<asp:TemplateField HeaderText="ID" Visible="false">
                <EditItemTemplate>
                    <asp:TextBox ID="txtID" runat="server" Text='<%# Bind("ID") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="lblID" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Question">
                <EditItemTemplate>
                    <asp:TextBox ID="txtQue" runat="server" Text='<%# Bind("question") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="lblQue" runat="server" Text='<%# Bind("question") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>


            --%>

            <asp:BoundField DataField="ID" HeaderText="ID" Visible="false" />
            <asp:BoundField DataField="question" HeaderText="Question" />

            <%--<asp:CommandField ShowEditButton="True" HeaderText="Edit2"  />--%>
            <asp:ButtonField HeaderText="Edit" CommandName="edit" Text="Edit" />
            <%--<asp:CommandField CausesValidation="False" HeaderText="Edit2" InsertVisible="False" ShowCancelButton="False" ShowEditButton="True" UpdateText="" />--%>
        </Columns>
        <EditRowStyle BackColor="#999999" />
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" HorizontalAlign="Left" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#E9E7E2" />
        <SortedAscendingHeaderStyle BackColor="#506C8C" />
        <SortedDescendingCellStyle BackColor="#FFFDF8" />
        <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
    </asp:GridView>
</center>
</div>



    <div style="height: 300px; margin-top: 250px">
        <center>
        <table width="40%" class="auto-style1">
            <tr>
                <td>
                    Add/Edit Question</td>
                <td>
                    &nbsp;</td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="Label5" runat="server" Text="Question :"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txtquestion" runat="server" Width="295px"></asp:TextBox>
                </td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="Label3" runat="server" Text="Option :"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txtoption" runat="server" Width="295px"></asp:TextBox>
                </td>
                <td>
                    <asp:Button ID="btnadd" runat="server" Text="Add" Width="50px" OnClick="btnadd_Click" />
                </td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td>
                    <asp:ListBox ID="ListBox1" runat="server" Width="300px"></asp:ListBox>
                </td>
                <td>
                    <asp:Button ID="btndelete" runat="server" Text="Delete" Width="50px" OnClick="btndelete_Click" />
                    <br />
                    <br />
                    <asp:Button ID="btnselect" runat="server" Text="Select" Width="50px" OnClick="btnselect_Click" />
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="Label4" runat="server" Text="Correct Option :"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txtcorrectoption" runat="server" Width="295px"></asp:TextBox>
                </td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td>
                    <br />
                    <asp:Button ID="btnsave" runat="server" Text="Save" Width="100px" />
                </td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
            </tr>
        </table>
        </center>
    </div>
</form>
    </body>

HERE IS MY ASP.NET CODE

public partial class Admin : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("server = yamma; uid = sa; pwd = /*-; database = rigo");
    SqlCommand cmd;
    SqlDataAdapter da = new SqlDataAdapter();
    DataTable dt = new DataTable();
    public static DataTable dtDefault = new DataTable(); 
    public Int64 n;
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!Page.IsPostBack)
        {
            showdata();
            //string EditID = Request.QueryString["EditId"];
        }

    }

    public void showdata()
    {
        if (con.State == ConnectionState.Closed)
        {
            con.Open();
        }
        cmd = new SqlCommand("RigoSelect", con);
        cmd.CommandType = CommandType.StoredProcedure;
        da.SelectCommand = cmd;
        da.Fill(dt);
        da.Fill(dtDefault);
        gvdetails.DataSource = dt;
        gvdetails.DataBind();

    }



    protected void btnadd_Click(object sender, EventArgs e)
    {
        ListBox1.Items.Add(txtoption.Text);         //to select data from textbox to listbox
    }

    protected void btndelete_Click(object sender, EventArgs e)
    {
        string remove = ListBox1.SelectedItem.Text;
        ListBox1.Items.Remove(remove);

        ////clear all items in listbox
        //listBox1.Items.Clear();
    }

    protected void btnselect_Click(object sender, EventArgs e)
    {
        txtcorrectoption.Text = ListBox1.SelectedItem.Text;
    }

    protected void gvdetails_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        //private Int64 n;
        //for(int i=0;i<=n;i++)
        //{
        int id = int.Parse(gvdetails.Rows[0].Cells[0].Text);

        DataRow[] dr = null;
        dr = dtDefault.Select("ID = '" + id + "'");
        txtquestion.Text = dr[0]["question"].ToString();
        txtoption.Text = dr[0]["options"].ToString();
        //}
        showdata();


    }

    protected void gvdetails_RowEditing(object sender, GridViewEditEventArgs e)
    {
        // when boundfied is exist in gridview then id can get....thisway
        //int id = int.Parse(gvdetails.Rows[e.NewEditIndex].Cells[0].Text);

        ////Label lbl = (Label)gvdetails.Rows[e.NewEditIndex].Cells[0].FindControl("lblID");
        ////string id = lbl.Text.Trim();
        ////int EID = int.Parse(id);
        //// Response.Redirect("Admin.aspx?EditId=" + id + "");

        //DataRow[] dr = null;
        //dr = dtDefault.Select("ID = '" + id + "'");
        //txtquestion.Text = dr[0]["question"].ToString();
        //txtoption.Text = dr[0]["options"].ToString();

        //showdata();
    }


     }
 }

  My problem occurs here 

   protected void gvdetails_RowCommand(object sender, GridViewCommandEventArgs e)
     {
        //private Int64 n;
        //for(int i=0;i<=n;i++)
        //{
        int id = int.Parse(gvdetails.Rows[0].Cells[0].Text);

        DataRow[] dr = null;
        dr = dtDefault.Select("ID = '" + id + "'");
        txtquestion.Text = dr[0]["question"].ToString();
        txtoption.Text = dr[0]["options"].ToString();
        //}
        showdata();


    }

Here is my main problem occurs:

  protected void gvdetails_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        //private Int64 n;
        //for(int i=0;i<=n;i++)
        //{
        int id = int.Parse(gvdetails.Rows[0].Cells[0].Text);

        DataRow[] dr = null;
        dr = dtDefault.Select("ID = '" + id + "'");
        txtquestion.Text = dr[0]["question"].ToString();
        txtoption.Text = dr[0]["options"].ToString();
        //}
        showdata();


    }

When I click on any row that display only first row data. I want to get each row data by each clicking on respective textbox of grid row.

I have tried with commandfield but when try to add the data of option textbox (txtoption) in listbox with add button, it automatically display update, cancel option in gridview.

2 Answers 2

1

In the gvdetails_RowCommand event, you're always accessing the first row in the GridView.

int id = int.Parse(gvdetails.Rows[0].Cells[0].Text);

See if this works instead (referencing the currently selected row):

int id = int.Parse(gvdetails.SelectedRow.Cells[0].Text);
Sign up to request clarification or add additional context in comments.

Comments

0

Here is a sample code for your reference. Hope this helps.

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName =="edit")
        {
            int i = Convert.ToInt32(e.CommandArgument);
            YourTextBox1.Text = GridView1.Rows[i].Cells[0].Text;
            YourTextBox2.Text = GridView1.Rows[i].Cells[1].Text;

        }
    }

2 Comments

It working but I don't know why it need RowEditing Command as error: The GridView 'gvdetails' fired event RowEditing which wasn't handled.
bind your gridview on page load when if(!IsPostBack){ bind gridview}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.