2014-02-24 17 views
1

我想通过单击GridView中出现的Edit buttonfield来在相应的文本框中显示我的GridView细节。我只能得到第一行(行[0])的数据,默认情况下或点击其他行。但我需要通过单击每行编辑按钮来获得每一行。这是我的设计代码。我使用VS 2013和SQL Server 2012的如何使用gridview中的按钮字段在文本框中显示每行数据?

<%@ 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> 

这里是我的ASP.NET代码

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


    } 

下面是发生我的主要问题:

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


    } 

当我点击任何只显示第一行数据的行时。我想通过每次点击网格行的相应文本框来获取每行数据。

我已经尝试使用commandfield,但是当尝试添加选项文本框(txtoption)在列表框中的数据与添加按钮时,它会自动显示更新,取消选项在gridview中。

回答

1

gvdetails_RowCommand事件中,您总是访问GridView中的第一行。

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

见,如果这个工程,而不是(参考当前所选行):

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

这里是供您参考示例代码。希望这可以帮助。

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

相关问题