【问题标题】:asp.net datagrid view row selection into textbox选择到文本框中的 asp.net datagridview 行
【发布时间】:2015-06-28 16:09:24
【问题描述】:

当用户单击网格视图中的编辑按钮时,我需要帮助将行选择放入文本框中,我有事件并将向您展示我尝试过的代码。 我在 Visual Studio 2013 和 sql server 2012 上运行。

txtname.Text = gridview.Rows[gridview.SelectedIndex].Cells[1].Text;

txtname.Text = gridview.Rows[gridview.SelectedRow].Cells[1].Text;

[屏幕]http://imgur.com/WCsvnLB,cLdMeQN

【问题讨论】:

    标签: c# asp.net sql-server datagrid


    【解决方案1】:

    而不是gridview.SelectedIndex 你应该有一个事件参数并获取它的索引。来自docs

    protected void TaskGridView_RowEditing(object sender, GridViewEditEventArgs e)
      {
        //Set the edit index.
        TaskGridView.EditIndex = e.NewEditIndex;
        //Bind data to the GridView control.
        BindData();
      }
    

    所以你的应该是:

    protected void gridView_RowEditing(object sender, GridViewEditEventArgs e)
    {
       txtname.Text = gridview.Rows[e.NewEditIndex].Cells[1].Text;
    }
    

    【讨论】:

      【解决方案2】:

      首先使用下面的代码创建一个 Gridview

       <asp:GridView ID="GV" runat="server" AutoGenerateColumns="false"  OnRowEditing="GV_RowEditing" DataKeyNames="id">
              <Columns>
                  <asp:TemplateField HeaderText="Username">
                      <ItemTemplate>
                          <asp:Label ID="Label1" runat="server" Text='<%#Eval("Column1")%>'></asp:Label>
                      </ItemTemplate>
                  </asp:TemplateField>
                  <asp:TemplateField HeaderText="Password">
                      <ItemTemplate>
                          <asp:Label ID="Label2" runat="server" Text='<%#Eval("Column2")%>'></asp:Label>
                      </ItemTemplate>
                  </asp:TemplateField>
                  <asp:TemplateField HeaderText="Action">
                      <ItemTemplate>
                          <asp:LinkButton ID="LinkButton1" runat="server" CommandName="edit" OnClick="LinkButton1_Click" >Edit</asp:LinkButton>
                      </ItemTemplate>
                  </asp:TemplateField>
              </Columns>
          </asp:GridView>
      

      然后生成gridview的行编辑事件并在其中编写这段代码

       string id = GV.DataKeys[e.NewEditIndex].Value.ToString();
          string select = "select * from tblLogin where id ='"+Convert.ToInt16(id)+"'";
          ds = gs.select(select);
          if (ds.Tables[0].Rows.Count > 0)
          {
             lblName.Text= ds.Tables[0].Rows[0]["Column1"].ToString();
              lblPass.Text=ds.Tables[0].Rows[0]["Column2"].ToString();
          }
      

      希望这会有所帮助

      【讨论】:

        【解决方案3】:

        所以我结合了你的两个答案,想出了一个效果很好的答案:

        int id = Convert.ToInt32(gridview.DataKeys[e.NewEditIndex].Value);
            txtid.Text = id.ToString();
            txtname.Text = gridview.Rows[id].Cells[3].Text;
            txtadd.Text = gridview.Rows[id].Cells[4].Text;
            txtcountry.Text = gridview.Rows[id].Cells[5].Text;
            txtcity.Text = gridview.Rows[id].Cells[6].Text;
            txtpin.Text = gridview.Rows[id].Cells[7].Text;
        

        让我知道你的想法,如果这是不好的编码,请告诉我。

        感谢

        【讨论】:

          猜你喜欢
          • 2021-12-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-05-13
          • 2013-11-07
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多