How can I get the selected cell value of the grid cells on the page?

I am using Grid View on my page.

I want to show the data of the selected cell of the cell via response.write() , in the page button click event.

+6
source share
5 answers

Note ::

  • set your CommandName to "selectCol"

  • Set CommandName for the second button, you will use uninstall

    in "deleteCol"

Set the command argument property for your button:

.aspx

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

for 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; } } 
+7
source

Greetings.

The easiest way to read the value from the gridview field is to write:
your_grid_name.SelectedRow.Cell (* number_of_index *). Text

In my case, this is:

Remove employer name as String
employer_name = poslodavac_grid.SelectedRow.Cells (1) .Text

Just remember that the first index of the cell is zero, and this is not considered the asp: CommandField ShowSelectButton tag as the first ...

+4
source

If you use LINK BUTTON as a grid, you can use the following code in the ROWCOMMAND method. This code retrieves all the values ​​in the selected 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; 
+3
source

Use the GridView.SelectedRow property.

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

See the next section for information on selecting a row in a GridView control.

Select a command in a GridView control in ASP.Net

+2
source

You can get it in the RowCommand gridview event:

 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); ................ } } 
+1
source

Source: https://habr.com/ru/post/888806/


All Articles