【问题标题】:Retrieving data from grid view cell to a text box将数据从网格视图单元格检索到文本框
【发布时间】:2010-12-22 15:51:45
【问题描述】:

我正在尝试将单元格数据检索到文本框,当我在网格视图中选择任何行时会发生这种情况,文本框将采用新值

我已经在文本框中启用了auto post back

这是我的代码

protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
{

    TextBox3.Text = GridView2.Rows[GridView2.SelectedIndex].Cells[2].Text;
}

但是,语法没有错误,它没有在文本框中检索任何东西,有什么建议吗?

我正在使用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data.Sql;

我在 C# 工作,Visual Studio 2010 Express Web 开发人员

【问题讨论】:

  • 单元格在 XML 端是什么样子的?它是模板字段还是绑定字段?
  • @Bader:如果你有它背后的代码,那么你也有前端代码。
  • ,我认为它是一个boundField
  • @Bader:那么它真的应该以文本形式出现。确定是第三列吗?
  • 成功了,感谢您的时间和帮助:)

标签: c# gridview selectedindexchanged


【解决方案1】:

根据 VS 2010 文档,我建议先检查行和单元格是否不为空。

// Get the currently selected row using the SelectedRow property.
GridViewRow row = CustomersGridView.SelectedRow;

// Display the company name from the selected row.
// In this example, the third column (index 2) contains
// the company name.
MessageLabel.Text = "You selected " + row.Cells[2].Text + ".";

【讨论】:

    【解决方案2】:
     protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
    {
        Panel1.Visible = true;
    
        if (GridView2.SelectedIndex == 0)
        {
            webspace.Text = GridView2.Rows[GridView2.SelectedIndex].Cells[1].Text;
            Bandwidth.Text = GridView2.Rows[GridView2.SelectedIndex].Cells[2].Text;
            Email.Text = GridView2.Rows[GridView2.SelectedIndex].Cells[3].Text;
            FTP.Text = GridView2.Rows[GridView2.SelectedIndex].Cells[4].Text;
            Subdomain.Text = GridView2.Rows[GridView2.SelectedIndex].Cells[5].Text;
            mysql.Text = GridView2.Rows[GridView2.SelectedIndex].Cells[6].Text;
    
        }
    

    我在活动部分所做的,因为我确信它会在网格视图内排一排,所以if (GridView2.SelectedIndex == 0)

    谢谢你:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-15
      • 2018-02-12
      • 2013-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多