2014-02-24 12 views
1

Edit buttonfield를 클릭하여 각 텍스트 상자에 내 gridview 세부 정보를 표시하려면 gridview에 나타납니다. 기본적으로 또는 다른 행 전체를 클릭하여 첫 번째 행 (행 [0]) 데이터 만 가져올 수 있습니다. 하지만 각 행 편집 버튼을 클릭하여 각 행이 필요합니다. 여기 내 디자인 코드입니다.텍스트 상자에 표시하기 위해 gridview의 버튼 필드를 사용하여 각 행 데이터를 가져 오는 방법은 무엇입니까?

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(); 


    } 
을 : 나는 여기

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(); 


    } 

내 주요 문제가 발생한다 MY ASP.NET 코드 VS 2013 및 SQL 서버 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> 

이 사용하고

첫 번째 행 데이터 만 표시하는 행을 클릭하면 각 행 데이터를 그리드 행의 각 텍스트 상자를 클릭하여 가져 오려고합니다.

나는 commandfield를 사용해 보았지만 add button을 사용하여 목록 상자에 옵션 텍스트 상자 (txtoption)의 데이터를 추가하려고하면 gridview에 update, cancel 옵션이 자동으로 표시됩니다.

답변

1

gvdetails_RowCommand 이벤트에서 항상 GridView의 첫 번째 행에 액세스합니다. 여기

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

를 참조에 대한 샘플 코드 :이 (현재 선택한 행을 참조) 대신 작동하는 경우

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

을 참조하십시오. 희망이 도움이됩니다.

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; 

     } 
    } 
+0

왜 작동하는지 알 수 없으므로 RowEditing 명령을 오류로 처리해야합니다. GridView 'gvdetails'실행되지 않은 이벤트 RowEditing이 처리되지 않았습니다. – Arjun

+0

페이지로드시 gridview 바인딩 if (! IsPostBack) {bind gridview} – Amitesh