如何在页面上获取网格视图的选定行单元格值?

时间:2022-08-24 11:42:03

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.

我想通过response.write()在页面按钮的click事件上显示所选行单元格的数据。

5 个解决方案

#1


8  

Note::

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

    请将按钮的CommandName设置为“selectCol”

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

    请为第二个按钮设置C​​ommandName,您将使用删除

    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;
        }
    }

#2


4  

Greeting Hims.

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

从gridview字段读取值的最简单方法是编写:your_grid_name.SelectedRow.Cell(* number_of_index *)。

In my case that is:

在我的情况下是:

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

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 ...

请记住,第一个单元格索引为零,并且不将“asp:CommandField ShowSelectButton”标记计为第一个...

#3


3  

Use GridView.SelectedRow property.

使用GridView.SelectedRow属性。

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

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

请参阅以下内容以了解如何在GridView控件中选择行。

Select Command in a GridView Control in ASP.Net

在ASP.Net中的GridView控件中选择Command

#4


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.

如果在网格视图中使用LINK BUTTON,则可以在ROWCOMMAND方法中使用以下代码。此代码检索特定选定行中的所有值。

 // 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;

#5


2  

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

您可以在gridview的RowCommand事件中获取它:

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


8  

Note::

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

    请将按钮的CommandName设置为“selectCol”

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

    请为第二个按钮设置C​​ommandName,您将使用删除

    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;
        }
    }

#2


4  

Greeting Hims.

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

从gridview字段读取值的最简单方法是编写:your_grid_name.SelectedRow.Cell(* number_of_index *)。

In my case that is:

在我的情况下是:

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

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 ...

请记住,第一个单元格索引为零,并且不将“asp:CommandField ShowSelectButton”标记计为第一个...

#3


3  

Use GridView.SelectedRow property.

使用GridView.SelectedRow属性。

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

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

请参阅以下内容以了解如何在GridView控件中选择行。

Select Command in a GridView Control in ASP.Net

在ASP.Net中的GridView控件中选择Command

#4


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.

如果在网格视图中使用LINK BUTTON,则可以在ROWCOMMAND方法中使用以下代码。此代码检索特定选定行中的所有值。

 // 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;

#5


2  

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

您可以在gridview的RowCommand事件中获取它:

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