saurabh goyal
saurabh goyal

Reputation: 1744

grid view selected row data

i am getting the data from gridview on rowcommand event by the following code

protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "editproject")
    {
        string rowindex = e.CommandArgument.ToString();
        int index = int.Parse(rowindex);
        GridViewRow row = GridView2.Rows[index];
        Label6.Text = row.Cells[1].Text;
    }
}

but it would give only the data of the field that is visible in gridview row .how can i get the field that is not visible but bound to the gridview.

Upvotes: 1

Views: 6554

Answers (4)

Muhammad Akhtar
Muhammad Akhtar

Reputation: 52241

You can't get the value that you set to invisible because these were not rendered at the client side and can't be grabbed on the server side.

Alternatively you can store the value in hidden field and then you can get it from hidden field.

Upvotes: 2

azad
azad

Reputation: 11

private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{   
    MessageBox.Show(dataGridView1.SelectedRows[0].Cells[0].Value.ToString ());
}

Upvotes: 1

mohit
mohit

Reputation: 31

You can get a command-like button which is invisible in the gridview, just look at this:--- False visibility of button requires you to have changed the property EnableEventValidation="False" on the page directive in default.aspx

private void grd_bind()
{
    SqlDataAdapter adp = new SqlDataAdapter("select* from tbbook", ConfigurationManager.ConnectionStrings["cn"].ConnectionString);

    DataSet ds = new DataSet();
    adp.Fill(ds);
    GridView1.DataSource = ds;
    GridView1.DataBind();
}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        LinkButton lk = (LinkButton)(e.Row.Cells[5].Controls[0]);
        e.Row.Attributes["Onclick"] = ClientScript.GetPostBackClientHyperlink(lk, "");
    }
}

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
    TextBox1.Text = GridView1.SelectedRow.Cells[0].Text;
    TextBox2.Text = GridView1.SelectedRow.Cells[1].Text;
    TextBox3.Text = GridView1.SelectedRow.Cells[2].Text;
    TextBox4.Text = GridView1.SelectedRow.Cells[3].Text;
    TextBox5.Text = GridView1.SelectedRow.Cells[4].Text;
}

then in the default.aspx page, set EnableEventValidation

<%@ Page Language="VB" EnableEventValidation="false" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

Upvotes: 0

SMK
SMK

Reputation: 2158

You can,t get the invisible bound elements but you can get the value from data source .For example you save data in data table which assigned to grid .Store this data table in view-state and on row command get data-key of that row and retrieved value through data table

Upvotes: 0

Related Questions