8

I am using a Grid View on my page.

I want to show the data of the selected row cell through response.write(), on the click event of the page button.

Himanshu
  • 31,810
  • 31
  • 111
  • 133
divya
  • 405
  • 2
  • 6
  • 18
  • what coulmn type are you using in gridview like templatefield or bound field or .... better to put your gridview markup for better solutions:) – Devjosh May 23 '11 at 09:51
  • @Devjosh: column type is bound field. – divya May 23 '11 at 10:06
  • you have to handle the gridview selected index changed event and in this event store the value of the cell in a variable and then fetch that value on button click the varible can be placed in partial claa of page or control whatever you are using. – Devjosh May 23 '11 at 10:22
  • @Devjosh: please can you give the complete coding.? – divya May 23 '11 at 10:38

5 Answers5

8

Note::

  • please set the CommandName of your button to "selectCol"

  • Please set the CommandName for the second button , you will use to delete

    to"deleteCol"

Set the command argument property for your button :

.aspx

CommandArgument='<%#((GridViewRow)Container).RowIndex%>'
CommandArgument='<%#((GridViewRow)Container).RowIndex%>'

for the two buttons.

.cs

 protected void gv_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            int index = Convert.ToInt32(e.CommandArgument);
            if (e.CommandName == "selectCol")
            {
                Response.Write(gv.Rows[index].Cells[0].Text); //consider you use bound field and the column you want to show its value is the first column.
            }

            else if(e.CommandName == "deleteCol")
             {
                 int id = int.Parse(gv.DataKeys[index].Value.ToString());//the primary key for your table.
                 Delete(id);//method which use (Delete From .... Where id = ....).
             }


            gv.DataBind();

        }

        catch (Exception ee)
        {
            string message = ee.Message;
        }
    }
Anyname Donotcare
  • 11,113
  • 66
  • 219
  • 392
4

Greeting Hims.

Easyest way to read value from gridview field is to write:
your_grid_name.SelectedRow.Cell(*number_of_index*).text

In my case that is:

Dim employer_name As String
employer_name=poslodavac_grid.SelectedRow.Cells(1).Text

Just remember that first cell index is zero and that doesn't count "asp:CommandField ShowSelectButton" tag as first one ...

SeniB
  • 61
  • 1
3

If you are using a LINK BUTTON in your grid view, you can use the following code in the ROWCOMMAND method. This code with retrieve all the values in the particular selected row.

 // to get the value of the link use the command argument 
 FaultId = Convert.ToInt32(e.CommandArgument);

 // to get the other column values 

UserId = Convert.ToInt32(((GridViewRow(((LinkButton)e.CommandSource).NamingContainer)).Cells[1].Text);  

Department = ((GridViewRow(((LinkButton)e.CommandSource).NamingContainer)).Cells[2].Text;

ProblemType = ((GridViewRow)(((LinkButton)e.CommandSource).NamingContainer)).Cells[3].Text;
analyticalpicasso
  • 1,993
  • 8
  • 26
  • 45
Suganya
  • 31
  • 1
3

Use GridView.SelectedRow property.

String cellText = this.gridView.SelectedRow.Cells[cellIndex].Text;

Refer to the following to learn about selecting a row in a GridView control.

Select Command in a GridView Control in ASP.Net

Akram Shahda
  • 14,655
  • 4
  • 45
  • 65
  • protected void lnkbtnDelete_Click(object sender, EventArgs e) { String cellText = grdlistWord.SelectedRow.Cells[0].ToString(); Response.Write(cellText.ToString()); } – divya May 23 '11 at 10:12
  • 1
    when i am doing this "object reference not set to an instance of an object".error occur. – divya May 23 '11 at 10:13
  • @divya: I guess you havent selected a row then. Where exactly are you getting the error ? – Akram Shahda May 23 '11 at 10:18
  • String cellText = grdlistWord.SelectedRow.Cells[0].ToString(); here i am getting error.and i had selected row through checkbox – divya May 23 '11 at 10:24
  • @divya: It would be more appropriate if you used another way to select rows. Check the appended section of my answer. – Akram Shahda May 23 '11 at 10:40
  • @Akram Shahda : i can not select row in another way because its my task to select row through checkbox and delete selected row. – divya May 23 '11 at 11:44
2

You can get it in the RowCommand event of the gridview:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)

{
    if (e.CommandName == "Select")
    {
        GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
        Response.Write(row.Cells[0].Text);
        Response.Write(row.Cells[1].Text);
        ................
    }
}
Off The Gold
  • 1,228
  • 15
  • 28
Muhammad Akhtar
  • 51,913
  • 37
  • 138
  • 191