将gridview值(数据)传递给文本框字段

时间:2009-10-26 01:48:05

标签: c# gridview textbox controls

对于我看来, 在页面中:

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.

我想要做的是使用选择hyperlink来选择我要编辑的row。当我点击select时,data中的row应该会出现在他们各自的textboxes。 我该怎么做呢?

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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;
 }

答案 2 :(得分: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);
    }