传递gridview的值(数据)到文本框的字段

问题描述 投票:1回答:3

对于我的看法,在页面:

1) I have a gridview with the select hyperlink in it. The gridview data is from the SQLDataSource. 

2) And, I also have a few textboxes (abt 5) - not in the gridview.

我想这样做的是使用select hyperlink选择,我想编辑的row。当我点击select,在datarow应该出来各自textboxes。我该如何去这样做呢?

c# gridview textbox controls
3个回答
0
投票

网格视图具有内嵌的记录编辑支持,您可以通过AutoGenerateEditButton属性设置为true启用此功能。您必须指定在UpdateCommand物业存储过程或SQL查询的名称,这是用来更新基础数据的基础数据。

从MSDN:

      <asp:gridview id="CustomersGridView" 
        datasourceid="CustomersSqlDataSource" 
        autogeneratecolumns="true"
        autogeneratedeletebutton="true"
        autogenerateeditbutton="true"
        datakeynames="CustomerID"  
        runat="server">
      </asp:gridview>

      <asp:sqldatasource id="CustomersSqlDataSource"  
        selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
        updatecommand="Update Customers SET CompanyName=@CompanyName, Address=@Address, City=@City, PostalCode=@PostalCode, Country=@Country WHERE (CustomerID = @CustomerID)"
        deletecommand="Delete from Customers where CustomerID = @CustomerID"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
        runat="server">
      </asp:sqldatasource>

here为完整的示例代码。


0
投票

Wireup OnSelectedIndexChanged事件:

ASPX:

<asp:GridView id="gvTest"  OnSelectedIndexChanged="gvTest_SelectedIndexChanged"
 ..........></asp:GridView>

<asp:TextBox id="text1" runat="server"/>

码:

 protected void gvTest_SelectedIndexChanged(object sender, EventArgs e)
 {
    //get currently selected row
    var r =gvTest.Rows[gvTest.SelectedIndex];

    //THIS WAY YOU CAN GET TEXT FROM ALL COLUMNS
    text1.Text = r.Cells[r.Cells.Count - 1].Text;
 }

0
投票
 protected void gvofertas_RowCommand(object sender, GridViewCommandEventArgs e)
{
    try
    {
        gvofertas.SelectedIndex = Convert.ToInt32(e.CommandArgument);
        switch (e.CommandName)
        {

            case "ELIMINAR":
                {
                    //lblSolEliminar.Text = "Usuario: " + Convert.ToString(gvCorreos.DataKeys[gvCorreos.SelectedIndex].Values["etspcpusrn"]);
                    mpeEliminar.Show();
                    break;
                }
            case "EDITAR":
                {
                    Limpiar();
                    Session["NROOFERTAACTUALIZA"] = Convert.ToString(gvofertas.DataKeys[gvofertas.SelectedIndex].Values["efophcodi"]).Trim();
                    txtDescripcion.Text = Convert.ToString(gvofertas.DataKeys[gvofertas.SelectedIndex].Values["efophdesc"]).Trim();
                    StartDate.Text= Convert.ToDateTime(gvofertas.DataKeys[gvofertas.SelectedIndex].Values["efophfini"]).ToShortDateString();
                    EndDate.Text = Convert.ToDateTime(gvofertas.DataKeys[gvofertas.SelectedIndex].Values["efophffin"]).ToShortDateString();
                    txtRango1Localidades1Agregar.Text = Convert.ToString(gvofertas.DataKeys[gvofertas.SelectedIndex].Values["efophloci"]).Trim();
                    txtRango2Localidades1Agregar.Text = Convert.ToString(gvofertas.DataKeys[gvofertas.SelectedIndex].Values["efophlocf"]).Trim();
                    this.mpeAgregar.Show();
                    BtnGuardar2.Text = "Actualizar";
                    txtDescripcion.Focus();
                    break;
                }

    }
    catch (Exception ex)
    {

        ucMsje.RegistrarMensajeCliente("dvMsjeError", F.m_strImagenError, ex.Message);
    }
© www.soinside.com 2019 - 2024. All rights reserved.