Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get selected row cell value of grid view on page?

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.

like image 586
divya Avatar asked May 23 '11 09:05

divya


1 Answers

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;
        }
    }
like image 178
Anyname Donotcare Avatar answered Oct 18 '22 11:10

Anyname Donotcare