将网格视图单元格中的数据检索到文本框

时间:2010-12-22 15:51:45

标签: c# gridview selectedindexchanged

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

我已将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开发人员

工作

2 个答案:

答案 0 :(得分:3)

从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 + ".";

答案 1 :(得分:0)

 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)

谢谢你:)