代码之家  ›  专栏  ›  技术社区  ›  Gopal

如何获取网格视图行值的文本框?

  •  5
  • Gopal  · 技术社区  · 16 年前

    使用c&mysql

    在我的网页中,如果我单击girdview中的列,该值将显示在文本框中。

    例如

    格里维

    Column1 column2 Column3
    
        1 Raja 9876
        2 Ravi 7890
        3 Ramu 9879
        ...
    

    如果我单击这两行,所有值都将显示在文本框中。

    Textbox1.text = 2
    textbox2.text = Ravi
    textbox3.text = 9879
    ...,
    

    如何为此条件编写代码。

    需要C代码帮助

    2 回复  |  直到 16 年前
        1
  •  2
  •   Giuseppe Accaputo    16 年前

    我想说的是 […]单击2行[…] “你的意思是” 单击第二行 “至少,这是您最后一段代码所建议的,因为它只显示第二行的值(注意:这里的ID是错误的;应该是 7890 )

    下面的代码段显示了 GridView 它允许选择单个行,并使用代码隐藏中的事件处理程序设置每个行的文本 TextBox 根据所选行中的相应值:

    PAG.ASPX :

    <asp:GridView runat="server" ID="gridView" OnSelectedIndexChanged="gridview_SelectedIndexChanged" AutoGenerateSelectButton="true"></asp:GridView>
    

    代码隐藏文件中的事件处理程序 PAC.ASPX.CS :

    void gridview_SelectedIndexChanged(object sender, EventArgs e)
    {
        var grid = sender as GridView;
        if (grid == null) return;
    
        //Cell[0] will be the cell with the select button; we don't need that one
        Textbox1.Text = grid.SelectedRow.Cell[1].Text /* 2 */;
        Textbox2.Text = grid.SelectedRow.Cell[2].Text /* Ravi */;
        Textbox3.Text = grid.SelectedRow.Cell[3].Text /* 7890 */;
    }
    
        2
  •  1
  •   Mark Cidade    16 年前

    你可以使用 EditItemTemplate 为此。

    参见代码项目文章, Editable Gridview with Textbox, CheckBox, Radio Button and DropDown List .