开发者

Retrieving data from grid view cell to a text box

I am trying to retrieve a cell data to a textbox , that will happen when i select any row in the grid view , the textbox will take the new value

I already enabled auto post back to the textbox

here is my code

protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
{

    TextBox3.Text开发者_如何学Python = GridView2.Rows[GridView2.SelectedIndex].Cells[2].Text;
}

however , there is not error in the syntax , it doesn't retrieve any thing in the textbox , any suggestions ?

i am using

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;

I work in C# , Visual studio 2010 express web developer


From VS 2010 documentation, I would recommend checking if the row and Cells are not null first.

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


 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;

    }

that what i did in the event section, because i am sure it will be one row inside the grid view , so if (GridView2.SelectedIndex == 0)

thank you :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜